How to Set-Up Virtual Environment for Python

Honey
3 min readDec 24, 2021

Why Virtual Environment???

Let’s say you want to work on a big project which needs the use of a specific version of Python, lets say Python3 and you might also in future need many other libraries and packages for the same project. So, you might install those directly on your system. Now with time, the more you advance in your project, the more you will bring in other libraries and packages. Also, with time, we get new versions of different things.

Sooner or later, conflicts come about between older and newer versions of all the packages you installed on your main system so far.

To prevent this mess, we create Virtual Environments.

How Virtual Environment Works??

When you create a virtual environment for let’s say, Python3, neither will the packages or libraries installed in the virtual environment affect the main system nor will the happenings in the main system affect your virtual environment.

Thus, it’s like a private Island.

Whenever you want to work with specific projects or stuff, just activate your Virtual environment and start working. Later when you are done with your work, just deactivate your virtual environment and come back to your main system.

Thus it becomes super handy when working on big projects. So, instead of installing everything globally on your system you can create a virtual environment and install the necessary libraries and packages which will then stay specific to that environment created and will not affect your main system.

How to Create a Virtual Environment?

1.On Ubuntu/Linux

Check if pip is installed in your system

$ pip --version

If it is not installed, install pip with the following commands,

$ sudo apt-get update
$ sudo apt-get install python3-pip

Once the installation is complete, verify the installation by checking the pip version with the command mentioned above.

Now install virtualenv using pip,

$ sudo pip3 install virtualenv

Now to create a virtual environment for Python3

$ virtualenv -p python3 myenv

To activate the virtual environment,

$ source ~/myenv/bin/activate

To deactivate,

$ deactivate

2. On Mac Os

If you are on Mac, you will not have a need to install pip, since the work will be done by a package venv which will be already installed in your system for python3. Although pip will already be installed in you system.

#NOTE

venv is for python 3

virtualenv is for Python 2

We will be setting up our virtual environment for Python 3, so we will be using venv for creating the environment. If you want to create a virtual environment for Python 2, use virtualenv instead of venv.

a) To create the virtual environment, run the following command

$ python3 -m venv ~/Documents/newenv

Thus, your virtual environment named newenv is now created in the Documents folder as shown below. If you want to create the environment at any location of your choice, just add the path accordingly.

To activate the environment,

$ source ~/Documents/newenv/bin/activate

To deactivate,

$ deactivate

To remove the virtual environment, go to the folder containing your virtual environment directory and and type the following command,

$ rm -r newenv

Thus, you are now done with creating your new virtual environment!

Have fun building projects!

--

--

Honey

Tech or non-tech 'll post all my crazy cool stuff here!