- Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java. The language provides constructs intended to enable writing clear programs on both a small and large scale.
- Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.
- 1 Installation
- 2 Package management
- 3 Widget bindings
- 4 Tips and tricks
- 5 Troubleshooting
- 6 See also
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 the relevant chapter in Dive into Python 3.
To install the latest version of Python 3, install the package.
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
To get the latest version of Python 2, install the package.
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:
In both cases, just change
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 explicitly with
$ 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 will not work with
#!/usr/bin/python. This trick relies on
env searching for the first corresponding entry in the
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
$ export PATH=~/bin:$PATH
To check which python 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 called by the script in question, is to use a virtual environment.
Sections above refer to the reference implementation of Python, called CPython. However, there are also other implementations available - the most popular ones:
- PyPy is a Python 2.7/3.5 implementation utilizing a JIT compiler. It is generally faster and uses less memory, but is not fully compatible with CPython (although the majority of packages and code will work without any changes).
- Jython is a Python 2.7 implementation built in Java. It allows easy integration of Python and Java code, but is not fully compatible with CPython libraries. It is often used to provide Python as a scripting language in a bigger Java application.
- IronPython is a Python 2.7 implementation built in .NET - it achieves the same goals as Jython, but for .NET languages (like C#/VB).
- MicroPython is a limited Python 3.4 implementation targeting microcontrollers and other embedded environments (like UEFI), but is incompatible with most standard packages due to minor syntax changes and severely limited standard library. It is often used for prototyping in with embedded environments (as it provides a Python REPL).
- More implementations are available, although most are no longer maintained due to improvements in the most popular ones.
Old versions of Python are available via the AUR and may be useful for historical curiosity, old applications that do not run on current versions, or for testing Python programs intended to run on a distribution that comes with an older version:
- Python 3.6: AUR
- Python 3.5: AUR
- Python 3.4: AUR
- Python 2.6: AUR
- Python 2.5: AUR
- Python 1.5: AUR
Extra modules/libraries for old versions of Python may be found on the AUR by searching for
python<version without period>, e.g. searching for "python26" for 2.6 modules.
Although a great number of Python packages are readily available in the official repositories and the AUR, the Python ecosystem provides its own package managers for use with PyPI, the Python Package Index:
- pip — The PyPA tool for installing Python packages.
- https://pip.pypa.io/ || ,
- setuptools — Easily download, build, install, upgrade, and uninstall Python packages.
The following widget toolkit bindings are available:
- TkInter — Tk bindings
- https://wiki.python.org/moin/TkInter || standard module
- pyQt — Qt bindings
- pySide2 — Qt bindings
- pyGTK — GTK+ 2 bindings
- PyGObject — GTK+ 2/3 bindings via GObject Introspection
- wxPython — wxWidgets bindings
To use these with Python, you may need to install the associated widget kits.
Tips and tricks
- bpython — Fancy interface for the Python interpreter.
- IPython — Enhanced interactive Python shell.
- Jupyter Notebook — Web interface to IPython.
- ptpython — Fancy interface for the Python interpreter based on prompt-toolkit input interface.
- https://github.com/jonathanslenders/ptpython || AUR AUR
Python provides tools to create isolated environments in which you can install packages without interfering with the other virtual environments nor with the system Python's packages. It could change the python interpreter used for a specific application.
See Python/Virtual environment for details.
Tab completion in Python2 shell
import rlcompleter import readline readline.parse_and_bind("tab: complete")
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 from the
PATH environment variable 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 a workaround. Create
/usr/local/bin/python with content like this:
#!/bin/bash script=$(readlink -f -- "$1") case "$script" in (/path/to/project1/*|/path/to/project2/*|/path/to/project3*) exec python2 "$@" ;; esac exec python3 "$@"
/path/to/project1/*|/path/to/project2/*|/path/to/project3* is a list of patterns separated by
| matching all project trees. For some scripts, the path may not be the first parameter, for example Google SDK it sends "-S" as the first parameter. The readlink command should change to
script=$(readlink -f -- "$1").
Do not forget to make it executable. Afterwards scripts within the specified project trees will be run with Python 2.
- O'Reilly's Learning Python, 5th edition commercial
- Dive Into Python, Dive Into Python3
- A Byte of Python
- Learn Python the Hard Way
- Learn Python
- Crash into Python (assumes familiarity with other programming languages)
- Beginning Game Development with Python and Pygame commercial
- Think Python
- OverIQ Python Tutorial
- Python Tutorial to Learn Step by Step
- awesome-python - A curated list of Python frameworks, libraries, software and resources.
- boltons - Constructs/recipes/snippets that would be handy in the standard library.