If you’ve used Python quite a bit, you know that sometimes the module you want to use is not supported in the version of Python you have installed. Sometimes you can get around this by using a different module. Other times you are forced to install and maintain a completely different version of Python. At the time of writing this, I have 4 versions installed: 2.7, 3.4, 3.5, 3.6.
For the most part, each Python installation is self contained. So you can pip install
whatever you want into each. Life is good, right? Well, unfortunately, problems tend to arise because of your environment variables. And there are various ones you need to be aware of: PATH
, PYTHONPATH
, PYTHONHOME
, to name the important ones. You need to make sure your environment variables reflect which version you want to be working in at any given time.
Now, constantly changing your environment variables is kind of a pain, so I wrote some PowerShell scripts to help (I’m using Windows 10). Save the following scripts to .ps1
files, and you should be able to execute them in PowerShell. For example the first I have saved as p27go.ps1
.
############################################################ # Switch to Python 2.7 ############################################################ # Get current PATH $current_path = [Environment]::GetEnvironmentVariable("Path", "Machine") # Remove all python from PATH $current_path = $current_path.replace("C:\Python27\Scripts\;", "") $current_path = $current_path.replace("C:\Python27\;", "") $current_path = $current_path.replace("C:\Python34\Scripts\;", "") $current_path = $current_path.replace("C:\Python34\;", "") $current_path = $current_path.replace("C:\Python35\Scripts\;", "") $current_path = $current_path.replace("C:\Python35\;", "") $current_path = $current_path.replace("C:\Python36\Scripts\;", "") $current_path = $current_path.replace("C:\Python36\;", "") # Add python 2.7 to PATH $current_path = "C:\Python27\;C:\Python27\Scripts\;" + $current_path # Set new PATH [Environment]::SetEnvironmentVariable("Path", $current_path, "Machine") # Set new PYTHONPATH [Environment]::SetEnvironmentVariable("PythonPath", "C:\Python27\Lib", "Machine") # Set new PYTHONHOME [Environment]::SetEnvironmentVariable("PythonHome", "C:\Python27", "Machine")
############################################################ # Switch to Python 3.5 ############################################################ # Get current PATH $current_path = [Environment]::GetEnvironmentVariable("Path", "Machine") # Remove all python from PATH $current_path = $current_path.replace("C:\Python27\Scripts\;", "") $current_path = $current_path.replace("C:\Python27\;", "") $current_path = $current_path.replace("C:\Python34\Scripts\;", "") $current_path = $current_path.replace("C:\Python34\;", "") $current_path = $current_path.replace("C:\Python35\Scripts\;", "") $current_path = $current_path.replace("C:\Python35\;", "") $current_path = $current_path.replace("C:\Python36\Scripts\;", "") $current_path = $current_path.replace("C:\Python36\;", "") # Add python 3.5 to PATH $current_path = "C:\Python35\;C:\Python35\Scripts\;" + $current_path # Set new PATH [Environment]::SetEnvironmentVariable("Path", $current_path, "Machine") # Set new PYTHONPATH [Environment]::SetEnvironmentVariable("PythonPath", "C:\Python35\Lib", "Machine") # Set new PYTHONHOME [Environment]::SetEnvironmentVariable("PythonHome", "C:\Python35", "Machine")
Note: There are definitely other ways to do this sort of thing. At the very least, these scripts could all be one script. And it could be modified to weed out any version of Python installed, not just the ones I switch between. But this did the job for me for now. If I were to install any more versions of Python, I would put together a more comprehensive, future compatible script 🙂