Skip to content

Instantly share code, notes, and snippets.

@chidimo
Created March 26, 2018 14:42
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 chidimo/d66fec9eed65fd1aa7b85530c70ad0e5 to your computer and use it in GitHub Desktop.
Save chidimo/d66fec9eed65fd1aa7b85530c70ad0e5 to your computer and use it in GitHub Desktop.
Creating and working with python virtual environments in windows

Creating and working with python virtual environments in windows

Assumption

I assume here that you have python installed in your system. Or perhaps you have a package manager such as Anaconda or Canopy and that you have your path variables setup correctly. If you don't then head over to any of these pages and download a python installation to start with. I recommend Anaconda personnally. But use any one of your choice

Warning these files are usually large, so you may want to use a non-metered connection.

Setting up your system path

Having downloaded and installed a python distribution, you have to add their installed directories to your system path. Python gives you the option to specify if you want to add it to path during the installation process, so it may not be much of a problem. If you installed Anaconda, then add the following folders to your system path if they were not already added during installation.

  1. <path to anaconda installation>\anaconda
  2. <path to anaconda installation>\anaconda\Scripts

In case you're on a system without admin rights, you can set user environment variables by following these steps (System-wide environment variables is not required) to work with python virtual environments.

  1. Open Run dialog by pressing Windows Logo Key + R
  2. Type rundll32 sysdm.cpl,EditEnvironmentVariables to open environment variables dialog
  3. Edit the path settings to add the required directories.

Installing necessary libraries

pip install virtualenv
pip install virtualenvwrapper-win

Creating and using the virtual environment

To create a virtual environment use the below command (the -a flag is optional. It only associates a directory with a virtual environment such that whenever you activate the environment, you're automagically cded into that directory as well).

mkvirtualenv <envname> -a <path to a directory>` --no-site-packages

To activate the virtual environment use the below command

workon <envname>

In case you're interested, your newly created environment resides in %USERPROFILE%\Envs by default. Just copy paste it into any open windows explorer address bar and it'll lead straight to the directory where you have all your virtual environments. If you want to change that location please consult resource number 3 below

Resources

  1. http://abdelraoof.com/blog/2014/11/11/install-nodejs-without-admin-rights
  2. https://virtualenv.pypa.io/en/stable/
  3. https://github.com/davidmarble/virtualenvwrapper-win
  4. https://virtualenvwrapper.readthedocs.io/en/latest/install.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment