This article is based on Tyler Butler original article from 2014. Tyler has done a fanstastic job of documenting the steps, however, when I followed the steps verbatim, I had many issues on Windows 10 machine. I have attempted to document the workarounds on Windows 10 machine to get VirtualEnv working properly on Windows 10. I have taken the liberty of using some of the steps which is already outlined in the original article.
For the sake of my learning I’m using 32 bit version of Python. This is what I noticed a lot of developers recommending to install as there is no significant advantage of using 64 bit and most libraries and drivers are available on 32 bit mode. I have not personally validated this claim but I’m not going to risk further delays with issues on 64 bit installations.
By default the latest version of Python will be on the environment path for easy execution. Here are the steps that I had to take to ensure proper installation of virtualenv so that I can use different versions of Python in parallel across projects.
- First ensure that you have both versions of Python installed in two different directories.
- I want to use Virtual environments in Python 2.7 . pip (Python Package Installer) was already installed under the Scripts folder as part of 2.7.12 version of Python.
- The next step is to ensure which Python version is in the PATH variable. By default when the newer versions are installed, user is provided an option to add Python executable to the PATH variable.
- Since we will be using a Powershell to use virtualenv, I suggest that the a proper profile is created with the right path. More info on powershell profiles can be found here.
- Open Powershell by Typing “Powershell” in the Search of the task bar. Right Click on it and “Run as Administrator”
- Ensure that Set-ExecutionPolicy is set to Unrestricted (more info on this is here.)
PS C:\> Set-ExecutionPolicy Unrestricted
In our case, we probably want the CurrentUserAllHosts profile, since that will execute for us in every PowerShell instance. If you navigate to the location listed, there might not be a file there to edit. In that case, the following command will create a file there in the right format:
PS C:\> New-Item -Path $Profile.CurrentUserAllHosts -Type file -Force
Navigate to the profile path ($Home\Documents\WindowsPowerShell\profile.ps1). In the profile file, let’s update the PATH so that the Powershell will always invoke the Python 2.7 version by default. In addition to Python 2.7, I have added other programs that I intend to use as well. If you notice, I did not include Python 3.6.0 in the path here.
$env:Path = "C:\Program Files (x86)\Python27;C:\Program Files (x86)\Python27\Scripts;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live; C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Windows\SysinternalsSuite; C:\System Tools\SysinternalsSuite;C:\Program Files (x86)\Java\jdk1.7.0_10\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Java\jre1.8.0_66; C:\Program Files\MongoDB\Server\3.4\bin";
Adding the this PATH to powershell now, saves a whole of inexplicable errors in the subsequent steps.
Close Powershell and Launch it again as Administrator
Type get-command python on the shell and ascertain that the Source points to the Python 2.7 path
PS C:\WINDOWS\system32> get-command python
CommandType Name Version Source ----------- ---- ------- ------ Application python.exe 0.0.0.0 C:\Program Files (x86)\Python27\python.exe
Now let’s install virtualenv
PS C:\> pip install virtualenv
Once installed, verify the virtualenv.exe file is successfully installed under the Python 2.7 Scripts Folder
Since we have already added the PATH environment variable, virtualenv.exe is available in the path. To confirm type
PS C:\WINDOWS\system32> virtualenv --version
The command will print the current version of virtualenv program and exit.
If you get an error such as:
virtualenv: The term 'virtualenv' is not recognized as the name of a cmdlet…….
This means that the PATH variable is not set properly. Verify steps 8 and 9 to ensure the PATH is set properly and includes and Scripts folder.
Now, let’s install the Powershell Wrapper for Virtualenv. More information on the Powershell Wrapper can be found here. However before installing, Create the directory structure manually under $HOME/Documents called WindowsPowerShell\Modules\VirtualEnvWrapper. This is an important step as for some reason, I kept getting “Access Denied” error when installing the PowerShell wrapper even though I had administrator rights.
PS C:\> pip install virtualenvwrapper-powershell
Once the wrapper is installed successfully, navigate to the directory where the Wrapper is installed. It should be installed under $HOME\Documents\WindowsPowerShell\Modules\VirtualEnvWrapper. Open the file VirtualenvWrapperTabExpansion.psm1 on line 12 from this:
$_oldTabExpansion = Get-Content Function:TabExpansion
to this:
$_oldTabExpansion = Get-Content Function:TabExpansion2
This step will aviod the ” Get-Content : Cannot find path ‘Function:\TabExpansion’ because it does not exist.” error when importing the modules into PowerShell.
Once this is done, Set two additional environment variables in the system. Navigate to Contraol Panel –> System –> Advanced System Settings –> Environment Variables.
Add the following two environment variables
Variable Name | Variable Value |
VIRTUALENVWRAPPER_PYTHON | <path to Python 2.7 installation> |
VIRTUALENVWRAPPER_VIRTUALENV | <path to Python 2.7 installation>\Scripts |
The next step is to import the wrapper modules into PowerShell. Type “Import-Module virtualenvwrapper” on the prompt
PS C:\> Import-Module virtualenvwrapper
Unfortunately, you might see an error will say something like this:
Virtualenvwrapper: Virtual environments directory 'C:\Users\seshan /.virtualenvs' does not exist. Create it or set $env:WORKON_HOME to an existing directory.
Well, at least you know you’re on the right track! Do exactly what the message says: create the missing directory.
mkdir '~\.virtualenvs'
You might also want to change the location to store your virtual environments. To do that, set the $env:WORKON_HOME variable to wherever you want to store them. I generally stick with the default of ~\.virtualenvs though. Whatever you do, remember this location; it will come in handy.
Now before moving forward, there is a small registry update that is required to address any Registry errors such as the one shown below when running the Import-Module Command.
Set-ItemProperty : Cannot find path 'HKCU:\Software\Python\PythonCore\2.7\InstallPath' because it does not exist.
To address this issues, created a file called “fix_python.reg” in a temp folder of your computer with the following information:
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Python] [HKEY_CURRENT_USER\Software\Python\Pythoncore] [HKEY_CURRENT_USER\Software\Python\Pythoncore\2.7] [HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\2.7\InstallPath] @="C:\\Program Files (x86)\\Python27" "ExecutablePath"="C:\\Program Files (x86)\\Python27\\python.exe" [HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\2.7\PythonPath] @="C:\\Program Files (x86)\\Python27\\Lib;C:\\Program Files (x86)\\Python27\\DLLs;C:\\Program Files (x86)\\Python27\\Lib\\lib-tk"
Ensure that the values are updated to reflect your correct version and path. Execute the registry file to add it to your computer’s registry.
Once all these steps are completed, Close the Powershell and Start a new one.
In the new PowerShell, Type Import-Module virtualenvwrapper command.
PS C:\> Import-Module virtualenvwrapper
Now Type “Get-Command *virtualenv*”
PS C:\WINDOWS\system32> get-command *virtualenv*
CommandType Name Version Source ----------- ---- ------- ------ Alias cdvirtualenv -> CDIntoVirtualEnvironment 0.1 virtualenvwrapper Alias cpvirtualenv -> Copy-VirtualEnvironment 0.1 virtualenvwrapper Alias lsvirtualenv -> Get-VirtualEnvironment 0.1 virtualenvwrapper Alias mkvirtualenv -> New-VirtualEnvironment 0.1 virtualenvwrapper Alias rmvirtualenv -> Remove-VirtualEnvironment 0.1 virtualenvwrapper Alias setvirtualenvproject -> Set-VirtualEnvProject 0.0 support Function add2virtualenv 0.1 virtualenvwrapper Function CDIntoVirtualEnvironment 0.1 virtualenvwrapper Function Copy-VirtualEnvironment 0.1 virtualenvwrapper Function Get-VirtualEnvironment 0.1 virtualenvwrapper Function New-VirtualEnvironment 0.1 virtualenvwrapper Function New-VirtualEnvProject 0.0 support Function Remove-VirtualEnvironment 0.1 virtualenvwrapper Function Set-VirtualEnvironment 0.1 virtualenvwrapper Function Set-VirtualEnvProject 0.0 support Function showvirtualenv 0.1 virtualenvwrapper Function VerifyVirtualEnv 0.0 support Application virtualenv.exe 0.0.0.0 C:\Program Files (x86)\Python27\Scripts\virtualenv.exe
You should see an output similar to this. Ensure that the Application virtualenv.exe is also listed in the output. Please note that not all virtualenv commands have been ported successfully to the Powershell Wrapper.
Now you can create virtual environments using New-VirtualEnvironment cmdlet
PS C:\WINDOWS\system32> New-VirtualEnvironment testenv New python executable in C:\Users\seshan\.virtualenvs\testenv\Scripts\python.exe Installing setuptools, pip, wheel...done. Added activation script por Powershell to C:\Users\seshan\.virtualenvs/testenv\Scripts. (testenv)PS C:\WINDOWS\system32>
As you can see once the new Virtual environment is created, the shell automatically changes into the virtual environment. You can see the prefix before the drive letter with the virtual environment name.
You can also type the command workon to see the virtual environment listing and switch between them.
Now, once you exit from Powershell, All modules imported will be lost. So, when you re-invoke a new shell, you will have to run the Import-Module command again.
PS C:\> Import-Module virtualenvwrapper
Instead, remember the profile file we created in the beginning to add PATH settings, Edit that profile file and add this command below the PATH Command. Navigate to $HOME\Documents\WindowsPowerShell and Edit the profile.ps1 file and add the command as the next line below the PATH.
$env:Path = "C:\Program Files (x86)\Python27;C:\Program Files (x86)\Python27\Scripts;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Windows\SysinternalsSuite;C:\System Tools\SysinternalsSuite;C:\Program Files (x86)\Java\jdk1.7.0_10\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Java\jre1.8.0_66;C:\Program Files\MongoDB\Server\3.4\bin";
Import-Module virtualenvwrapper
In summary, I encourage you to read Tyler’s post first and clearly understand the steps. Then follow the steps that I have outlined here to get virtualenv working on Windows 10.