When working on different projects in Python it is essential to separate the different versions of Python packages so not to interfere with each other. This is where virtual environments comes in. They are isolated environments that can contain different versions of the same package.

Let’s get started

Open the Command Prompt as Administrator.

Make sure that you use the correct version of Python and pip when installing virtualenv. If you have multiple version of Python installed, it’s easiest to add the installation to the global enviroment path you want to use as baseline for the virtual environment.

Install package

Next install the Python package virtualenv in your base Python installation:

pip install virtualenv

Create virtual environment

I usually create a folder where I keep all my virtual environments and separate them from my code base. Navigate to this folder and create a virtual enviroment:

virtualenv <name of virtual environment>

If you want to use another Python installation you can use -p and the path to the Python.exe file.

virtualenv -p <path> <name of virtual environment>

This will create a folder with the same name as the name of the virtual environment.

Activate virtual environment

After the virtual environment has been created you can activate it:

<name of virtual environment>\Scripts\activate

With you environment activated you can start installing the packages you want for you project. The requirements.txt file contains all packages and versions you want installed.

pip install -r requirements.txt

Shutdown virtual environment

When you are done type:

deactivate

This will turn off the virtual environment inside your command prompt. By closing you command prompt, the virtual environment will shut down automatically so you don’t need to deactive it manually.

Conclusion

Now you have your virtual environments and you can start coding in Python without fearing that different projects will ruin each other by using incompatible version of installed packages.