Python Virtual Environment

05 Nov 2020

python

1. Introduction

A Python virtual environment (venv) supports for managing your dependencies without affecting with default environment. This venv is based in your own site directories. The venv is a copy of an existing version Python. It is really useful when you develop a project. Helping you to manage your own site-packages, avoiding the changes which might cause nightmares, between the current version of the library (working on your project) and the latest version of the library in the future.

It’s too bad every package is installed in the default system of Python. The venv is part of the most essential work most Python developers use.

Contents

2. Requirements

3. Creating Virutal Environment

There are many ways to create a virtual environment, which include using virtualenv or venv or conda. If you’re using Python 2, you have to install the virtualenv with pip or use conda. In this article, I just mention to create venv via virtualenv and venv.

3.1 Virtualenv package

For Python 2, you have to install virtualenv to create vitural environment. Python 3 can also use this way.

In the terminal of Windows (I use powershell):

PS D:\my-project> pip install virtualenv

In the terminal of MacOS/ Linux:

For Python 3:

anthng@anthng:~/my-project$ pip3 install virtualenv

For Python 2:

anthng@anthng:~/my-project$ pip install virtualenv

Next, type the following:

virtualenv .envName

where .envName is the name of your virtual environment (you can change it).

3.2 Venv module

Since version 3.3, Python 3 has come with a built-in venv module. To use the module, in your terminal:

python3 -m venv .envName

where .envName is the name of your virtual environment (you can change it).

Type ‘python’ instead of ‘python3’ if you use Windows.

3.3 Active the virtual environment

Activate the environment by running the following command:

MacOS/Linux:

anthng@anthng:~/my-project$ source .envName/bin/activate
(.envName)anthng@anthng:~/my-project$ 

Windows:

PS D:\my-project> .envName/Scripts/activate
(.envName) PS D:\my-project>

The name of your venv will appear on the left side of your terminal when it is active ((.envName)in this case)

4. Deactive the virtual environment

To deactivate the venv, type deactivate

deactivate

5. Other Notes

In order to automatically save all packages to the text file that have been installed in the venv.

For Python 2 or Windows use below:

pip freeze > requirements.txt

For Python 3:

pip3 freeze > requirements.txt

In order to install packages from requirements.txt:

pip install -r requirements.txt

You should also add the venv to .gitignore file to exclude this folder if you use Git.

6. Conclusion

That’s all being needed to start a project with Python Virtual Environment. Lastly, I highly recommend creating a venv before starting any project, which will help you become more professional.