How to Manage Python Dependencies with Virtual Environments (2024)

Multiple Python projects, each with their own dependencies can be effectively managed using separate virtual environments. Pipenv, Venv, and Pyenv are popular dependency management tools. The ActiveState Platform is a modern solution to dependency management.Virtualenv has been deprecated in Python 3.8.

If you don’t have Python installed, you’ll need to create a global installation BEFORE you can create a virtual environment. To avoid this, install Python 3.9 from ActiveState, which will automatically install Python 3.9 into a virtual environment for you so you can start working on your project right away. Install Python 3.9

Managing Dependencies with Pipenv

Pipenv is a dependency manager that lets you create a separate virtual environment for each of your projects, and automatically manages the dependencies within each of them.

You can check to see if Pipenv is installed, with the following command:

$ pipenv –version

You can also check to see if Pipenv is up-to-date with the following command:

$ pip install –upgrade pipenv

Using Pip with Pipenv

Pip is integrated into Pipenv. Use pipenv to install the packages listed in the requirements.txt file for each of your projects. As a best practice, each project should be installed in a different virtual environment to avoid dependency conflicts that can arise when different projects require different versions of the same package.

  1. To create a Pipenv project using Python 3.8, cd to the directory where you would like to create the project, then enter:

$ pipenv –python 3.8

This will automatically create a virtual environment within for you, along with a Pipfile to track dependencies for maintenance, and a Pipfile.lock file that declares all dependencies and sub-dependencies required in the project.

  1. You can now install an individual package and its dependencies with Pipenv, by entering:

$ pipenv install <packagename>

NOTE: If you had not already created a Pipenv virtual environment when you installed Python 3.8, the pipenv install command will create one now and add a Pipfile and Pipfile.lock file.

Alternatively, you can provide a requirements.txt file when running pipenv install. In this case, Pipenv will automatically import the contents of the requirements file and create a Pipfile.

  1. To ensure all the packages have been installed, you can list them.

a. Cd into a Pipenv virtual environment and open a terminal or command window.

b.Run the following command to retrieve a list of python packages installed in this environment:

$ pipenv run pip list

Dependency Management with Pipfile.lock

Whenever a package is installed in a pipenv environment, pipfile.lock is automatically updated to reflect dependency and sub-dependency changes.

Depending on the circ*mstances, however, pipfile.lock may not update during package installation:

  1. Pipfile.lock may fail to update during a package installation. For example, when cloning a repository made on another machine with a different OS, pipfile.lock may not update. Pipfile.lock incompatibilities can occur because the Pipfile.lock is system-specific.

2. Large pipfile.lock files are very slow to update, and it may be practical to postpone the update. The pipenv install –skip-lock command option can be used to skip the pipfile.lock.

If for any reason the pipfile.lock is not updated, the pipenv lock command can be run to generate a new pipfile.lock file:

$ pipenv install <packagename>

# pipfile.lock will be updated.

$ pipenv install –skip-lock <packagename>

# pipfile.lock will not update.

$ pipfile lock

# a pipfile.lock will be generated.

Managing Dependencies with Venv

Venv is the successor to VirtualEnv, and functions in a similar manner with two important differences:

  • Venv is included in Python 3.3+ and does not have to be installed.
  • Venv is a lower level tool than Pipenv, and is especially useful if Pipenv does not meet your particular virtual environment needs.

If you are working with Python 3.8, you can create a Venv virtual environment by doing the following:

  1. cd into the directory where you would like to create your project
  2. Open a terminal or command window and enter:

$ python -m venv <project_directoryname>

If you are working with Python 3.7, you can create a Venv virtual environment by doing the following:

  1. cd into the directory where you would like to create the project
  2. Open a terminal or command window and enter:

$ python -m virtualenv venv <project_directoryname>

Managing Dependencies With Pyenv

Pyenv is a Python version manager that lets you do many tasks, including:

  • Change the global Python version
  • Install multiple Python versions
  • Set directory or project specific Python versions
  • Create and manage virtual environments.

NOTE:Pyenv is a Bash extension and will not work on Windows outside of the Windows subsystem for Linux.

