Tutorial creating virtual environment for Python

Posted in Uncategorized

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

By creating virtual environment for Python, you can use different versions of Python on the same system.

You first have to create a directory to hold your virtual environment. Let’s call our new folder “venv”. Create this directory with this command in the terminal…

mkdir venv

You then need to install the Python module “virtualenv” with this command…

pip3 install virtualenv

Now you have the virtualenv module installed. It’s basic command is “virtualenv -p python3 <dir>” where <dir> is the directory name of your virtual environment folder. And the “-p python3” is the python interpreter to use. Reference here. In our case, the command would be …

virtualenv -p python3 venv

However, you might have to run it like this …

python3 -m virtualenv -p python3 venv

This means invoke the python3 interpreter to run the module “virtualenv” (that’s the -m flag).

If you are on a Mac, you might need to precede that command with sudo.

After this, you “venv” folder is no longer empty. It has a “bin”, “lib”, and “includes folders. Inside the “bin”, you find an “activate” script.

We activate the virtual environment with the source command passing to it the activate script…

source venv/bin/activate 

Now when you type …

python -V

It is the python of your virtual environment.

To get out of the virtual environment, type “deactivate”.


Related Posts

Tags

Share This