Python 3 has been around for quite awhile, but I have delayed the upgrade from my 2.7 version partially due to a small fear of my code breaking but mostly out of laziness. Well, I have procrastinated long enough, and today it was finally time to upgrade. However, I wanted to still be able to run/test code in Python 2.7 in case some modules were no longer available in 3.5. The obvious solution from 5 minutes of Googling was virtual environments, but I had trouble finding an “Idiot’s Guide” to working with virtual environments in Windows with Spyder. Once I figured out how to do it, I felt pretty stupid, because it is actually incredibly basic.
So. For the vast majority of the time, I code in Python on Windows. This opens up a lot of debatable questions about what is the ideal coding scenario (obviously this is not it), but the plain and simple fact is that a lot of other people out there also code in Windows. I have generally found using Anaconda—a cross-platform Python distribution—in conjunction with Spyder—the Integrated Development Environment (IDE) that comes with Anaconda—to suit my needs perfectly and to be fairly well-documented. I recommend it highly to anyone interested in coding in Python with Windows.
The exception to this “well documented” rule was in switching between multiple Python versions. A very common reason you would want to do this is testing code to verify that it runs in both Python 2.X and Python 3.X, where an “X” here simply represents any available version number. Alternatively, some Python packages have only been developed for a previous version of Python, so it can be extremely useful to be able to switch to an older version of Python, run some legacy code, then post-process the results in a more “modern” Python version.
I figured out how to set up multiple Python versions with Spyder after a bit of Googling and trial-and-error, but since I didn’t find a tutorial that addressed both the creation of virtual environments and how to work with Spyder from those environments, I wanted to write up something quickly in case it could help others. I also assembled a few useful conda commands at the end of this post to serve as a handy reference.
The remainder of this post is split into three parts:
Finally, before I get into the nitty-gritty details, I probably missed something crucial! So if you would do me the honor of placing any comments or questions in the section below this blog, I will happily update the text if there is better advice floating around out there. Collaboration is a great way to produce a better product. 😉
The methodology, as I mentioned earlier, is actually incredibly simple. We first use the Anaconda prompt to create a “virtual environment” for each version of Python that we want. A virtual environment is basically a Python island–it is a version of Python that is completely isolated from other Python installations. The packages and dependencies for each environment are completely unconnected from other virtual environments, which makes it very useful for switching between codes with different dependencies.
In the tutorial below, I will assume that we want a 2.7 Python version and a 3.5 Python version to coexist in the same machine. You can install any number of versions–it’s all up to you! Once the different versions are installed, you simply open whichever version of Spyder corresponds to the version of interest at that time. It is seriously dead simple.
This tutorial draws very heavily from this page of the “eResearch cookbook”, so many thanks to them for providing very useful steps on creating virtual environments!
Step 1: Install Anaconda
To check if you have Anaconda installed, open a Windows command prompt (winkey+r, type “cmd” and hit “OK”), type
and hit enter. If no error is returned, Anaconda is installed.
I recommend a clean install of Anaconda, especially if you tried to manually install Python before installing Anaconda. Windows is not forgiving when it comes to messing with paths/dependencies/etc., so if you have any issues with the remainder of this tutorial, I recommend uninstalling Anaconda, making sure there are no Python distributions in your C:\ drive, then clean-installing Anaconda.
You can download the Anaconda installer here. It doesn’t really matter what version of Python you choose to install with Anaconda , since you can always create a virtual environment with an older version. However, I would recommend installing the most up-to-date distribution because it’s generally better to be on the curve than behind it.
Step 2: Check conda is up-to-date
If you haven’t yet, open a Windows command prompt. Enter the following command
conda update conda
then enter “y” if prompted to accept the packages that need to be updated.
Step 3: Create your virtual environments
Let’s assume we want two virtual environments: one for Python 2.7 and one for Python 3.5. It is possible that the root environment (i.e., the one that was initially installed with Anaconda) is one of those distributions. You can check your default Python by simply entering
into the command prompt and seeing what version number pops up. I like to keep my versions clearly separated, so in this tutorial I will create two new virtual environments, one for each desired version. This is beautifully simple thanks to Anaconda:
conda create -n python27 python=2.7 anaconda
conda create -n python35 python=3.5 anaconda
With these commands, we have created two virtual environments: one named “python27” and one named “python35”. You can name your virtual environments whatever you want; I chose these names based on my preference. We can verify what virtual environments we have on a given machine by entering
conda info -e
into the command prompt.
Step 4: Open Spyder for desired environment
This step is the one I couldn’t find on the internet, and it is by far the easiest. If we have virtual environments for each version of Python, how do we switch from one version to the other in Spyder? I first tried to change the Python interpreter (Tools -> Preferences -> Python interpreter in Spyder 3.1) so that it pointed to my 3.5 interpreter instead of my 2.7 interpreter. This prompted a warning from Spyder that I was “doing it wrong”. (Classic Rinker move.) I then poked around a bit before I realized…
Anaconda had already installed new versions of Spyder to go with each virtual environment I had created. Yes, holy crap, it was that easy.
So, if you need to run some 2.7 code–just go to the Anaconda folder in your Start Menu and choose the Spyder that corresponds to your python27 virtual environment. (It’s clearly indicated in parentheses next to the program name.) You can even pin different Spyders to your task bar if you need access to them quickly. Seriously, I was quite delighted when I found out how easy this was.
Step 5: Have fun!
Aaaaand that’s it! Hopefully these instructions are clear, though perhaps a bit verbose. Happy coding! 🙂
Some Useful Conda Commands
Here is a collection of useful Windows commands for working with conda and virtual environments in the command prompt. Please leave a comment below if there’s a useful conda command I missed, and I’ll add it to the list (with credit, of course!).
conda update conda
Update Spyder for root environment:
conda update spyder
Update Spyder for virtual environment $ENV_NAME:
conda update -n $ENV_NAME spyder
List available virtual environments:
conda info -e
Install package $PKG_NAME in virtual environment $ENV_NAME using conda:
conda install -n $ENV_NAME $PKG_NAME
Install package $PKG_NAME in virtual environment $ENV_NAME using pip (Anaconda prompt in Windows):
pip install $PKG_NAME
Update package $PKG_NAME in virtual environment $ENV_NAME:
conda update -n $ENV_NAME $PKG_NAME
Update all packages in virtual environment $ENV_NAME:
conda update -n $ENV_NAME --all
Create a new virtual environment $ENV_NAME with Python version X.X:
conda create -n $ENV_NAME python=X.X anaconda
Delete virtual environment $ENV_NAME:
conda remove -n $ENV_NAME -all
Activate virtual environment $ENV_NAME in Windows command prompt:
Exit virtual environment $ENV_NAME in Windows command prompt: