Python and virtualenv on Debian


Virtualenv is useful for creating a virtual environment. A local playground (adda) where you can manually install all the packages and be assured that the underlying GNU/Linux system which also relies on python packages (with specific version umbers) is not affected.


There are some initial dependencies we need to install as root

#apt-get install python3 python3-venv virtualenv python3-virtualenv

Get started

First decide on a place where you want the virtualenv. Lets say /home/user/pyadda is where we want our virtualenv to be.

$ python3 -m venv pyadda

If you notice we are using venv module from python 3 specifically to create the virtualenv. This is needed instad of simple virtualenv command due to the reason that we still have virtualenv pointing to old python2.

$ cd pyadda/
~/pyadda$ ls
bin include lib lib64 pyvenv.cfg share

We go into the freshly created directory and see what is created by the virtualenv tool. Do notice that all the binaries and libraries are created by the script automatically :robot:

~/pyadda$ source bin/activate

This command will activate your virtual environment.

(pyadda) user@host:~/pyadda$

Notice the change in shell prompt, the name inside the brackets is the name of our virtual environment. Use ctrl d to exit it.

You can also check if the binaries are being fetched from the right place.

(pyadda) user@host:~/pyadda$ whereis pip
The whereis command checks for the binary location

pip: /home/user/pyadda/bin/pip /home/user/pyadda/bin/pip3.7
(pyadda) user@host:~/pyadda$

:white_check_mark: This should confirm that we are inside our own adda :wink:

1 Like