Pyenv Installation on Linux

There are two main methods for installing Pyenv:

  • Pyenv installer.
  • Clone Pyenv from Github, and then configure it manually.

How to Install Pyenv

To install Pyenv on Linux using its installer, first ensure that your system has the necessary prerequisites, including Git, Curl and Bash.

  1. Open a terminal in Linux and enter:

$ curl https://pyenv.run | bash

pyenv.runredirects to an installation script athttps://github.com/pyenv/pyenv-installer

2.Restart the shell in order for path changes to take effect:

$ exec $SHELL

How to Clone Pyenv from Github

Pyenv can also be cloned fromhttps://github.com/pyenv/pyenv.gitand installed with Bash.

  1. Run thegit clonecommand from a location where you want Pyenv installed to.$HOME/.pyenvis recommended, but it can be installed elsewhere.

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

2. Define an environment variable calledPYENV_ROOTto point to the path where Pyenv is cloned to, and then add$PYENV_ROOT/binto your$PATHin order to access the Pyenv command-line utility.

3.Use Bash to Install Pyenv on most flavors of Linux:

$ echo ‘export PYENV_ROOT=”$HOME/.pyenv”‘ >> ~/.bash_profile

$ echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bash_profile

For Ubuntu desktops, use the following Bash commands:

$ echo ‘export PYENV_ROOT=”$HOME/.pyenv”‘ >> ~/.bashrc

$ echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bashrc

4. Addpyenv initto your shell in order to enable it. Puteval “$(pyenv init -)” toward the end of the shell configuration file, like so:

$ echo -e ‘if command -v pyenv 1>/dev/null 2>&1; then\n eval “$(pyenv init -)”\nfi’ >> ~/.bash_profile

Note:for Ubuntu and Fedora Linux modify the~/.bashrcfile instead of~/.bash_profile.

Caution: some systems have theBASH_ENVvariable configured to point to.bashrc. To avoid unpredictable behavior on these systems, you should puteval “$(pyenv init -)”into .bash_profile, and not into .bashrc.

5. Restart the shell in order for path changes to take effect:

$ exec “$SHELL”

Pyenv Usage

Pyenv commands will let you install and manage multiple Python versions in the same system.

To list all Python versions available for installation, enter:

$ pyenv install –list

Available versions:

2.1.3

2.2.3

2.3.7

3.8.1

To install python 3.8.1, enter:

$ pyenv install 3.8.1

To determine which version of Python you are currently using, enter:

$ pyenv version

To view all Python versions currently installed on the system and available for use, enter:

$ pyenv versions

To set the global version of Python to be used in all shells, enter:

$ python global<version#>

To set the local version of Python to be used for a specific project, overriding the global version, enter:

$ python local<version#>

To set the version of Python to be used in a specific instance of a shell, enter:

$ pyenv shell<version#>

Thepyenv shellcommand resets thePYENV_VERSIONenvironment variable in the shell. This will override the global and local versions.

Managing Dependencies With The ActiveState Platform

The ActiveState Platform is an all-in-one package management and risk management solution for open source languages:

Development and DevOps teams can improve the security and reduce the complexity of the Python, Perl and Tcl environments they’re using to build their applications.

Security & Compliance teams can reduce risk with better oversight and maintenance of all open source components.

How to Manage Python Dependencies with Virtual Environments (1)

You need only choose a language and the packages your project requires, and the ActiveState Platform will:

  • Pull in and resolve all dependencies, providing your with a complete software BoM, including:
  • Transitive dependencies (ie., dependencies of dependencies)
  • OS-level dependencies
  • Shared dependencies (ie., OpenSSL)
  • Environment dependency-checks flag any Common Vulnerabilities and Exposures (CVEs), providing you with severity level for each along with a link to the National Vulnerability Database (NVD) so you can read further details
  • Build everything from source code in parallel in just a few minutes
  • Package your environment for deployment on Windows, Linux or Mac

Ensure your development process is secure from the first line of code.

How to Manage Python Dependencies with Virtual Environments (2)

