What is the Virtual Environment in Python and How to Set up Virtual Environment in Python



A bit of theory first...

If you are using Python, you will always need to use a virtual environment if you want to keep the packages you installed as organized and neat as possible. So basically virtual environment will make it possible to have different versions of the same library or framework for each project. Let's say you are a great Django developer (I believe you are :D), and for god knows whatever reason you want to have Django version 3.0.0 for one project and version 3.2 for another. So in this case, what you can do is you can create a separate virtual environment for each project and use different versions for packages. Another advantage of using a virtual environment for me is to have the ability to create a requirements.txt file that can be used in the project setup. Well, you can tell me that you can create it without a virtual environment but if you do so, requirements.txt file will contain all of the packages that have been installed globally on your PC. You can see the content by executing:

pip freeze

Enough of theory

We can now take a look at the practical part...

To move on further, the operating system should first be considered. I will cover the content for both Linux and Windows OS.

For Linux/Unix:

Well, the conventional way to create a virtual environment is:

python3 -m venv venv

You can change the last argument (venv) as it stands for the name of the virtual environment and you can name it whatever you want.

After you have created the virtual environment, you need to activate it before installing any package inside it. To activate it (assuming its name is venv):

source venv/bin/activate

and use the below snippet to deactivate it:

deactivate

To be honest, activation snippet has always seemed really long to me, as well as creating. But in Linux, you can give Alias to almost any command you like. Just go to the ~/.bashrc file (might be ~/.zshrc), and add alias-es for both commands, namely creating and activating:

# create alias
alias cvenv="python -m venv venv"
# activate alias
alias activate="source venv/bin/activate"

From now on, you can just do cvenv to create and activate to activate the virtual environment.

Goodbye Linux user, now it's time for Windows users (I know you might not like this part, but you can always drop a "I use Arch btw" comment in the comment section :D.

For Windows:

To create the virtual environment use the below command:

python -m venv venv

You can change the last argument (venv) as it stands for the name of the virtual environment and you can name it whatever you want.

To activate it:

venv\Scripts\activate

If commands look really long for you, you can use virtualenvwrapper-win package to simplify the process. To install it:

pip install virtualenvwrapper-win

You can now create and activate virtualenv as below:

# to create virtual environment
mkvirtualenv venv

# to activate
workon venv

If you use this specific tool, bear in mind that, virtual environment directory might not be stored in the same directory that you have executed the commands above.

You might have a question in your mind that, well, if it's stored in another directory, how I can manage them. Well, you can always list all the virtual environments you created so far by executing:

lsvirtalenv

and delete any of them by executing:

rmvirtualenv venv

venv above is the name of the virtual environment.

Creating requirements.txt File

You can always first see the content of requirements.txt by running:

pip freeze

To echo it into a file you can run:

pip freeze > requirements.txt

This will freeze out the names of all installed packages of your virtual environment into requirements.txt file with their exact version.

To install from requirements.txt you can execute the command below:

pip install -r requirements.txt

Thanks for reading!



Subscribe to get notified about new posts!

We'll never share your email with anyone else.