virtual environment

Introduction to Python Virtual Environments: The Hows and Whys

70 VIEWS

A Python virtual environment is a package that makes it possible to have several virtual installations of Python with their own packages. Programs running in one virtual environment do not interfere with the main installation of Python and other installations of Python. The ability to have virtual installations of Python with their own packages makes virtual environments similar to containers; however, unlike containers, virtual environments do not make it possible to assign specific resources to different environments.

Why you should use virtual environments

The ability to have several installations of virtual environments presents the user with certain advantages, such as:

Keeping track of the packages required for a particular project
Having a single installation of Python means that the packages for each of your projects will be installed in the same location. The ability to have a clean installation where project-specific packages can be found makes it easy to keep track of those packages, and makes it easy for several developers to work on the same project. For example, the packages in a virtual environment can be stored in a file using the following command:

This command will save all the packages in your virtual environment to the file named requirements.txt in the directory where the command was run. The requirements file can then be used to install the project-specific packages in other virtual environments, thereby simplifying the process of collaborating on the same project. Note that this command should be run after the virtual environment has been activated, otherwise pip will freeze the packages in your main installation of Python. (Activating virtual environments and installing packages from the requirements file will be covered under the How to use virtual environments section.

Working with packages that conflict with each other

Once in a while, you will come across the awkward situation where some Python packages cannot be installed side-by-side because they conflict. The conflict may arise because some packages depend on different versions of the same package. This can be easily resolved by installing the conflicting packages in different virtual environments, provided they are not needed for the same project.
Using different versions of the same package
If for whatever reason you need to use different versions of the same package on different projects, virtual environments will allow you to have both packages on the same system without hassle. Just install the required package in the virtual environment of your respective projects. Make sure you activate the environment pertaining to your desired project before you start working on it, and you’re good to go.

How to use virtual environments

To use virtual environments, the virtual environment package must first be installed using the command:

Next, navigate to your project directory and create a virtual environment by typing

This will create a new virtual environment in the directory myvenv within your project directory. You can specify the version of Python you want to use in the virtual environment with

Activate the virtual environment while in your project directory by typing

For Unix based systems. On windows, type

After activating the virtual environment, its name will be seen in the command line prompt, like this:

After activating the virtual environment, calling Python in the shell will run Python in the virtual environment. All packages imported in Python programs will also be imported from the virtual environment. For example, typing:

Will run myfile.py in the virtual environment and import all packages imported in myfile.py from the virtual environment. You can also install packages in your virtual environment with pip simply by typing:

You can also install packages from a requirements.txt file generated with pip freeze by navigating to the directory with the requirements.txt file and typing the following command:

Finally a virtual environment can be deactivated by typing

Conclusion

Even though using a single installation of Python is usually sufficient, it is advisable to keep packages for different projects separate, especially for the sake of collaboration. The benefits of using virtual environments typically outweigh the overhead of learning to use them and setting them up.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Stephan is a third-year student at Ashesi pursuing an undergraduate degree in computer engineering. Stephan is passionate about technology and is focused on continuous learning to gain new skills. He has worked in software development, and has worked on IoT projects and designing products for the health sector.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu