Python arch wiki

6

Click here to load reader

description

litelatur

Transcript of Python arch wiki

Page 1: Python   arch wiki

Related articles

Python PackageGuidelines

mod_python

Python VirtualEnv

PythonFrom ArchWiki

Python (http://www.python.org) "is a remarkably powerful dynamicprogramming language that is used in a wide variety of application domains.Python is often compared to Tcl, Perl, Ruby, Scheme or Java."

Contents

1 Installation1.1 Python 31.2 Python 2

2 Dealing with version problem in build scripts3 Integrated Development Environments

3.1 Eclipse3.2 Eric3.3 IEP3.4 Ninja3.5 Spyder

4 Getting easy_install5 Getting completion in Python shell6 Widget bindings7 Old versions8 More Resources9 For Fun

Installation

There are currently two versions of Python: Python 3 (which is the default) and the older Python 2.

Python 3

Python 3 is the latest version of the language, and is incompatible with Python 2. The language is mostlythe same, but many details, especially how built-in objects like dictionaries and strings work, have changedconsiderably, and a lot of deprecated features have finally been removed. Also, the standard library has beenreorganized in a few prominent places. For an overview of the differences, visit Python2orPython3(http://wiki.python.org/moin/Python2orPython3) and their relevant chapter (http://getpython3.com/diveintopython3/porting-code-to-python-3-with-2to3.html) in Dive into Python 3.

To install the latest version of Python 3, install the python (https://www.archlinux.org/packages

/?name=python) package from the official repositories.

If you would like to build the latest RC/betas from source, visit Python Downloads (http://www.python.org/download/). The Arch User Repository also contains good PKGBUILDs. If you do decide to build the RC,note that the binary (by default) installs to /usr/local/bin/python3.x .

Python - ArchWiki https://wiki.archlinux.org/index.php/Python

1 of 6 1/29/2014 12:40 PM

Page 2: Python   arch wiki

Python 2

To install the latest version of Python 2, install the python2 (https://www.archlinux.org/packages

/?name=python2) package from the official repositories.

Python 2 will happily run alongside Python 3. You need to specify python2 in order to run this version.

Any program requiring Python 2 needs to point to /usr/bin/python2 , instead of /usr/bin/python ,which points to Python 3.

To do so, open the program or script in a text editor and change the first line.

The line will show one of the following:

#!/usr/bin/env python

or

#!/usr/bin/python

In both cases, just change python to python2 and the program will then use Python 2 instead of Python 3.

Another way to force the use of python2 without altering the scripts is to call it explicitely with python2, i.e.

python2 myScript.py

Finally, you may not be able to control the script calls, but there is a way to trick the environment. It onlyworks if the scripts use #!/usr/bin/env python , it won't work with #!/usr/bin/python . This trickrelies on env searching for the first corresponding entry in the PATH variable. First create a dummy folder.

$ mkdir ~/bin

Then add a symlink 'python' to python2 and the config scripts in it.

$ ln -s /usr/bin/python2 ~/bin/python$ ln -s /usr/bin/python2-config ~/bin/python-config

Finally put the new folder at the beginning of your PATH variable.

$ export PATH=~/bin:$PATH

Note that this change is not permanent and is only active in the current terminal session. To check whichpython interpreter is being used by env , use the following command:

$ which python

A similar approach in tricking the environment, which also relies on #!/usr/bin/env python to be calledby the script in question, is to use a Virtualenv. When a Virtualenv is activated, the Python executablepointed to by $PATH will be the one the Virtualenv was installed with. Therefore, if the Virtualenv is

Python - ArchWiki https://wiki.archlinux.org/index.php/Python

2 of 6 1/29/2014 12:40 PM

Page 3: Python   arch wiki

installed with Python 2, python will refer to Python 2. To start, install python2-virtualenv(https://www.archlinux.org/packages/?name=python2-virtualenv).

# pacman -S python2-virtualenv

Then create the Virtualenv.

$ virtualenv2 venv # Creates a directory, venv/, containing the Virtualenv

Activate the Virtualenv, which will update $PATH to point at Python 2. Note that this activation is onlyactive for the current terminal session.

$ source venv/bin/activate

The desired script should then run using Python 2.

Dealing with version problem in build scripts

Many projects' build scripts assume python to be Python 2, and that would eventually result in an error -typically complaining that print 'foo' is invalid syntax. Luckily, many of them call python in the$PATH instead of hardcoding #!/usr/bin/python in the shebang line, and the Python scripts are allcontained within the project tree. So, instead of modifying the build scripts manually, there is an easyworkaround. Just create /usr/local/bin/python with content like this:

/usr/local/bin/python

#!/bin/bashscript=`readlink -f -- "$1"`case "$script" in (/path/to/project1/*|/path/to/project2/*|/path/to/project3*) exec python2 "$@" ;;esac

script=`readlink -f -- "$2"`case "$script" in (/path/to/project1/*|/path/to/project2/*|/path/to/project3*) exec python2 "$@" ;;esac

exec python3 "$@"

Where /path/to/project1/*|/path/to/project2/*|/path/to/project3* is a list of patterns separatedby | matching all project trees.

Don't forget to make it executable:

# chmod +x /usr/local/bin/python

Afterwards scripts within the specified project trees will be run with Python 2.

Integrated Development Environments

Python - ArchWiki https://wiki.archlinux.org/index.php/Python

3 of 6 1/29/2014 12:40 PM

Page 4: Python   arch wiki

There are some IDEs for Python available in the official repositories.

Eclipse

Eclipse supports both Python 2.x and 3.x series by using the PyDev extension.

Eric

For the latest Python 3 compatible version, install the eric (https://www.archlinux.org/packages

/?name=eric) package.

Version 4 of Eric is Python 2 compatible and can be installed with the eric4

(https://www.archlinux.org/packages/?name=eric4) package.

These IDEs can also handle Ruby.

IEP

IEP is an interactive (e.g. MATLAB) python IDE with basic debugging capabilities and is especially suitablefor scientific computing. It is provided by the package iep (https://aur.archlinux.org/packages

/iep/).

Ninja

The Ninja IDE is provided by the package ninja-ide (https://www.archlinux.org/packages

/?name=ninja-ide).

Spyder

Spyder (previously known as Pydee) is a powerful interactive development environment for the Pythonlanguage with advanced editing, interactive testing, debugging and introspection features. It focuses onscientific computations, providing a matlab-like environment. It can be installed with the package spyder

(https://aur.archlinux.org/packages/spyder/)

Getting easy_install

The easy_install tool is available in the package python-setuptools (https://www.archlinux.org

/packages/?name=python-setuptools).

Getting completion in Python shell

Copy this into Python's interactive shell

/usr/bin/python

import rlcompleterimport readlinereadline.parse_and_bind("tab: complete")

Python - ArchWiki https://wiki.archlinux.org/index.php/Python

4 of 6 1/29/2014 12:40 PM

Page 5: Python   arch wiki

Source (http://algorithmicallyrandom.blogspot.com.es/2009/09/tab-completion-in-python-shell-how-to.html)

Widget bindings

The following widget toolkit bindings are available:

TkInter — Tk bindings

http://wiki.python.org/moin/TkInter || standard module

pyQt — Qt bindings

http://www.riverbankcomputing.co.uk/software/pyqt/intro || python2-pyqt4(https://www.archlinux.org/packages/?name=python2-pyqt4) python2-pyqt5

(https://www.archlinux.org/packages/?name=python2-pyqt5) python-pyqt4

(https://www.archlinux.org/packages/?name=python-pyqt4) python-pyqt5

(https://www.archlinux.org/packages/?name=python-pyqt5)

pySide — Qt bindings

http://www.pyside.org/ || python2-pyside (https://aur.archlinux.org/packages/python2-pyside/) python-pyside (https://aur.archlinux.org/packages/python-pyside/)

pyGTK — GTK+ 2 bindings

http://www.pygtk.org/ || pygtk (https://www.archlinux.org/packages/?name=pygtk)

PyGObject — GTK+ 2/3 bindings via GObject Introspection

https://wiki.gnome.org/PyGObject/ || python2-gobject2 (https://www.archlinux.org/packages/?name=python2-gobject2) python2-gobject (https://www.archlinux.org/packages

/?name=python2-gobject) python-gobject2 (https://www.archlinux.org/packages

/?name=python-gobject2) python-gobject (https://www.archlinux.org/packages

/?name=python-gobject)

wxPython — wxWidgets bindings

http://wxpython.org/ || wxpython (https://www.archlinux.org/packages/?name=wxpython)

To use these with Python, you may need to install the associated widget kits.

Old versions

Old versions of Python are available via the AUR and may be useful for historical curiosity, old applicationsthat don't run on current versions, or for testing Python programs intended to run on a distribution thatcomes with an older version (eg, RHEL 5.x has Python 2.4, or Ubuntu 12.04 has Python 3.1):

python15 (https://aur.archlinux.org/packages/python15/): Python 1.5.2python16 (https://aur.archlinux.org/packages/python16/): Python 1.6.1python24 (https://aur.archlinux.org/packages/python24/): Python 2.4.6python25 (https://aur.archlinux.org/packages/python25/): Python 2.5.6python26 (https://aur.archlinux.org/packages/python26/): Python 2.6.8python30 (https://aur.archlinux.org/packages/python30/): Python 3.0.1

Python - ArchWiki https://wiki.archlinux.org/index.php/Python

5 of 6 1/29/2014 12:40 PM

Page 6: Python   arch wiki

python31 (https://aur.archlinux.org/packages/python31/): Python 3.1.5python32 (https://aur.archlinux.org/packages/python32/): Python 3.2.3

As of November 2012, Python upstream only supports Python 2.6, 2.7, 3.1, 3.2, and 3.3 for security fixes.Using older versions for Internet-facing applications or untrusted code may be dangerous and is notrecommended.

Extra modules/libraries for old versions of Python may be found on the AUR by searching forpython(version without decimal), eg searching for "python26" for 2.6 modules.

More Resources

Learning Python (http://shop.oreilly.com/product/9780596158071.do) is one of the mostcomprehensive, up to date, and well-written books on Python available today.Dive Into Python (http://www.diveintopython.net/) is an excellent (free) resource, but perhaps formore advanced readers and has been updated for Python 3 (http://diveintopython3.ep.io/).A Byte of Python (http://www.swaroopch.com/notes/Python) is a book suitable for users new toPython (and scripting in general).Learn Python The Hard Way (http://learnpythonthehardway.org) the best intro to programming.facts.learnpython.org (http://facts.learnpython.org) nice site to learn python.Crash into Python (http://stephensugden.com/crash_into_python/) Also known as Python forProgrammers with 3 Hours, this guide gives experienced developers from other languages a crashcourse on Python.Beginning Game Development with Python and Pygame: From Novice to Professional(http://www.apress.com/book/view/9781590598726) for games

For Fun

Try the following snippets from Python's interactive shell:

>>> import this

>>> from __future__ import braces

>>> import antigravity

Retrieved from "https://wiki.archlinux.org/index.php?title=Python&oldid=294398"Category: Programming language

This page was last modified on 25 January 2014, at 21:30.Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.

Python - ArchWiki https://wiki.archlinux.org/index.php/Python

6 of 6 1/29/2014 12:40 PM