In this section, we will explore how to use Virtualenv to create, as well as manage, a virtual environment. You will now discover the usefulness of our Virtualenv installation that we just learned. This will further enhance our knowledge about virtualization from the context of GPU programming. Follow these steps to get started:
- Launch a new Terminal. Create a new empty directory in the $HOME location (/home/user-directory/). Here, we name it env:
sudo mkdir env
- After creating the directory, let's now use virtualenv to place the new environment on it:
virtualenv env
- The preceding command will yield an output similar to the following result on your screen:
Using base prefix '/usr'
New python executable in /home/avimanyu/env/bin/python3
Also creating executable in /home/avimanyu/env/bin/python
Installing setuptools, pip, wheel...
done.
- Now the virtual environment has been created in our newly creating directory. You can verify the new contents with the ls command:
ls env
The new contents are inclusive of three directories: bin, include, and lib:
bin/ include/ lib/
- Let's now check the contents of each of these three directories in our new virtual environment:
ls ~/env/bin
The contents of $HOME/env/bin are revealed:

- Similarly, we can also find out the contents of the other two directories:
ls ~/env/include
This will give us the following output:
python3.6m@
Here, m signifies a configuration done with the --with-pymalloc flag. It is a faster implementation than malloc.
- Now we check the contents of the last directory in our virtual environment directory:
ls ~/env/lib
python3.6/
ls ~/env/lib/python3.6
The last command will give the following output:
Here, we again find three directories: distutils, __pycache__, and site-packages.
- Let's explore their contents as well. We do this to gain a brief idea on every part of the entire environment structure:
ls ~/env/lib/python3.6/distutils
distutils.cfg __init__.py __pycache__/
ls ~/env/lib/python3.6/distutils/pycache
__init__.cpython-36.pyc
.pyc files are compiled bytecode files in Python. They enable faster access when importing libraries. Using bytecode makes the process more convenient:
ls ~/env/lib/python3.6/__pycache__
This command will give the following output:

- Finally, we find that there are many sub-directories present in site-packages. It also contains its own pip package manager that is isolated to our virtual environment only:
ls ~/env/lib/python3.6/site-packages
The command will give the following output:

Let's stop our navigation at this stage. Now we have a brief outlook on how a newly created virtualenv environment is structured.
Now, let's activate the virtual environment:
$ source ~/env/bin/activate
(env) $ which python
/home/avimanyu/env/bin/python
(env) $ deactivate
We can confirm from the preceding code that our virtual environment is indeed using the local python executable and not the system-wide version. It is isolated from the outer system:
$ which python
/usr/local/bin/python
Now that we are out of the virtual environment after deactivating it, the system-wide python executable is back in charge.