Installing Python modules¶
As a popular open source development project, Python has an active supporting community of contributors and users that also make their software available for other Python developers to use under open-source license terms.
Це дозволяє користувачам Python ефективно ділитися та співпрацювати, отримуючи вигоду від рішень, які інші вже створили для поширених (а іноді навіть рідкісних!) проблем, а також потенційно вносячи власні рішення в загальний пул.
This guide covers the installation part of the process. For a guide to creating and sharing your own Python projects, refer to the Python packaging user guide.
Примітка
Для корпоративних та інших інституційних користувачів пам’ятайте, що багато організацій мають власні політики щодо використання програмного забезпечення з відкритим вихідним кодом і надання допомоги в ньому. Будь ласка, візьміть до уваги ці правила під час використання інструментів розповсюдження та встановлення, які надаються разом з Python.
Ключові терміни¶
pip is the preferred installer program. It is included by default with the Python binary installers.
Віртуальне середовище — це напівізольоване середовище Python, яке дозволяє встановлювати пакети для використання певною програмою, а не встановлювати в системі.
venvis the standard tool for creating virtual environments. It defaults to installing pip into all created virtual environments.virtualenvis a third-party alternative (and predecessor) tovenv.The Python Package Index (PyPI) is a public repository of open source licensed packages made available for use by other Python users.
The Python Packaging Authority is the group of developers and documentation authors responsible for the maintenance and evolution of the standard packaging tools and the associated metadata and file format standards. They maintain a variety of tools, documentation, and issue trackers on GitHub.
Змінено в версії 3.5: Тепер для створення віртуальних середовищ рекомендується використовувати venv.
Базове використання¶
Усі стандартні інструменти пакування призначені для використання з командного рядка.
The following command will install the latest version of a module and its dependencies from PyPI:
python -m pip install SomePackage
Примітка
Для користувачів POSIX (включаючи користувачів macOS і Linux) приклади в цьому посібнику передбачають використання virtual environment.
Для користувачів Windows приклади в цьому посібнику припускають, що під час інсталяції Python було вибрано параметр налаштування системної змінної середовища PATH.
Також можна вказати точну або мінімальну версію безпосередньо в командному рядку. У разі використання таких операторів порівняння, як >, < або будь-який інший спеціальний символ, який інтерпретується командною оболонкою, назву пакета та версію слід брати в подвійні лапки:
python -m pip install SomePackage==1.0.4 # specific version
python -m pip install "SomePackage>=1.0.4" # minimum version
Зазвичай, якщо відповідний модуль уже встановлено, повторна спроба встановити його не матиме ефекту. Оновлення існуючих модулів має надаватися в явному вигляді:
python -m pip install --upgrade SomePackage
More information and resources regarding pip and its capabilities can be found in the Python Packaging User Guide.
Створення віртуальних середовищ здійснюється за допомогою модуля venv. Для встановлення пакетів у активне віртуальне середовище використовуються команди, наведені вище.
Як мені …?¶
Це короткі відповіді або посилання для деяких типових завдань.
… встановити пакети лише для поточного користувача?¶
Передача параметра --user до python -m pip install встановить пакет лише для поточного користувача, а не для всіх користувачів системи.
… встановити наукові пакети Python?¶
A number of scientific Python packages have complex binary dependencies, and aren’t currently easy to install using pip directly. It will often be easier for users to install these packages by other means rather than attempting to install them with pip.
… працювати з кількома версіями Python, встановленими паралельно?¶
On Linux, macOS, and other POSIX systems, use the versioned Python commands
in combination with the -m switch to run the appropriate copy of
pip:
python3 -m pip install SomePackage # default Python 3
python3.14 -m pip install SomePackage # specifically Python 3.14
Appropriately versioned pip commands may also be available.
On Windows, use the py Python launcher in combination with the -m
switch:
py -3 -m pip install SomePackage # default Python 3
py -3.14 -m pip install SomePackage # specifically Python 3.14
Поширені проблеми встановлення¶
Встановлення в систему Python на Linux¶
On Linux systems, a Python installation will typically be included as part of the distribution. Installing into this Python installation requires root access to the system, and may interfere with the operation of the system package manager and other components of the system if a component is unexpectedly upgraded using pip.
On such systems, it is often better to use a virtual environment or a per-user installation when installing packages with pip.
Pip не встановлено¶
It is possible that pip does not get installed by default. One potential fix is:
python -m ensurepip --default-pip
There are also additional resources for installing pip.
Встановлення бінарних розширень¶
Python once relied heavily on source-based distribution, with end users being expected to compile extension modules from source as part of the installation process.
With the introduction of the binary wheel format, and the ability to publish wheels through PyPI, this problem is diminishing, as users are more regularly able to install pre-built extensions rather than needing to build them themselves.
Some of the solutions for installing scientific software that are not yet available as pre-built wheel files may also help with obtaining other binary extensions without needing to build them locally.