Unlike other dependency scanners, the ActiveState Platform will show you the implications of selecting a new version of a dependency on all the other components of your environment BEFORE you commit to it. Never break your environment again!

The ActiveState Platform currently supports the Python, Perl and Tcl ecosystems with more languages (such as Ruby, PHP and node.js) being added soon.

Ready to see for yourself? You can try the ActiveState Platform by signing up for a free account using your email or GitHub credentials. Or contact us for a free demo and let us show you how you can implement secure dependency management in your organization.

Dependency Management Resources

For more information about pip and dependency management, you can refer to:

How to Manage Python Dependencies with Virtual Environments (2024)

FAQs

Why should we use a virtual environment for managing a project's dependencies? ›

You won't accidentally install packages globally, which could lead to conflicts or difficulties in tracking dependencies. Virtual environments keep your system Python installation untouched, reducing the risk of breaking system-level packages.

How do you resolve dependency conflict in Python? ›

As a first step, it is useful to audit your project and remove any unnecessary or out of date requirements (e.g. from your setup.py or requirements. txt files). Removing these can significantly reduce the complexity of your dependency tree, thereby reducing opportunities for conflicts to occur.

What is the best way to manage dependencies in Python? ›

Install, use, and manage third-party Python packages with the “pip” package manager on Windows, macOS, and Linux. Isolate project dependencies with so-called virtual environments to avoid version conflicts in your Python projects.

Should you always use virtual environments for Python? ›

Always use a Virtual Environment

Always. Virtual environments let you have a stable, reproducible, and portable environment. You are in control of which packages versions are installed and when they are upgraded.

Should I use Conda or venv? ›

Choosing the right environment management tool depends on your needs. If you need a simple, easy-to-use tool, venv might be the best choice. If you're dealing with complex dependencies, Conda env is the way to go. If you need to switch between different Python versions, consider pyenv or virtualenv.

Does pip automatically install dependencies? ›

The pip install <package> command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs.

Where does Python store dependencies? ›

Dependencies in Python are managed with pip and expressed in a metadata file called requirements. txt . This file must be in the same directory as the main.py file that contains your function code.

How do I reduce the size of a Python dependency? ›

An easy way reduce the size of python packages is to build from source instead of use pre-compiled wheels. We can see the footprint has reduced by ~70% when using sdist instead of wheel. This 4 article provides more details about these CFLAG optimization when installing a package from source.

What is the difference between venv and virtualenv? ›

In summary, venv and virtualenv are similar in functionality but differ in implementation, where venv is a built-in module in Python 3 while virtualenv is a third-party tool. pyvenv is a deprecated tool that has been replaced by venv.

How to install all Python libraries at once? ›

If you need to install multiple packages at once in Python, then it is a simple task. All we have to do is create a file and write the names of all the packages there and save it in the form of “. txt”. Once the file is created open the command prompt and hit the install command followed by the file name.

How does Python dependency management work? ›

Basics of Python dependency management

Dependency management is documenting the required environment for your project and making it easy and deterministic for others to reproduce it. You could write installation instructions on a piece of paper. You could write them in your source code comments.

Why is poetry better than conda? ›

Poetry emerges as a modern and organized solution for Python dependency management, offering improved organization, version control, and flexibility compared to traditional tools like Pip and Conda.

Which of the following tool can be used to manage dependencies of a Python project? ›

Managing Python Dependencies – Alternative Solutions

Using venv and pipenv are two methods of managing dependencies in Python. They are simple to implement and, for most users, adequate solutions for handling multiple projects with different dependencies. However, they are not the only solutions.

Why is it important to use virtual environments? ›

Virtual environments are useful ways of ensuring that the correct package/library versions are consistently used every time the software runs. This helps isolate software projects from potentially conflicting libraries and packages that are installed on an operating system.

Why should I use a virtual environment? ›

virtualenv allows you to avoid installing Python packages globally by making an isolated python environment. That means it will install packages just in your desire project folder. After activating you can install your packages using pip.

Why would you use a virtual environment? ›

Python virtual environments give you the ability to isolate your Python development projects from your system installed Python and other Python environments. This gives you full control of your project and makes it easily reproducible.

Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6120

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.