Dual
Installations of Python 2.7 and Python 3.6 with Anaconda/Spyder
Anaconda is a
cross-platform python distribution that comes with three IDEs through the Anaconda Navigator:
- jupyterlab 0.27: An extensible environment for interactive and reproducible computing, based on the Jupyter Notebook and Architecture
- notebook 5.0: Web-based, interactive computing notebook environment. Edit and run human-readable docs while describing the data analysis
- spyder 3.2 Scientific python Development Environment. Powerful Python IDE with advanced editing, interactive testing, debugging and introspection features.
It is
one of the best way to go about Python programming. The only issue is switching
between difference versions of Python. A very common reason you would want to
do this is testing code to verify that it runs in both Python 2.7 and Python 3.6,
say for instance. 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.
You can solve the challenge
easily in two ways:
- Have both Python versions installed
independently
- Installed one and then create a virtual
environment to install the other(s)
A.
Having
both executables always instantly available
If you prefer to have
both executables always instantly available from the command line. The you may
have to install both Anaconda2 and Anaconda3 separately. Follow these steps:
1. Install Anaconda2
and Anaconda3 to the C:\ drive as "C:\Anaconda2\" and
"C:\Anaconda3\" respectively.
2. Edit your
"Path" environment variable (Control Panel -> System and Security
-> System -> Advanced system settings -> Environment Variables) and
make sure that
"C:\Anaconda2;C:\Anaconda2\Scripts;C:\Anaconda2\Library\bin" is in
front of "C:\Anaconda3;C:\Anaconda3\Scripts;C:\Anaconda3\Library\bin".
3. Copy and rename the
file "C:\Anaconda3\python.exe" to
"C:\Anaconda3\python3.exe".
4. Copy and rename the
file "C:\Anaconda3\Scripts\conda.exe" to
"C:\Anaconda3\Scripts\conda3.exe"
5.
Copy and rename any other scripts you might use in
"C:\Anaconda3\Scripts\", such as "pip.exe" to "pip3.exe",
etc.
Now, when you type
"python" or "conda" at the command line you will get the
python2 version, and when you type "python3" or "conda3",
etc. at the command line you will get the python3 version.
B.
Switch
the virtual environment
In this case, you
first use the Anaconda prompt to create a “virtual environment” for each
version of Python that we want. A virtual environment is 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.6 Python version to
coexist in the same machine. You can install any number of versions–it 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 “Life of a Jenni”, so many thanks to her for
providing very useful tips!
Step 1: Install
Anaconda
To check if you have Anaconda
installed, open a Windows command prompt and 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.6. 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.6 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.6 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.
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