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 environmentworkon- activate an existing environmentdeactivate- deactivate the current environmentrmvirtualenv- delete an environmentlsvirtualenv- list all environmentssetprojectdir- 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:
- Open your autoexec.bat (or you can set a user environment variable):
- 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:
| Command | Description |
|---|---|
| mkvirtualenv | Create and activate a new env |
| workon env | Activate existing env |
| deactivate | Exit the current env |
| rmvirtualenv | Delete the environment |
| lsvirtualenv | List all local environments |
Now you are ready to keep your Python projects organized and isolated, the right way!