Python
From What is Python?:
- Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. It supports multiple programming paradigms beyond object-oriented programming, such as procedural and functional programming. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants including Linux and macOS, and on Windows.
Installation
Python 3
Python 3 is the latest and actively developed version of the language. See What's New in Python to see the latest changes in Python 3.
To install the current release of Python 3, install the python 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 /usr/local/bin/python3.x
. As an alternative which does not require superuser capabilities and installs to the home directory, consider using pyenv.
Python 2
Python 2 is an older version of the language. Python 3 and Python 2 are incompatible. For an overview of the differences, see the historical version of the Python2orPython3 document.
Although Python 2 is no longer actively maintained, there are some packages that still depend on it. Python 2 may also be useful for developers maintaining, using or porting legacy Python 2 software.
To get the latest version of Python 2, install the python2 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 use /usr/bin/python2
, instead of /usr/bin/python
, which points to Python 3. However, many legacy Python 2 scripts incorrectly specify python
in their shebang line. To change this, open the program or script in a text editor and change the first line. The line may show one of the following:
#!/usr/bin/env python
or
#!/usr/bin/python
In either case, 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 explicitly with python2
:
$ 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 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
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.
Alternative implementations
The python package installs CPython, the reference implementation of Python. However, there are also other implementations available:
- PyPy is a Python 2.7/3.6/3.7 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). It is incompatible with most standard packages due to minor syntax changes and a severely limited standard library. It is often used for prototyping in embedded environments with the included REPL.
- More implementations are available, although most are no longer maintained due to improvements in the most popular ones.
Alternative shells
The python package includes an interactive Python shell/REPL which can be launched with the python
command. The following shells are also available:
- bpython — A fancy interface for the Python interpreter.
- IPython — A powerful interactive Python shell.
- Jupyter — A web-based computation application powered by IPython.
- ptpython — An advanced Python REPL built with prompt-toolkit.
Old versions
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.8: python38AUR
- Python 3.7: python37AUR
- Python 3.6: python36AUR
- Python 3.5: python35AUR
- Python 3.4: python34AUR
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.
Package management
There are several ways to install Python packages on Arch Linux:
- Official repositories and AUR — A large number of popular packages are available in the Arch repositories. This is the preferred way to install system-wide packages.
- pip — The official package installer for Python. You can use pip to install packages from the Python Package Index and other indexes.
- Anaconda — An open source package management system and environment management system, originally created for Python programs. You can use Conda to install packages from the Anaconda repositories.
- Miniconda — A lightweight alternative to Anaconda which installs the package manager but does not install scientific computing packages by default.
When installing packages from sources other than the official repositories and AUR, it is recommended to use a virtual environment (or Conda environment management) to prevent conflicts with system packages in /usr
. Alternatively, pip install --user
can be used to install packages into the user scheme instead of /usr
.
See the Python Packaging User Guide for the official best practices for package management.
Historically, easy_install (part of python-setuptools) was used to install packages distributed as Eggs. easy_install and Eggs have been replaced with pip and Wheels. See see pip vs easy_install and Wheel vs Egg for more information.
Widget bindings
The following widget toolkit bindings are available:
- Tkinter — the standard Python interface to the Tk GUI toolkit
- Qt for Python (PySide2) — The official Python bindings for Qt
- pyQt — A set of Python bindings for Qt
- PyGObject — Python bindings for GObject based libraries such as GTK, GStreamer, WebKitGTK, GLib, and GIO
- wxPython — A cross-platform GUI toolkit for Python which wraps wxWidgets
To use these with Python, you may need to install the associated widget kits.
Tips and tricks
Virtual environment
Python provides tools to create isolated virtual environments into which packages may be installed without conflicting with other virtual environments or the system packages. Virtual environments can also run applications with different versions of Python on the same system.
See Python/Virtual environment for details.
Tab completion in Python shell
Since Python 3.4 tab completion is enabled by default. Note that the readline completer will only complete names in the global namespace. You can use python-jedi for a richer tab completion experience [1].
Troubleshooting
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:
/usr/local/bin/python
#!/bin/bash script=$(readlink -f -- "$1") 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 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.
See also
Official
Third-Party
- Automate the Boring Stuff with Python - Creative Commons book
- Awesome Python - A curated list of Python resources
- A Byte of Python - Creative Commons book
- Cracking Codes With Python - Free online book
- Crash into Python - Free tutorial
- Debugging in Python - Guide to using
pdb
, the Python debugger - Dive Into Python - Creative Commons book
- Fluent Python - Commercial book
- Introducing Python - Commercial book
- Invent Your Own Computer Games with Python - Free online book
- Learn Python - Free interactive tutorial
- Learn Python the Hard Way - Commercial book
- Pythonspot Python Tutorials - Free online tutorials
- Think Python - Creative Commons book