Sitemap
Dipesh Basnet

Dipesh Basnet

Python Virtual Environment | How to?

2 min readJan 20, 2023
Press enter or click to view image in full size
Isolated python environments in different projects, a virtual environment use case

A Python virtual environment is a tool used to create isolated Python environments. These environments allow for separate dependencies and package versions for different projects without interfering with the machine’s global Python installation or other running projects.

Creating a virtual environment is easy using the venv module that is included in the Python standard library. Here is an example of how to create a virtual environment named “myvenv” in a directory called “myproject”:

python -m venv myproject/myvenv

After a virtual environment has been created, it can be activated by executing the activate script, which can be found in the virtual environment's bin directory. The following command will enable the virtual environment on Windows:

myproject\myvenv\Scripts\activate

On Linux or macOS:

source myproject/myvenv/bin/activate

Any pip-installed packages will be added to the virtual environment rather than the system-wide Python installation when the virtual environment is active. You can install packages using pip as usual:

pip install django

Additionally, you can manage your project’s dependencies in the requirements.txt file, which makes it simple to replicate your environment on other computers.

Get Dipesh Basnet’s stories in your inbox

Join Medium for free to get updates from this writer.

There is no limit to the number of virtual environments you can make, each with its own set of dependencies and packages. You can use the deactivate command to deactivate the virtual environment:

deactivate

By providing the path of the Python interpreter, it is also possible to create a virtual environment with a specific Python version. For example:

python3.9 -m venv myproject/myvenv

This will create a virtual environment with python 3.9 version.

For Python development, using virtual environments is regarded as best practice because they enable isolated development environments and prevent conflicts between various package versions. Additionally, it facilitates the management of project-specific dependencies and the replication of development environments on different computers. To make sure that the application has the precise version of the packages it needs to run and to prevent conflicts with other applications running on the same server, it’s also crucial to use virtual environments when deploying Python applications in production.

Tools like virtualenv, virtualenvwrapper, and pipenv, which offer more functionality and usability, can also be used to manage and create virtual environments. For instance, pipenv is a tool that manages virtual environments and dependencies in a more streamlined manner. virtualenvwrapper offers a set of commands for setting up and managing virtual environments.

In conclusion, Python virtual environments are an effective and essential tool for managing package versions and dependencies in Python development. It enables programmers to produce isolated, self-contained environments that are simple to duplicate on other computers. It ensures that the application runs with the correct dependencies and package versions.

--

--