Virtual Environment in Python Linux.

Krishna Gaire
2 min readApr 18, 2020

Suppose you are about to work on a new project, In this scenario, you will encounter the term Virtual Environment.

In a simple idea, Virtual Environment for a project means, you give your project a separate “environment” inside an environment(your computer) and allow it to operate there. Virtual Environment aims to isolate your Python and its dependent packages from all projects that are hosted by your computer.

Requirements for creating Virtual Environment is, You need to have Python installed in your machine.

Creating a Virtual Environment:

let’s start by installing the python3-venv package that provides the venv module.

sudo apt install python3-venv

1.

Once the module is installed we are ready to create a virtual environment for Python3.

Switch to the directory where you would like to store your Python3 virtual environments. Within that directory /folder run the following command to create your new virtual environment.

python3 -m venv my-venv-name

After running this command you will see a new folder which internal structure look like this:

Inside the ‘my-venv-name’ folder

Inside the bin folder, you have seen activate, That will be used to run the virtual environment

And Inside the lib/python3.6, you have seen a site-packages folder, This is where any third-party modules you install will be stored.

2.

To start using the virtual environment, you need to activate it by running the activate script.

source my-venv-name/bin/activate

After you run this, Your prompt will change and it will show the name of the virtual environment you’re currently using.

Virtual environment folder name is shown on the left side, as in my case it is ‘my-venv-name’

You have now learned to create and use Python virtual environments.

3.

Once you are done with your work, to deactivate the environment, simply type deactivate and you will go back to normal, prompt without virtual environment.

deactivate

Deactivate Virtual Environment

Thank you, happy coding…

--

--