How to create Python virtual environments on Windows and Linux

Guide Feb 29, 2020

What's the point in using Virtual environments well the main benefits is that you can have the same package installed with different versions in the same computer/server.
That's helpful if you have a bunch of projects that have the same packages but works with different versions of them.
Also a venv can be in any folder you want, to use python env we need any python 3 version.

First of all we are going to separate the windows command from the linux commands using (W) for windows and (L) for Linux.

Now we need to make sure we have the virtualenv package install: pip3 install virtualenv

Creating the venv

Go to your terminal
Change dir to the folder of your env cd name_of_env
(W): python -m venv name_of_env
(L): virtualenv name_of_env

This will create venv in the selected folder
Now to access the venv or rather to activate it:

(W): execute name_of_env/Scripts/activate.bat
(L): source name_of_env/bin/activate

To make sure we are in the venv we can see the terminal that we have the name of the env at the beginning of the line or doing:

(W): where python
(L): which python

This will show where python is executing and it should we in our env.

Now if you execute pip list you will see that we don't have much installed in our environment but that was the point of all of this 😄

If you want the current packages that you have in your env you can execute pip freeze and if you are wild, you can do pip freeze > requirements.txt that will save the output of the terminal into that file so you can have the same version of the packages
To install the packages in other env or machine you can grab that file and do
pip install -r requirements.txt

It's not a good practice to put files in your venv, so when you use one you should make the folder of the project and then a dedicated venv folder, so the  virtual env will be in project_name/venv

Deactivating and removing the env

To Deactivate the venv
(W): execute deactivate
(L): deactivate

To remove the env we only need to delete the folder that the env it's in:

(W): rmdir folder_name /s
(L): rm -rf folder_name

Bonus creating anaconda env

Conda it's a bundle of frameworks and libraries that we can also use to create env

Creating a conda environment
conda create -n name_of_env python=3.6

As you can see one of the advantages that conda gives is the ability to choose the version of python

To activate the env:
conda activate name_of_env
To see all the env:
conda env list
To deactivate:
conda deactivate name_of_env
To remove a conda env:
conda env remove -n name_of_env

That's it! if you have question or found an error talk to me on twitter @ramgendeploy

Tags

Ramiro

Hey hi! My name is Ramiro, this is my blog about artificial intelligence, coding and things that are in my head. Follow me on twitter @ramgendeploy so we can chat!

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.