Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, 29 October 2017

Dual Installations of Python 2.7 and Python 3.6 with Anaconda & Spyder



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:

  1. Have both Python versions installed independently
  2. 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


Saturday, 28 October 2017

Installing TensorFlow on Windows




TensorFlow is an open-source software library for machine learning across a range of tasks. According to Wikipedia, it is a symbolic math library, and also used as a system for building and training neural networks to detect and decipher patterns and correlations, analogous to human learning and reasoning. TensorFlow was developed by the Google Brain team and  was released under the Apache 2.0 open source license on 9 November 2015

 This guide explains how to install TensorFlow on Windows.

Determine how to install TensorFlow


You must pick the mechanism by which you install TensorFlow. The supported choices are as follows:
  • "native" pip
  • Anaconda
Native pip installs TensorFlow directly on your system without going through a virtual environment. Since a native pip installation is not walled-off in a separate container, the pip installation might interfere with other Python-based installations on your system. However, if you understand pip and your Python environment, a native pip installation often entails only a single command! Furthermore, if you install with native pip, users can run TensorFlow programs from any directory on the system.
In Anaconda, you may use conda to create a virtual environment. However, within Anaconda, we recommend installing TensorFlow with the pip install command, not with the conda install command.
NOTE: The conda package is community supported, not officially supported. That is, the TensorFlow team neither tests nor maintains this conda package. Use that package at your own risk.

Installing with native pip


If neither of the following versions of Python is installed on your machine, install it now:
-TensorFlow supports Python 3.5.x and 3.6.x on Windows. Note that Python 3 comes with the pip3 package manager, which is the program you'll use to install TensorFlow.

Installing with Anaconda


The Anaconda installation is community supported, not officially supported.
Take the following steps to install TensorFlow in an Anaconda environment:
  1. Follow the instructions on the Anaconda download site to download and install Anaconda.
  2. Create a conda environment named tensorflow by invoking the following command:
C:> conda create -n tensorflow python=3.6


 
  1. Activate the conda environment by issuing the following command:
  2. C:> activate tensorflow
 (tensorflow)C:>  # Your prompt should change
  1. Issue the appropriate command to install TensorFlow inside your conda environment. To install the CPU-only version of TensorFlow, enter the following command:
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow
To install the GPU version of TensorFlow, enter the following command (on a single line):
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu

Validate your installation


Start a terminal.
If you installed through Anaconda, activate your Anaconda environment.
Invoke python from your shell as follows:

$ python

Enter the following short program inside the python interactive shell:


>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))




If the system outputs the following, then you are ready to begin writing TensorFlow programs:
Hello, TensorFlow!