Create virtual environment in Python3

Python virtual environment is tool that keeps every package in solated from system. This tool is required when you are working on different project on same machine. This will help to install same package with different version for different package.

In this article, we will install and create virtual environment for Python 3. We will use PIP to install the package. If you have not installed PIP, you can install PIP with this article. So let's start by installing virtual environment.

First open the Terminal and run the below command.

pip3 install virtualenv

or install using apt command for Debian based distribution system.

sudo apt-get install python3-venv

You have installed virtual environment package. Now we need to create virtual environment for every project. For that, go to the project directory in Terminal and run the command.

python3 -m venv new-env

This will create new-env directory with python files. This directory will install all packages when you activate environment. To use this environment, you need to activate it. To activate environment, run the below command.

source new-env/bin/activate

When you run this command, environment name will be prefixed in the new line.

Output:

(new-env) root@hackthestuff-dell-inspirion:/var/www/python

Now all your python package will install and remove package in this environment. Once you done with environment package, you can deactivate the environment with deactivate command.

deactivate

Conclusion

So in this article, we have learned how to install and create Python environment. This way you can create as many environment as you want. Thank you for giving time in reading the article.

Tags: