Python Virtualenv

A Virtual Environment in Python helps you to manage different versions of Python for different projects without doing a mess with your default system dependencies. It allows you to manage separate environments for different packages for different projects. This is one of the most important tools that most Python developers use

Why Would You Use A Virtual Environment Python?

Let’s say you’re working on a project which uses Python3.10 and your default system interpreter is Python3.8 and uses the 1.0 version of that dependency then you need to have Python3.10 installed in order to work on that project.
To get rid of these painful experiences, virtualenv helps you to isolate your different Python environments, dependencies, etc and you can create as many virtual environments as you want. While developing Python applications it is recommended to use a virtual environment to preserve conflicts.

Creating A Virtual Environment

Step 1: Install the virtualenv package.

pip3 install virtualenv

Now check your installation

virtualenv --version

Step 2: Create a virtual environment.

After running the below command, a directory named virtualenv_name will be created. This is the directory that contains all the necessary executables to use the packages that a Python project would need.

virtualenv virtualenv_name

Step 3: Create a new virtual environment

virtualenv -p /usr/bin/python3 venv

where venv is the name of the virtual environment you can change it accordingly

Step 4: Activate our newly created virtual environment

source venv/bin/activate

If everything works well then the name of your virtual environment will appear on the left side of the terminal as shown in the above image which means that the virtual environment is currently active.

Deactivate The Virtual Environment

After you finish working deactivate your virtual environment by simply executing the below command

deactivate

As you can see in the above image the name of our virtual environment disappeared.

Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.