Python Project Dependency Management

Project Dependency Management is the process of managing dependencies of a project which are available in the form of reusuable functions in libraries.

To maintain dependencies of a project in Python, you must have virtualenv and pip installed on your computer.

Verify if virtualenv is installed using the command below:

virtualenv --version

Verify if pip3 is installed using the command below:

pip3 --version

If they are installed then both of the above commands should return their versions respectively. Otherwise you can use this guide to install them.

Manage Project Dependencies

  1. Go inside the project folder.
        cd my_first_project
  3. Create a virtual environment inside the project folder. A virtual environment creates an isolated environment in which dependencies are installed for a project. To create a virtual environment, you must have the Virtual Environment tool installed on your computer. Use the following command to create a virtual environment for your project (Replace python3.8 with the version of Python that is installed on your computer):
        virtualenv env --python=python3.8

    A new env folder will get created inside the project folder. The env directory will contain files and folders such as bin and lib.

  5. Activate the virtual environment by executing the following command:
        source env/bin/activate

    If the virtual environment is activated successfully, you will see that the next line of the terminal starting with the (env).

    Whenever you need to install dependencies in your project, you can install them by using the pip command from within the virtualenv activated terminal. For example, the pip command below installs pure-Python MySQL client library .

        pip3 install PyMySQL
  7. When you move your project to some other system, you can install all the dependencies of that project by installing them from the requirements.txt file. Use the following command to generate requirements.txt file:
        pip3 freeze > requirements.txt

    Note: you must generate the requirements.txt file from inside the activated virtual environment terminal.

  9. Use the following command to install dependencies from the requirements.txt file:
        pip3 install -r requirements.txt