Dual Installations of Python 2.X and Python 3.X with Anaconda/Spyder

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. 😉

Overview

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.

Tutorial

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

conda -V

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

python -V

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

followed by

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!).

Update conda:

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):

activate $ENV_NAME
pip install $PKG_NAME
deactivate

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:

activate $ENV_NAME

Exit virtual environment $ENV_NAME in Windows command prompt:

deactivate

15 Replies to “Dual Installations of Python 2.X and Python 3.X with Anaconda/Spyder”

  1. Truly, unbelievable this step is not mentioned in any support documentation.. thanks for showing the way. Might be useful to add to the last step on Spyder that, if going through the GUI/IDE, one just needs to change folder from “Root” to wherever the alternate environment has been installed.

    1. Hi ramuaddib,

      Can you explain your step in a bit more detail? I don’t quite understand, so I can’t recreate it.

      Thanks!

      Jenni

  2. I just started using Anaconda this week. I installed the new py35 environment from CLI as described above. I learned a simpler way to manage the different environments:
    – from Anaconda Nav home, select Environments on the Left side
    – select the newly created “py35” (as I have it named) and Anaconda installed all the base apps for me, to include Spyder
    – management and updates can be taken from CLI (via “activation” of new Python install) or from opening apps in Anaconda

  3. Thank you so much for this post – I inherited this server running 2.7 from a colleague that used to work here and your instructions are terrific. Two comments:
    1. You can get a conda cheat sheet here which has a nice .pdf that can be downloaded. https://conda.io/docs/_downloads/conda-cheatsheet.pdf

    2. For those of us that use PowerShell, the activate and deactivate won’t work. You have to download Will Ernst’s .ps1 files. I tested them both and they work great. https://github.com/Liquidmantis/PSCondaEnvs

    1. Ooh, thanks for these tips! I didn’t know about the PowerShell issues, that is frustrating but really good to know.

  4. Hi,
    I had Python 2.7 and Spyder 2.7 installed. Now I need Python 3.6. I installed it, but I don’t know if I should install Spyder 3.6 as well. Also, even though, I do have Anaconda- Spyder 2.7, typing “conda -V” , it cannot recognize that I have anaconda and no error is NOT returned. Should I uninstall Spyder 2.7 then?

    Thanks

    1. Hmm…this issue sounds a little more complicated than I can easily resolve using comments on this webpage. If you haven’t already, I would definitely recommend making a post on Stack Exchange explaining your issue and providing some details on what set-up you had. For example, how did you have things installed? Are you running these commands off a normal Windows terminal or an Anaconda Prompt? If you can create a clear list of steps to recreate the issue, then you can also submit a bug report at Anaconda so they can fix it for future users. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *