How to Set Up mkvirtualenv on Windows for Python Virtual Environments


Managing Python virtual environments can get messy — but with mkvirtualenv, you can create and switch environments with ease. This guide walks you through setting it up on Windows using virtualenvwrapper-win.


🛠️ Step 1: Install Python and pip

Make sure Python is installed and available in your system PATH.

python --version
pip --version

If either command fails, install Python and enable “Add to PATH” during setup.

📦 Step 2: Install virtualenvwrapper-win

This is the Windows version of the popular Unix-based virtualenvwrapper.

pip install virtualenvwrapper-win

Once installed, you get access to these helpful commands:

  • mkvirtualenv - create a new virtual environment
  • workon - activate an existing environment
  • deactivate - deactivate the current environment
  • rmvirtualenv - delete an environment
  • lsvirtualenv - list all environments
  • setprojectdir - set the environments working directory

🧪 Step 3: Create Your First Virtual Environment

mkvirtualenv myproject

To deactivate the current environment, just run:

deactivate

That’s it! Your environment will be created and automatically activated. You’ll now see the environment name in your terminal prompt.

🔁 Step 4: Switch Between Environments

workon myproject

Then, as I stated above you can just deactivate the current environment with:

deactivate

Now you can use:

workon myotherproject

to open the environment for your next project!

🧹 Step 5: Delete an Environment

rmvirtualenv myproject

Be careful when you use this command, it can take some trial-and-error to get all of your dependencies back if you don’t have a requirements.txt file handy! This command deletes the folder permanently.

📍 Bonus Tip: Set Your Default Environments Directory

By default, virtualenvwrapper-win stores all of your environments in %USERPROFILE%\Envs. If you want to change that directory, you can:

  1. Open your autoexec.bat (or you can set a user environment variable):
  2. Add or change:
set WORKON_HOME=C:\Path\To\Envs

Once you restart your terminal, the changes have taken effect.

📖 Summary

virtualenvwrapper-win and mkvirtualenv make managing your python environments much easier and cleaner within Windows. Here’s a quick cheatsheet to help get you going:

CommandDescription
mkvirtualenvCreate and activate a new env
workon envActivate existing env
deactivateExit the current env
rmvirtualenvDelete the environment
lsvirtualenvList all local environments

Now you are ready to keep your Python projects organized and isolated, the right way!