Python, Research

Python Virtual Environments 2: Venv, Virtualenv, & Virtualenvwrapper

Now that I’ve got venv installed and have made a virtual environment, I want to spend time reading the article Python Virtual Environments: A Primer to pick up more tips and tricks. While virtual environments help keep things organized internally, when multiple are made it can get tricky to switch between them.

That’s where the virtualenvwrapper tool comes into play. It helps with organization and management of virtual environments, as well as creating commands to easily switch between them. Basically it’s just an extension of virtualenv, giving us more commands at our disposal. It doesn’t replace venv or virtualenv, but it is yet another tool that can be used.

Totally simple stuff, amirite?

To install virtualenvwrapper, I opened the terminal and typed:

pip install virtualenvwrapper

Nothing happened after typing in “which”, so something clearly went wrong. I uninstalled and discovered I had to use “sudo” to install. Which then got me questioning whether I should use “pip” (which installs to Python 2) or “pip3” (which installs to Python3).

Way too many things to ponder today

Researching this question led me to a FANTASTICALLY HELPFUL article Understanding Python Installation and VirtualEnv : A friendly guide for beginners and not-so-beginners that has FINALLY watered everything down to the level I need to make sense of this all.


So once again, I went back to the terminal and started entering commands:

sudo apt update
sudo apt upgrade
sudo apt autoremove
sudo apt install python3
sudo apt install python3-pip
sudo pip3 install virtualenvwrapper
sudo nano ~/.bashrc

I then copy and pasted the following into the bashrc file and usd Ctrl+X to save and close.

# default location of virtual environment directories
export WORKON_HOME=$HOME/.virtualenvs
# default python version to use with virtualenv
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
source `which`

Back in the terminal I ran the command source ~/.bashrc. I then tested out the functionality of virtualenvwrapper by typing the following into the terminal.

mkvirtualenv name_of_environment #made environment
workon #listed available environments
workon name_of_environment #activates environment
deactivate #exits out of virtual environment
rmvirtualenv name_of_environment #removes environment



After tinkering with using the different commands between virtualenv, venv, and virtualenvwrapper, I came up with my own “cheat sheet” of commands that I feel the most comfortable using. I listed them below along with an explanation of what I am using them for, they are mostly virtualenvwrapper commands with one venv command and some others that were referenced in articles I’ve read.

  • Create an environment w/ Python3 default: mkvirtualenv env_name_here
    • The default python version to use is stored in the bashrc file
    • To change this run the command: sudo nano ~/.bashrc and edit accordingly
  • Create an environment with a specific python version:
    • Python3: python3.6 -m venv env_name_here
    • Python2: virtualenv -p $(which python2) env_name_here
    • NOTE: to ensure this environment is installed in the correct location, open the terminal from the same location as your other virtual environments OR run command: cd .virtualenvs
  • List environments available: workon
  • Activate an environment: workon env_name_here
  • Deactivate an environment: deactivate
  • Remove an environment: rmvirtualenv env_name_here
  • Manually remove an environment:
    • Locate the folder of the virtual environment (check /home/yourusername/.virtualenvs) and manually delete it
    • For a locked folder run command: sudo nautilus to launch the file browser with root privileges, then locate the folder and delete
  • List modules installed in environment: pip freeze
  • Display python version of active environment: python -V
  • Displays the location that is associated with a key word: which keyword
    • For example: which python, which python3, which pip, etc
  • Display pip version of active environment: pip –version
  • Display where global site-packages are installed in the active environment: python -m site
  • Display where per-user site-packages are installed in the active environment: python -m site –user-site
  • List the locations that are currently referenced by the $PATH variable: echo $PATH
    • The order in which these paths are listed are the order the that programs will search for an executable file, this is how you can ensure the compatible version of modules are used by programs within the environment
    • Since the virtual environment’s bin folder is prioritized first, its instance of Python is used instead of the one referenced system-wide
Have your eyes glazed over yet? Then go watch Portlandia and find the episodes that have Kumail Nanjiani. He’s fucking hilarious, watch the cell phone plan episode where the Mayor is missing. Maybe I should just start blogging about comedy TV shows that people should watch instead of this boring tech stuff.

And if all of that barely makes sense and doesn’t satisfy you, then you are welcome to read the masterfully written in-depth response from user Flimm that breaks down the differences between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, and you can create your own cheat sheet of commands to use between them all.

Seriously, I made this cheat sheet for ME, 🎶 do what you want, what you want with your body 🎶

I’ve been a super Excel nerd for the past 15 years of my career (my first language was actually VBA), and python reminds me of it as there are a million different ways to do the same thing. While some ways are easier than others there is rarely a “wrong way” to do something if it works for you!

Okay, maybe there ARE some “wrong ways” that I’ve discovered, but everyone starts as a newbie so just cut me some slack

Now that I’ve got python installed on my laptop and have a firm grasp on virtual environments and python folder structure, I think I’m finally ready to pick up where I left off from my Neural Network project. In case you don’t remember (or more likely, didn’t read it) I want to automatically make memes based on the neural network’s output and then have ones I approve post to an Instagram account.

But that’s for another day, for now I’ll let Princess Marshmallow wave goodbye, I’M OUT!

Leave a Reply