Installing virtualenvwrapper Python virtual environment

Overview

This how-to will guide you though setting up virtualenvwrapper using Python3 on Debian based system.


Below is from virtualenvwrpeer doc’s.

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

  1. Organizes all of your virtual environments in one place.

  2. Wrappers for managing your virtual environments (create, delete, copy).

  3. Use a single command to switch between environments.

  4. Tab completion for commands that take a virtual environment as argument.

  5. User-configurable hooks for all operations (see Per-User Customization).

  6. Plugin system for creating more shareable extensions (see Extending Virtualenvwrapper).


Install pip for python3

Before installing the virtual environment, install pip. pip is a package manager which helps to install, uninstall and upgrade packages for your projects.

To install pip for python 3 type:

$ sudo apt update
$ sudo apt install python3-pip

Install virtual environment for python 3

The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want. Install venv package using the following command:

$ sudo apt-get install python3-venv

Install and setup virtualenvwrapper:

Install using PIP package manager, from pypi.org, more information on the virtualenvwrapper module can be found in the link below.

$ sudo pip3 install virtualenvwrapper

If you get this error:

WARNING: The script pbr is installed in '/home/billf/.local/bin'
which is not on PATH. Consider adding this directory to PATH or, if
you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script virtualenv is installed in
'/home/billf/.local/bin' which is not on PATH. Consider adding this
directory to PATH or, if you prefer to suppress this warning, use
--no-warn-script-location. WARNING: The script virtualenv-clone is
installed in '/home/billf/.local/bin' which is not on PATH. Consider
adding this directory to PATH or, if you prefer to suppress this
warning, use --no-warn-script-location.

Reload the shell ~/.profile. For my installation there is an if statement in the ~/.profile that test to see if ~/.local exist and if true it will add to PATH:

$ source .profile

See if the PATH was added, if not you will need to add the PATH: in your ~/.profile.

$ **env \|grep /home/billf/.local/bin**
PATH=/home/billf/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Activate virtualenvwrapper using shell startup file

To activate virtualenvwrapper source $HOME/.local/bin/virtualenvwrapper.sh

$ source $HOME/.local/bin/virtualenvwrapper.sh
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/premkproject virtualenvwrapper.user_scripts
creating /home/billf/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/initialize virtualenvwrapper.user_scripts
creating /home/billf/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/predeactivate virtualenvwrapper.user_scripts
creating /home/billf/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/preactivate virtualenvwrapper.user_scripts
creating /home/billf/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating
/home/billf/.virtualenvs/get_env_details

You my get this error if some of the required system packages are not installed, simply install them the and try again

Command ” not found, but can be installed with:
sudo apt install mailutils-mh    # version 1:3.7-2.1, or
sudo apt install meshio-tools    # version 4.0.4-1
sudo apt install mmh             # version 0.4-2
sudo apt install nmh             # version 1.7.1-6
sudo apt install termtris        # version 1.3-1

Now add the virtualenvwrapper startup script to your ~/.bashrc. file,this will activate virtualenvwrapper when you start a shell.

$ nano ~/.bashrc
# added the line below to the bottom of the .bashrc file.
$ $HOME/.local/bin/virtualenvwrapper.sh

Setup virtualenvwrapper environment.

Now, create a virtual environment, I will use “mysite” in this example.

$ mkvirtualenv mysite
created virtual environment CPython3.8.10.final.0-64 in 144ms
   creator CPython3Posix(dest=/home/billf/.virtualenvs/mysite, clear=False, no_vcs_ignore=False, global=False)
   seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/billf/.local/share/virtualenv)
      added seed packages: pip==21.1.3, setuptools==57.2.0, wheel==0.36.2
   activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /home/billf/.virtualenvs/mysite/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/billf/.virtualenvs/mysite/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/billf/.virtualenvs/mysite/bin/preactivate
virtualenvwrapper.user_scripts creating /home/billf/.virtualenvs/mysite/bin/postactivate
virtualenvwrapper.user_scripts creating /home/billf/.virtualenvs/mysite/bin/get_env_details

List or change to the working virtual environments. If no environment_name is given the list of available environments is printed to stdout.

$ workon mysite

To exit the environment type the following command;

$ deactivae

I configured the projects postactivate file to change into the working directory of my project folder when I activate the virtual environment.

$ nano  ~/.virtualenvs/mysite/bin/postactivate
#I add this line for this project name you would put what ever you use for a poject
$ cd /Devel/mysite

I want to revert back to my home directory when I deactivate the virtual environments. I add this to the ~/.virtualenvwrapper/postactivate file, which is global, it will run this script for all of my projects.

$ nano  ~/.virtualenvs/postdeactivate
# simply add
$ cd $HOME

There are many commands and tweaks with virtualenvwrapper command reference