python basics -- installing Python modules

As a popular open source development project, Python has an active support community of contributors and users who also make their software available to other Python developers under the terms of the open source license.

This allows Python users to share and collaborate efficiently, benefiting from solutions to common (and sometimes rare!) problems that others have already created, as well as potentially contributing their own solutions to the common pool.

This guide covers the installation portion of the process. See the distribution guidelines for guidelines on creating and sharing your own Python projects.

For corporate and other institutional users, please be aware that many organizations have their own policies regarding the use and contribution of open source software. Consider these strategies when using the distribution and installation tools provided with Python.

1. Key terms

  • pipis the preferred installer. Starting with Python 3.4, it is included by default in the Python binary installer.
  • A virtual environment ( virtual environment) is a semi-isolated Python environment that allows packages to be installed for use by a specific application rather than being installed system-wide.
  • venvis the standard tool for creating virtual environments and has been part of Python since Python 3.3. Starting with Python 3.4, it will pipbe installed by default into all created virtual environments.
  • virtualenvYes venvthird-party alternatives (also predecessors). It allows the use of virtual environments on Python versions prior to 3.4, which either don't provide one at all venv, or don't automatically pipload into created environments.
  • The Python Package Index is a public repository of open source licensed packages, available to other Python users.
  • The Python Packaging Authority is an organization of developers and documentation authors responsible for maintaining and evolving standard packaging tools and associated metadata and file format standards. They maintain various tools, documentation, and issue trackers on GitHub and Bitbucket .
  • distutilsis the original build and distribution system first added to the Python standard library in 1998. While direct use of distutils is being phased out, it still forms the basis for the current packaging and distribution infrastructure, not only is it still part of the standard library, but its name exists in other ways (e.g. for coordinating Python packaging standard development the name of the mailing list).

Changed in version 3.5: Created virtual environments are now recommended venv.

Python Packaging User Guide: Creating and using virtual environments

2. Basic usage

The standard packaging tools are all designed to be used from the command line.

The following command will install the latest version of the module and its dependencies from the Python Package Index :

python -m pip install SomePackage

For POSIX users (including macOS and Linux users), the examples in this guide assume the use of virtual environments.
For Windows users, the examples in this guide assume that the option to adjust the system PATH environment variable was selected when installing Python.

Exact or minimal versions can also be specified directly on the command line . When using comparison operators such as >, <or other special characters, the package name and version should be enclosed in double quotes:

python -m pip install SomePackage==1.0.4    # specific version
python -m pip install "SomePackage>=1.0.4"  # minimum version

Normally, if a suitable module is already installed, trying to install it again will have no effect. Upgrading an existing module must be explicitly requested:

python -m pip install --upgrade SomePackage

More information and resources about pip and its capabilities can be found in the Python Packaging User Guide .

The creation of virtual environments venvis done through modules. Install the package into the active virtual environment using the command shown above.
Python Packaging User Guide: Installing Python Distribution Packages

3. How to do it

These are quick answers or links to some common tasks.

Installed in versions of Python prior to Python 3.4 pip?

Python only started being bundled in Python 3.4 pip. For earlier versions, pipit needs to be "bootstrapped" as described in the Python Packaging User Guide.

Python Packaging User Guide: Requirements for Installing Packages

Install packages for the current user only?

Passing --userthe option python -m pip installto will install the package for the current user only, not for all users of the system.

Install the scientific Python package?

Many scientific Python packages have complex binary dependencies and are not currently easy to install directly pip. At this point, it is often easier for users to install these packages by other means than to try to install them using pip.

Python Packaging User Guide: Installing Scientific Packages

Install multiple versions of Python side-by-side?

On Linux, macOS, and other POSIX systems, use versioned Python commands combined with -mswitches to run corresponding pipcopies of

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4

You can also use the appropriate version of pipthe command.
On Windows, use the Python launcher with -mthe switch :py

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4

4. Common installation problems

4.1 Install system Python on Linux

On Linux systems, the Python installation is usually included as part of the distribution. Installing to this Python installation requires root access to the system, which pipmay interfere with the operation of the system package manager and other components of the system if accidentally upgraded components are used.

On such systems, pipwhen using installation packages, it is best to use a virtual environment or install one per user.

4.2 pip is not installed

pip may not be installed by default. A possible solution is:

python -m ensurepip --default-pip

There are other resources for installing pip as well.

4.3 Installing binary extensions

Python generally relies heavily on source-based distributions , and end users are expected to compile extension modules from source as part of the installation process.

With wheelthe introduction of support for the binary format, and the ability to ship via the Python Package Index at least on Windows and macOS wheel, this problem will hopefully decrease over time as users will more often be able to install pre-built extensions rather than Need to build them yourself.

Some solutions for installing scientific softwarewheel that are not yet provided as prebuilt files can also help you get other binary extensions without building them locally.

Python Packaging User Guide: Binary Extensions

Guess you like

Origin blog.csdn.net/chinusyan/article/details/132421229