DEV Community

Cover image for Python3 - Virtual Environments
Richie Moluno
Richie Moluno

Posted on • Updated on

Python3 - Virtual Environments

In python, any interpreter you install by default works on the global environment which is not unique to each project. So, any project you start, for the packages needed, when installed or uninstalled they'll have an impact on the global environment.


Why Virtual environment

For example Project_A and Project_B requires a certain package to work, lets call this package PackageB.
At first it doesn't seem like a big deal, but lets say the two projects need a different version of the same package (PackageB), this is where the issue arises.

A good approach to solving this issue is introducing a Virtual environment. It solves this by isolating each project in a seperate environment. In this case, any package installed in the virtual environment are specific to that environment i.e they are only installed in that environment alone when activated.

As we proceed, i'll show you how to setup and activate a virtuanenv for python3


Install virtualenv for Python3

Here are some of the prerequisite for setting up a virtualenv

  • Python3
  • pip

1 To install Virtualenv with python3 run python3 -m pip -U install virtualenv.

Image description

2 After installing, we need to check the directory where python3 is installed on the system, to do this run where python3

Image description

In my case it's C:/Users/HP/AppData/Local/Microsoft/WindowsApps/python3.exe.


Create the Virtualenv

1 After getting the python3 path, the next step is to create the virtual environment. To do this i first create my working folder python-venv then navigate to this folder using cd python-venv.

python3 -m venv /path-to-new-virtual-environment

Image description

In the screenshot above, i used a relative path instead of the full path, for more on this refer to my previous article on Relative and absolute paths

2
If you navigate to your new venv folder you will notice a set of new folders (Lib, Include, Scripts), it shows that your venv was created successfully.

Image description

3 Now your virtual environment is set. To check for available commands and options for working with virtualenvs run

python -m venv -h this will display more like a reference summary of different command options available

vevn help reference


Activate the virtualenv

At the current state if we run pip install any package, it will still install them globally, which is not what we want. In order to install packages to our virtualenv we have to activate it.

To activate the virtualenc, navigate to the root directory of the virtualenv and run .\scripts\activate (note the slash direction).

Image description

If your environment is activated you’ll see (python-venv) before your path in your terminal prompt like in the image above. Now all packages installed will be in the new virtualenv.

To deactivate an active virtualenv, run deactivate


Exporting the virtualenv

Let's say you successfully finished a program that required you to install certain packages using pip, and you want your program to be available to others, a problem will arise since they'll need the exact packages and version. To fix this we have to export a list of packages used. To do this run pip freeze > requirements.txt. This command exports a list of the packages to the requirements.txt file which can later be installed by others using pip install -r requirements.txt

Top comments (0)