2.9.2. Install Python libraries

To install libraries in Python, you can either use a local connection of library files to scripts or install packages within a Python virtual environment.

Packages installed using pip will be placed in the directory /home/example/.local/lib/pythonX.X/site-packages, where example is the hosting account name and X.X is the Python version for which the package was installed.
  1. Connect to the hosting via SSH.
  2. Install the necessary packages with the command:
    pip install --user package
    • Select the desired version of pip using any of the following methods:
      • Select the desired Python version in Linux configuration and execute the command:
        python -m pip install --user package
      • Instead of pip, substitute the alias for the desired version of pip:
        • pip or pip3.6 — Python 3.6.
        • pip3.3 — Python 3.3.
        • pip2 — Python 2.7.
    • Instead of package, specify the name of the package you want to install. Example command to install bcrypt for Python 3.6:
      pip install --user bcrypt

Attention!

Only use this method if you need to create a virtual environment. For normal library installation, use pip.

The virtual environment in Python allows you to create a separate environment with its own dependencies and packages. Virtual environments can only be created in Python 3 and above, where the standard venv module is available. This feature is not available in Python 2. For more details, see the official documentation.

Create virtual environment

  1. Connect to the hosting via SSH.
  2. Go to the directory where you want to place the virtual environment (use your own data in the command):
    cd ~/example.com/subdomain/dir
  3. Create a virtual environment in the current working directory (in the command, instead of X, specify the desired Python version, for example, 3 or 3.6, and instead of example, specify an arbitrary name for the virtual environment):
    pythonХ -m venv --without-pip example

As a result, a directory with the name of the virtual environment will be created, in which further actions will be performed.

Activate virtual environment

  1. Connect to the hosting via SSH.
  2. Activate the virtual environment (in the command, instead of /path/to/env, specify the path to the directory with the created virtual environment):
    source /path/to/env/bin/activate

If successful, the command line prompt should display the name of the virtual environment in the format (example) -bash-4.2$.

Deactivate virtual environment

Execute the command:

deactivate

Install packages in virtual environment using pip

  1. Connect to the hosting via SSH.
  2. Create and activate a virtual environment for Python 3.6.
  3. Install the necessary packages using the command (in the command, instead of package, specify the name of the package you want to install, for example, bcrypt):
    pip install package

Attention!

When using this method, packages are installed without dependencies. All dependencies will need to be identified and installed manually according to these instructions.
  1. Connect to the hosting via SSH.
  2. Create and activate a virtual environment for the required version of Python.
  3. Create a temporary directory and go to it (in the command, instead of /path/to/env, specify the path to the directory with the created virtual environment):
    mkdir /path/to/env/temp
    cd !$
  4. For further installation, download the required package to this directory. The difference between downloads lies in the method used to download them. Please note that you must only download source versions of packages. Download methods:
    • If you are using GitHub, you can download the package with the following command:
      git clone https://github.com/user/package/

      Example command for downloading the requests package:

      git clone git://github.com/psf/requests.git
    • If you are using a third-party repository, you can download the package with the following command:
      wget https://example.com/package.zip

      After downloading, you also need to extract the archive using the zip or tar utilities, or using the file manager. Example command for downloading and extracting the requests package:

      wget https://github.com/psf/requests/archive/master.zip
      unzip master.zip
    • You can manually download the package files as an archive and upload them using the file manager or any FTP client, then extract the archive.
  5. After downloading all the files in the package, you need to install it (in the command, instead of package, specify the name of the directory where the package files will be placed):
    cd package
    python setup.py install

    Example command for installing the requests package:

    cd requests-master
    python setup.py install
Content

    (1)