How to use VirtualEnv in Python

What is VirtualEnv

virtualenv is a tool to create isolated Python environments. This is a very useful tool if you want to try out some Python package but you do not want to get your system’s Python package affected. If you have need to use the specific package version for specific project, this is also a convenient way to utilize this package. In this post, it will show you how to use VirtualEnv in Python.

How to use the VirtualEnv in Python

First of all, check if you have VirtualEnv package installed. Run the following line of command in the terminal

pip3 show virtualenv

If you have the virtualenv package installed, it should return something like below:

Name: virtualenv
Version: 16.7.6
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb@colorstudy.com
License: MIT
Location: /Users/developer/Library/Python/3.7/lib/python/site-packages
Requires: 
Required-by: 

You can also use pip3 list to simply show the list of Python packages installed in your system.

Install virtualenv package

If you do not have virtualenv package, run the below command to install one in your system.

pip3 install virtualenv

Make a VirtualEnv directory

To make things organize, choose a directory where you want all your virtual environments will be situated.

For example, in my case, I have created a directory called virtualenv under /Users/developer/Sites/python/. This location is up to you where you want to put them.

Make a container for VirtualEnv development

This time, you need to create a directory where VirtualEnv will deploy some initial files to have an isolated environment for your development.

Go to your VirtualEnv directory and create a directory called helloworld

For example cd to following path,

cd /Users/developer/Sites/python/virtualenv/

and mkdir helloworld

mkdir helloworld

Initialize the VirtualEnv

Execute the following command to initialize the VirtualEnv.

virtualenv helloworld

When you get a command not found error

Note: If you have multiple versions of Python, it may throw some error such as command not found: virtualenv. If you encounter such error, make sure you have the right path to execute the virtualenv.

NOTE: Initializing VirtualEnv command changed to the following. Instead of issuing the virtualenv [container name], use the below command.

python3 -m venv env

Please check more details from the below link.

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment

Check your virtualenv package location. You can check by issuing the pip3 show virtualenvcommand in terminal.

Location: /Users/developer/Library/Python/3.7/lib/python/site-packages

This means, your virtualenv needs to be executed from that path. Thus you need to execute a command something like below:

python3 /Users/developer/Library/Python/3.7/lib/python/site-packages/virtualenv.py helloworld/

Once the virtualenv command is executed, it will generate some initial files and you will see a message something like below:

Using base prefix '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/developer/Sites/python/virtualenv/helloworld/bin/python3.7
Also creating executable in /Users/developer/Sites/python/virtualenv/helloworld/bin/python
Installing setuptools, pip, wheel...
done.

If you ls in the helloworld directory, you will also see there are bin include lib directory has been created.

Activate your VirtualEnv

To activate your virtualenv, you need to execute the activate in the bin directory generated by virtualenv. For example, if you already in the helloworld directory, you will see the bin directory. In this case execute the following command.

source bin/activate

Once the virtualenv is activated, you will see the virtualenv’s name in front of the command prompt.

You can check also by executing the command

which python

It will return such as:

/Users/developer/Sites/python/virtualenv/helloworld/bin/python

This means, it is using a Python under/Users/developer/Sites/python/virtualenv/helloworld/bin/python

If you are in this environment and if you install the packages, it will only install in this environment and will not affect the python packages in the system itself.

Deactivate the VirtualEnv

While you are in the virtual environment, type the following command to deactivate the virtual environment.

deactivate

You will see the (helloworld) in front of the command prompt is now gone.

Reference

https://virtualenv.pypa.io/en/latest/

Latest Posts

Feel free to share this post!

Scroll to Top