Difference between revisions of "Python"

From ArchWiki
Jump to: navigation, search
(Added config script when switching python version)
(Removed section "A permanent approach to switching to Python 2.x". This will just lead to breaking packages or file system conflicts with pacman. If you want to override the default version of Python, put a symlink in $HOME/bin directory and add to PATH)
Line 54: Line 54:
To check which python interpreter is being used by {{ic|env}}, use the following command:
To check which python interpreter is being used by {{ic|env}}, use the following command:
  $ which python
  $ which python
==A permanent approach to switching to Python 2.x==
Instead of changing the local user environment to python 2.x, the default python version for the system can be set. First type into your console
$ cd /usr/bin
and then type in the command
$ ls -l python
which should something like
$ lrwxrwxrwx 1 root root 7 Dec 22 13:23 python -> python3
If you receive some kind of error, then python3 is most likely not installed. In that case just install python2 by issuing the command
$ sudo pacman -S python2
To make this the default, if you are not in root, enter the commands
$ sudo ln -sf python2 python
$ sudo ln -sf python2-config python-config
which switches your default version of python to python 2.x. To ensure that you are now using the correct version, type in the comman
$ ls -l python
which will return a value that looks like
$ lrwxrwxrwx 1 root root 7 Mar 13 22:23 python -> python2
==Dealing with version problem in build scripts==
==Dealing with version problem in build scripts==

Revision as of 19:18, 28 March 2013

zh-CN:Python Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

Python "is a remarkably powerful dynamic programming language that is used in a wide variety of application domains. Python is often compared to Tcl, Perl, Ruby, Scheme or Java."


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 mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Also, the standard library has been reorganized in a few prominent places. For an overview of the differences, visit Python2orPython3 and their relevant chapter in Dive into Python 3.

To install the latest version of Python 3, install the python package from the official repositories.

If you would like to build the latest RC/betas from source, visit Python Downloads. 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 2

To install the latest version of Python 2, install the 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.

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



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 only works if the scripts use #!/usr/bin/env python, it won't work with #!/usr/bin/python. This trick relies 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 which python interpreter is being used by env, use the following command:

$ which python

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 all contained within the project tree. So, instead of modifying the build scripts manually, there is an easy workaround. Just create /usr/local/bin/python with content like this:

script=`readlink -f -- "$1"`
case "$script" in
    exec python2 "$@"

exec python3 "$@"

Where /path/to/project1/*|/path/to/project2/*|/path/to/project3* is a list of patterns separated by | 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

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


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


For the latest Python 3 compatible version, install the eric package.

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

These IDEs can also handle Ruby.


The Ninja IDE is provided by the package ninja-ide.


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

Getting easy_install

The easy_install tool is available in the package python-distribute or python2-distribute.

Widget bindings

The following widget toolkit bindings are available:

http://wiki.python.org/moin/TkInter || standard module
http://www.riverbankcomputing.co.uk/software/pyqt/intro || pyqt
http://www.pyside.org/ || python2-pyside
http://www.pygtk.org/ || pygtk
http://wxpython.org/ || 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 applications that don't run on current versions, or for testing Python programs intended to run on a distribution that comes with an older version (eg, RHEL 5.x has Python 2.4, or Ubuntu 12.04 has Python 3.1):

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 not recommended.

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

More Resources

For Fun

Try the following snippets from Python's interactive shell:

>>> import this
>>> from __future__ import braces
>>> import antigravity