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

Warning: Python 2 reached its end of life on January 1st of 2020. A small number of packages still depend on Python 2 but that version of Python will not be maintained further. See: Sunsetting 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 last version of Python 2, install the package.

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

Any program requiring Python 2 needs to use , instead of , which points to Python 3. However, many legacy Python 2 scripts incorrectly specify 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 to 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 my_script.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 searching for the first corresponding entry in the variable.

First create a dummy folder:

$ mkdir ~/bin

Then add a symlink to python2 and the configuration 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 variable:

$ export PATH=~/bin:$PATH

To check which python interpreter is being used by , use the following command:

$ command -v 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. These implementations are usually based on older versions of Python and are not fully compatible with CPython.

Implementations available on Arch Linux include:

  • micropython Python for microcontrollers. It includes a small subset of the Python standard library and is optimized to run on microcontrollers and in constrained environments.
https://micropython.org/ || micropythonAUR

    More implementations exist. Some, such as Stackless, Pyston and Cinder are used internally at large technology companies. Others are historically notable but are no longer maintained due to improvements in the most popular implementations.

    Alternative shells

    The python package includes an interactive Python shell/REPL which can be launched with the command. The following shells are also available:

    • Jupyter A web-based computation application powered by IPython.
    https://jupyter.org/ || jupyterlab, jupyter-notebook

      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.9:
      • Python 3.8:
      • Python 3.7:
      • Python 3.6:

      Extra modules/libraries for old versions of Python may be found on the AUR by searching for , e.g. searching for for 3.7 modules.

      As an alternative which does not require superuser capabilities, consider using pyenv to install additional Python versions to the home directory.

      Package management

      There are several ways to install Python packages on Arch Linux:

      When installing packages using pip, it is recommended to use a virtual environment to prevent conflicts with system packages in . Alternatively, can be used to install packages into the user scheme instead of . pipx and Conda integrate environment management into their workflows.

      See the Python Packaging User Guide for the official best practices for package management.

      Historically, easy_install (part of ) was used to install packages distributed as Eggs. easy_install and Eggs have been replaced with pip and Wheels. See pip vs easy_install and Wheel vs Egg for more information.

      Note: There are also tools integrating pip with pacman by automatically generating PKGBUILDs for specified PyPI packages: see Creating packages#PKGBUILD generators.

      Widget bindings

      The following widget toolkit bindings are available:

      • Qt for Python (PySide2) The official Python bindings for Qt5.
      https://www.qt.io/qt-for-python || pyside2, pyside2-tools

      To use these with Python, you may also need to install the associated widget toolkit packages (e.g. must also be installed to use Tkinter).

      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

      Tab completion is available in the interactive shell by default. Note that the readline completer will only complete names in the global namespace. You can use for a richer tab completion experience .

      See also


      Official

      Third-Party

      This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.