Skip to content

Instantly share code, notes, and snippets.

@GLMeece
Last active November 25, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GLMeece/4a46eea7c4fd02ff7b641fbb98ff0f27 to your computer and use it in GitHub Desktop.
Save GLMeece/4a46eea7c4fd02ff7b641fbb98ff0f27 to your computer and use it in GitHub Desktop.
Setting up a Virtual Environment for Python using VirtualEnvWrapper

Virtual Environments

  1. Before beginning, you should have a version of pip installed. If you don't, it is recommended you install it universally via the Terminal: sudo easy_install pip
  2. Once you do, install virtualenvwrapper: sudo pip install virtualenvwrapper. More information can be found in Read the Docs
  3. Change into the directory where your new project is; e.g., cd ~/Documents/Repos/my_new_project
  4. Create your new environment in this directory, automatically giving it the name of the current directory: thisdir=${PWD##*/};mkvirtualenv -a . -r requirements.txt $thisdir

Using Your Virtual Environment

  • To make sure you remember what your virtual environment is named (and, indeed - all you have created), execute lsvirtualenv which lists all virtual environments.
  • To make sure you're in the virtual environment, execute: workon my_new_project (where the last bit is your virtual environment name).

Uninstalling All Pip packages

If you want to "start over" in a virtual environment by removing all Pip-installed packages, the easiest way to do it is execute:

pip freeze | xargs pip uninstall -y

For Python 3 and Pip 3, you'll need to do this:

pip3 freeze > uninstall_requirements.txt; pip3 uninstall -r uninstall_requirements.txt
@kvabapo
Copy link

kvabapo commented Nov 25, 2019

Cool! I am trying now pipenv. Sofar looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment