2.9.2. Installing the Python libraries

To install libraries in Python, you can use either locally linking library files to scripts, or installing packages within the Python virtual environment.

The installation process for libraries depends on the version you need and the method chosen:

The packages installed with pip will be placed in the directory /home/example/.local/lib/pythonX.X/site-packages, where example thisis title hosting account, and X.X — the Python version for which the package was installed.
  1. Connect to hosting via SSH.
  2. Install the required packages with the command:
    pip install --user package
    • Select pip of the required version, which can be done in several ways:
      • Set the default python version you need to Linux configurations and use the command:
        python -m pip install --user package
      • Instead pip substitute an alias for the appropriate pip version:
        • pip or pip3.6 — for Python version 3.6.
        • pip3.3 — for Python version 3.3.
        • pip2 — for Python version 2.7.
    • Instead package specify the name of the package you want to install. For example, the install command bcrypt for python 3.6 it will look like this:
      pip install --user bcrypt

Attention!

Use this method only when you need to create a virtual environment. For a typical library installation, use the standard installation with pip

A virtual environment in Python allows you to create a separate environment that has its own dependencies and packages. You can create virtual environments only in versions of Python 3 and higher, since the standard module is used venv... This is not possible for Python version 2. More details on working with the virtual environment are described in documentation.

Creating a virtual environment

  1. Connect to hosting via SSH.
  2. Change to the directory where you want to place the virtual environment:
    cd ~/example.com/subdomain/dir

    Instead example.com/subdomain/dir specify the desired path.

  3. Run the command to create a virtual environment in the current working directory:
    pythonХ -m venv --without-pip example

    Instead X specify the version of Python you want (for example 3 or 3.6), instead of example Is the name for the virtual environment.

After completing the specified actions, a directory with the name of the virtual environment will be created, in which all subsequent actions will be performed.

Activating the virtual environment

  1. Connect to hosting via SSH.
  2. Run the activation command:
    source /path/to/env/bin/activate

    Instead /path/to/env specify the path to the directory where the virtual environment was created.

If everything is done correctly, then the name of the virtual environment will be displayed at the command line prompt, for example (example) -bash-4.2$.

Disconnect from the virtual environment

Run the command:

deactivate

Installing packages in a virtual environment using pip

  1. Connect to hosting via SSH.
  2. Create and activate virtual environment for Python version 3.6.
  3. Install the required packages with the command:
    pip install package

    Instead package specify the name of the package you want to install. For example, the install command bcrypt will look like this:

    pip install bcrypt

Attention!

When using this method, packages are installed without the dependencies required for them to work. All dependencies will need to be determined and installed independently according to the instructions below.
  1. Connect to hosting via SSH.
  2. Create and activate virtual environment for Python of the required version.
  3. Create a temporary directory and change to it. For example, in the virtual environment directory, create a directory tempby running the commands:
    mkdir /path/to/env/temp
    cd !$

    Instead /path/to/env specify the path to the directory where the virtual environment was created.

  4. For further installation, the required package must be downloaded to this directory. The difference in downloading lies in the download method used. Please note, be sure to download only source-versions of packages. Download methods:
    • If you are using GitHub, you can download the package using the command:
      git clone https://github.com/user/package/

      For example, downloading requests will look like this:

      git clone git://github.com/psf/requests.git
    • If you are using a third-party repository, then to download the archive you need to run the command:
      wget https://example.com/package.zip

      After downloading, you will need to unpack the archive using the zip or tar utilities, or using filemanager... For example, downloading and unpacking requests will look like this:

      wget https://github.com/psf/requests/archive/master.zip
      unzip master.zip
    • You can manually download the required package files as an archive and upload them using filemanager or any FTPclient, then unpack archive.
  5. After downloading all the package files, you need to install it by running the command:
    cd package
    python setup.py install

    Instead package specify the name of the directory where the files of the required package are located. For example, setting requests looks like that:

    cd requests-master
    python setup.py install
Content