Python: Difference between revisions

From ArchWiki
(Undo revision 755095 by Dharmab (talk) - this was meant as an alternative to installing packages from AUR)
Tag: Undo
(Undo revision 806228 by Jlownie (talk) - tkinter is provided by the python package, tk is just a dependency of tkinter)
Tag: Undo
 
(31 intermediate revisions by 17 users not shown)
Line 4: Line 4:
[[pt:Python]]
[[pt:Python]]
[[ru:Python]]
[[ru:Python]]
[[uk:Python]]
[[zh-hans:Python]]
[[zh-hans:Python]]
{{Related articles start}}
{{Related articles start}}
Line 10: Line 11:
{{Related|mod_wsgi}}
{{Related|mod_wsgi}}
{{Related|Django}}
{{Related|Django}}
{{Related|IDE#Python IDEs}}
{{Related|List of applications/Utilities#Python IDEs}}
{{Related articles end}}
{{Related articles end}}
From [https://docs.python.org/3/faq/general.html#what-is-python What is Python?]:
From [https://docs.python.org/3/faq/general.html#what-is-python What is Python?]:


Line 18: Line 20:
== Installation ==
== Installation ==


[[Install]] the {{Pkg|python}} package, which provides the current release of Python 3.  
[[Install]] the {{Pkg|python}} package.  


=== Other versions ===
=== Other versions ===
Line 24: Line 26:
Previous and future versions of Python are available via the [[AUR]], and may be useful for old applications that do not run on the current version, for programs intended to run on other versions, or just for curiosity:
Previous and future versions of Python are available via the [[AUR]], and may be useful for old applications that do not run on the current version, for programs intended to run on other versions, or just for curiosity:


* Python 3.11: {{AUR|python311}}  
* Python 3.13: {{AUR|python313}} pre-release
* Python 3.10: Current release, see previous section
* Python 3.12: {{AUR|python312}}
* Python 3.10: {{AUR|python310}}
* Python 3.9: {{AUR|python39}}
* Python 3.9: {{AUR|python39}}
* Python 3.8: {{AUR|python38}}
* Python 3.8: {{AUR|python38}}
* Python 3.7: {{AUR|python37}}
* Python 3.7: {{AUR|python37}} ([https://peps.python.org/pep-0537/ unmaintained])
* Python 3.6: {{AUR|python36}} (unmaintained)
* Python 3.6: {{AUR|python36}} ([https://peps.python.org/pep-0494/ unmaintained])
* Python 2.7: {{AUR|python2}} ([https://www.python.org/doc/sunset-python-2/ unmaintained])
* Python 2.7: {{AUR|python2}} ([https://www.python.org/doc/sunset-python-2/ unmaintained])


{{Warning|Python versions before 3.7 have reached end of life and are unmaintained. See [https://devguide.python.org/versions/ Status of Python versions].}}
{{Warning|Python versions before 3.8 have reached end of life and are unmaintained. See [https://devguide.python.org/versions/ Status of Python versions].}}


Each of these packages installs a distinct binary named after the version number, e.g. ''python3.7'' for Python 3.7, allowing multiple versions to coexist on a system. You can also use {{Pkg|pyenv}} to easily switch between multiple versions of Python.
Each of these packages installs a distinct binary named after the version number, e.g. ''python3.8'' for Python 3.8, allowing multiple versions to coexist on a system. You can also use {{Pkg|pyenv}} to easily install and switch between multiple versions of Python.


Extra modules/libraries for old versions of Python may be found on the AUR by searching for {{ic|python<''version without period''>}}, e.g. searching for {{ic|python37}} for Python 3.7 modules.
Extra modules/libraries for old versions of Python may be found on the AUR by searching for {{ic|python<''version without period''>}}, e.g. searching for {{ic|python38}} for Python 3.8 modules.


You can also download the source for any release on the https://www.python.org/downloads/ page.
You can also download the source for any release on the https://www.python.org/downloads/ page.
Line 63: Line 66:


== Package management ==
== Package management ==
{{Out of date|pip has been disabled on Arch Linux, the tool throws an error that python libraries are "externally managed".}}
{{Accuracy|Listed unofficial methods of installing python packages, python packages are only officially supported through install via pacman, not via other tools.}}


There are several ways to install Python packages on Arch Linux:
There are several ways to install Python packages on Arch Linux:
Line 68: Line 75:
* [[Official repositories]] and [[AUR]] — A large number of popular packages are available in the Arch repositories. This is [[System maintenance#Use the package manager to install software|the preferred way]] to install system-wide packages.
* [[Official repositories]] and [[AUR]] — A large number of popular packages are available in the Arch repositories. This is [[System maintenance#Use the package manager to install software|the preferred way]] to install system-wide packages.
* {{App|{{man|1|pip}}|The official package installer for Python. You can use pip to install packages from the [https://pypi.org/ Python Package Index] and other indexes.|https://pip.pypa.io/|{{Pkg|python-pip}}}}
* {{App|{{man|1|pip}}|The official package installer for Python. You can use pip to install packages from the [https://pypi.org/ Python Package Index] and other indexes.|https://pip.pypa.io/|{{Pkg|python-pip}}}}
* {{App|pipx|Closely related to pip, but creates, for the user running it, an isolated environment for each application and its associated packages, preventing conflicts with system packages. Focused on packages that can be run from the command line directly as applications. You can use pipx to install packages from the [https://pypi.org/ Python Package Index] and other indexes.|https://pypa.github.io/pipx/|{{Pkg|python-pipx}}}}
* {{App|pipx|Closely related to pip, but creates, for the user running it, an isolated environment for each application and its associated packages, preventing conflicts with system packages. Focused on packages that can be run from the command line directly as applications. You can use pipx to install packages from the [https://pypi.org/ Python Package Index] and other indexes.|https://pipx.pypa.io/stable/|{{Pkg|python-pipx}}}}
* {{App|Poetry|Python dependency management and packaging made easy. It manages virtual environments, building and distribution of packages from the [https://pypi.org/ Python Package Index], and so it is a single command alternative to pip plus other separate tools.|https://python-poetry.org/docs/|{{Pkg|python-poetry}}}}
* {{App|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 [https://repo.anaconda.com/ Anaconda repositories].|https://docs.conda.io/projects/conda/|{{Aur|anaconda}}}}
* {{App|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 [https://repo.anaconda.com/ Anaconda repositories].|https://docs.conda.io/projects/conda/|{{Aur|anaconda}}}}
* {{App|Miniconda|A lightweight alternative to Anaconda which installs the package manager but does not install scientific computing packages by default.|https://docs.conda.io/en/latest/miniconda.html|{{Aur|miniconda3}}}}
* {{App|Miniconda|A lightweight alternative to Anaconda which installs the package manager but does not install scientific computing packages by default.|https://docs.conda.io/en/latest/miniconda.html|{{Aur|miniconda3}}}}


When installing packages using ''pip'', it is recommended to use a [[#Virtual environment|virtual environment]] to prevent conflicts with system packages in {{ic|/usr}}. Alternatively, {{ic|pip install --user}} can be used to install packages into the [https://pip.pypa.io/en/latest/user_guide/#user-installs user scheme] instead of {{ic|/usr}}. ''pipx'' and Conda integrate environment management into their workflows.
When installing packages using ''pip'', it is recommended to use a [[#Virtual environment|virtual environment]] to prevent conflicts with system packages in {{ic|/usr}}. Alternatively, {{ic|pip install --user}} can be used to install packages into the [https://pip.pypa.io/en/latest/user_guide/#user-installs user scheme] instead of {{ic|/usr}}. Other tools including ''pipx'', ''poetry'' and Conda integrate environment management into their workflows.


See the [https://packaging.python.org/ Python Packaging User Guide] for the official best practices for package management.
See the [https://packaging.python.org/ Python Packaging User Guide] for the official best practices for package management.


Historically, ''easy_install'' (part of {{Pkg|python-setuptools}}) was used to install packages distributed as [https://packaging.python.org/glossary/#term-egg Eggs]. ''easy_install'' and Eggs have been replaced with ''pip'' and [https://packaging.python.org/glossary/#term-wheel Wheels]. See [https://packaging.python.org/pip_easy_install/#pip-vs-easy-install pip vs easy_install] and [https://packaging.python.org/discussions/wheel-vs-egg Wheel vs Egg] for more information.
Historically, ''easy_install'' (part of {{Pkg|python-setuptools}}) was used to install packages distributed as [https://packaging.python.org/glossary/#term-egg Eggs]. ''easy_install'' and Eggs have been replaced with ''pip'' and [https://packaging.python.org/glossary/#term-wheel Wheels]. See [https://packaging.python.org/en/latest/discussions/pip-vs-easy-install/ pip vs easy_install] and [https://packaging.python.org/discussions/wheel-vs-egg 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]].}}
{{Note|There are also tools integrating ''pip'' with ''pacman'' by automatically generating PKGBUILDs for specified PyPI packages: see [[Creating packages#PKGBUILD generators]].}}
Line 88: Line 96:
* {{App|Tkinter|The standard Python interface to the [https://www.tcl.tk/ Tk] GUI toolkit.|https://docs.python.org/3/library/tkinter.html|{{Pkg|python}}}}
* {{App|Tkinter|The standard Python interface to the [https://www.tcl.tk/ Tk] GUI toolkit.|https://docs.python.org/3/library/tkinter.html|{{Pkg|python}}}}
* {{App|Qt for Python (PySide2)|The official Python bindings for [[Qt]]5.|https://www.qt.io/qt-for-python|{{Pkg|pyside2}}, {{Pkg|pyside2-tools}}}}
* {{App|Qt for Python (PySide2)|The official Python bindings for [[Qt]]5.|https://www.qt.io/qt-for-python|{{Pkg|pyside2}}, {{Pkg|pyside2-tools}}}}
* {{App|Qt for Python (PySide6)|The official Python bindings for [[Qt]]6.|https://www.qt.io/qt-for-python|{{Pkg|pyside6}}, {{AUR|pyside6-tools}}}}
* {{App|Qt for Python (PySide6)|The official Python bindings for [[Qt]]6.|https://www.qt.io/qt-for-python|{{Pkg|pyside6}}, {{Pkg|pyside6-tools}}}}
* {{App|pyQt|A set of Python bindings for Qt.|https://riverbankcomputing.com/software/pyqt/intro|{{Pkg|python-pyqt5}}}}
* {{App|pyQt|A set of Python bindings for Qt.|https://riverbankcomputing.com/software/pyqt/intro|{{Pkg|python-pyqt5}}, {{Pkg|python-pyqt6}}}}
* {{App|PyGObject|Python bindings for GObject based libraries such as [[GTK]], [[GStreamer]], WebKitGTK, GLib, and GIO.|https://pygobject.readthedocs.io/|{{Pkg|python-gobject}}}}
* {{App|PyGObject|Python bindings for GObject based libraries such as [[GTK]], [[GStreamer]], WebKitGTK, GLib, and GIO.|https://pygobject.readthedocs.io/|{{Pkg|python-gobject}}}}
* {{App|wxPython|A cross-platform GUI toolkit for Python which wraps [https://www.wxwidgets.org/ wxWidgets].|https://wxpython.org/|{{Pkg|python-wxpython}}}}
* {{App|wxPython|A cross-platform GUI toolkit for Python which wraps [https://www.wxwidgets.org/ wxWidgets].|https://wxpython.org/|{{Pkg|python-wxpython}}}}
Line 106: Line 114:


[https://docs.python.org/3/tutorial/interactive.html 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 {{Pkg|python-jedi}} for a richer tab completion experience [https://jedi.readthedocs.io/en/latest/docs/usage.html#tab-completion-in-the-python-shell].
[https://docs.python.org/3/tutorial/interactive.html 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 {{Pkg|python-jedi}} for a richer tab completion experience [https://jedi.readthedocs.io/en/latest/docs/usage.html#tab-completion-in-the-python-shell].
== Troubleshooting ==
=== Module not found after Python version update ===
A Python-based application might output {{ic|No module named ''module_name''}} for an installed dependency named {{ic|''module_name''}} after having upgraded the {{Pkg|python}} package to a new minor version (e.g. from version 3.10 to 3.11).
The above scenario happens when a dependency is not available for that Python version or not installed at all. Python packages are installed in a versioned site-packages directory ({{ic|/usr/lib/python''X.Y''/site-packages}} if system-wide, or {{ic|~/.local/lib/python''X.Y''/site-packages/}} if per-user, where {{ic|''X.Y''}} is a version like "3.11"). So whenever there is a new minor version upgrade, the Python-based package built with previous Python version must be rebuilt against the new one in order to be properly used.
Please notice it is the user's responsibility to rebuild non-official packages, including Python-based packages installed from AUR. See [[AUR#Updating packages]] and [[FAQ#What if I run a full system upgrade and there will be an update for a shared library, but not for the applications that depend on it?]]


== See also ==
== See also ==
Line 112: Line 130:
=== Official ===
=== Official ===


* [https://docs.python.org/ Official Python documentation]
* [https://docs.python.org/ Official Python documentation] (Can be installed with the {{Pkg|python-docs}} package for offline access.)
* [https://docs.python.org/3/tutorial/index.html Official Python tutorial]
* [https://docs.python.org/3/tutorial/index.html Official Python tutorial]


Line 122: Line 140:
* [http://inventwithpython.com/cracking/ Cracking Codes With Python] - Free online book
* [http://inventwithpython.com/cracking/ Cracking Codes With Python] - Free online book
* [https://stephensugden.com/crash_into_python/ Crash into Python] - Free tutorial
* [https://stephensugden.com/crash_into_python/ Crash into Python] - Free tutorial
* [https://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/ Debugging in Python] - Guide to using {{ic|pdb}}, the Python debugger
* [https://realpython.com/python-debugging-pdb/ Python Debugging With Pdb] - Guide to using {{ic|pdb}}, the Python debugger
* [https://diveintopython3.net/ Dive Into Python] - Creative Commons book
* [https://diveintopython3.net/ Dive Into Python] - Creative Commons book
* [https://www.oreilly.com/library/view/fluent-python-2nd/9781492056348/ Fluent Python] - Commercial book
* [https://www.oreilly.com/library/view/fluent-python-2nd/9781492056348/ Fluent Python] - Commercial book

Latest revision as of 09:39, 17 April 2024

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

Install the python package.

Other versions

Previous and future versions of Python are available via the AUR, and may be useful for old applications that do not run on the current version, for programs intended to run on other versions, or just for curiosity:

Warning: Python versions before 3.8 have reached end of life and are unmaintained. See Status of Python versions.

Each of these packages installs a distinct binary named after the version number, e.g. python3.8 for Python 3.8, allowing multiple versions to coexist on a system. You can also use pyenv to easily install and switch between multiple versions of Python.

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 python38 for Python 3.8 modules.

You can also download the source for any release on the https://www.python.org/downloads/ page.

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:

  • PyPy — A Python implementation written in Python. It has speed and memory usage advantages compared to CPython.
https://www.pypy.org || pypy, pypy3
  • Jython — An implementation of the Python language written in Java. It can be used to embed Python scripting into Java programs or use Java libraries in Python programs.
https://www.jython.org/ || jython
  • 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
  • IronPython — An implementation of the Python programming language which is tightly integrated with .NET. It can use .NET libraries and allows .NET programs to use Python libraries.
https://ironpython.net || ironpython-gitAUR

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 python command. The following shells are also available:

  • bpython — A fancy interface for the Python interpreter.
https://bpython-interpreter.org/ || bpython
  • IPython — A powerful interactive Python shell.
https://ipython.org/ || ipython
  • Jupyter — A web-based computation application powered by IPython.
https://jupyter.org/ || jupyterlab, jupyter-notebook
https://github.com/prompt-toolkit/ptpython || ptpythonAUR

Package management

This article or section is out of date.

Reason: pip has been disabled on Arch Linux, the tool throws an error that python libraries are "externally managed". (Discuss in Talk:Python)

The factual accuracy of this article or section is disputed.

Reason: Listed unofficial methods of installing python packages, python packages are only officially supported through install via pacman, not via other tools. (Discuss in Talk:Python)

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

https://pip.pypa.io/ || python-pip
  • pipx — Closely related to pip, but creates, for the user running it, an isolated environment for each application and its associated packages, preventing conflicts with system packages. Focused on packages that can be run from the command line directly as applications. You can use pipx to install packages from the Python Package Index and other indexes.
https://pipx.pypa.io/stable/ || python-pipx
  • Poetry — Python dependency management and packaging made easy. It manages virtual environments, building and distribution of packages from the Python Package Index, and so it is a single command alternative to pip plus other separate tools.
https://python-poetry.org/docs/ || python-poetry
  • 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.
https://docs.conda.io/projects/conda/ || anacondaAUR
  • Miniconda — A lightweight alternative to Anaconda which installs the package manager but does not install scientific computing packages by default.
https://docs.conda.io/en/latest/miniconda.html || miniconda3AUR

When installing packages using pip, it is recommended to use a virtual environment 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. Other tools including pipx, poetry 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 python-setuptools) 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.
Tip: pipenv provides a single CLI for Pipfile, pip and virtualenv. It is available as python-pipenv.

Widget bindings

The following widget toolkit bindings are available:

  • Tkinter — The standard Python interface to the Tk GUI toolkit.
https://docs.python.org/3/library/tkinter.html || python
  • Qt for Python (PySide2) — The official Python bindings for Qt5.
https://www.qt.io/qt-for-python || pyside2, pyside2-tools
  • Qt for Python (PySide6) — The official Python bindings for Qt6.
https://www.qt.io/qt-for-python || pyside6, pyside6-tools
  • pyQt — A set of Python bindings for Qt.
https://riverbankcomputing.com/software/pyqt/intro || python-pyqt5, python-pyqt6
  • PyGObject — Python bindings for GObject based libraries such as GTK, GStreamer, WebKitGTK, GLib, and GIO.
https://pygobject.readthedocs.io/ || python-gobject
  • wxPython — A cross-platform GUI toolkit for Python which wraps wxWidgets.
https://wxpython.org/ || python-wxpython

To use these with Python, you may also need to install the associated widget toolkit packages (e.g. tk 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 python-jedi for a richer tab completion experience [1].

Troubleshooting

Module not found after Python version update

A Python-based application might output No module named module_name for an installed dependency named module_name after having upgraded the python package to a new minor version (e.g. from version 3.10 to 3.11).

The above scenario happens when a dependency is not available for that Python version or not installed at all. Python packages are installed in a versioned site-packages directory (/usr/lib/pythonX.Y/site-packages if system-wide, or ~/.local/lib/pythonX.Y/site-packages/ if per-user, where X.Y is a version like "3.11"). So whenever there is a new minor version upgrade, the Python-based package built with previous Python version must be rebuilt against the new one in order to be properly used.

Please notice it is the user's responsibility to rebuild non-official packages, including Python-based packages installed from AUR. See AUR#Updating packages and FAQ#What if I run a full system upgrade and there will be an update for a shared library, but not for the applications that depend on it?

See also

Official

Third-Party