Python on the RDP
There are two production versions of Python: Python 2 and Python 3.
- You can run scripts written for 3 with 2 if you import the special package
__future__
- You can run scripts written for 2 with 3 if you run the script
2to3
- All new code should be written in Python 3 for library support and maintainability reasons. If for no other reason know that it uses UTF-8 as the default encoding and so it makes your life easier in every possible way.
Contents
Installation
Many Python modules are actually 90% low-level C code with the remaining 10% a python wrapper. That means to install these modules (e.g. numpy, scipy, hdf) you need to have all of the development libraries and compiler tools to compile the C stuff. Enter Anaconda, a widely used repackaging of Python for Windows that comes with most of the libraries you'll ever need. In addition, they've made it easy to have two installs of Python running at once.
- Python2
- Python 2.7.14 via Anaconda2 5.0.1 for 64-bit
- Located at
C:\ProgramData\Anaconda2
*
- Python3
- Python 3.6.3 via Anaconda3 5.0.1 for 64-bit
- Located at
C:\ProgramData\Anaconda3
*
* Note that Anaconda is not installed in C:\Program Files\AnacondaN
intentionally. Spaces in the path lead to a bunch of unpredictable errors.
Manually Defining Explicit Python Binary Executables
A common practice is to have a bunch of specific Python commands to force explicit behavior. For example on Ubuntu, you can choose from running python, python2, python 2.6, python2.7, python3, python3.5, etc in the shell. To emulate this behavior, copy and paste the python.exe
and rename it to pythonN.exe
where N is whatever specific version you copied. The same applies to scripts you want to use like pip
: copy-and-paste the file to a more explicit name.
Path is evaluated with the first entries taking precedence. Thus, whichever version of Python you want to be the default should be listed at the leftmost position on the string. You can also just delete the unregistered python.exe to achieve the same result.
ArcMap Aside
Note that by default, ESRI ArcMap maintains a separate Python 2 installation at C:\Python27
. Just ignore this...it is not to be used for development or as the environment for running programs other than ArcMap.
Simultaneously Registering Python 2 and 3
Anaconda automatically "registers" the separate installations for use with some development environments. It does not (by default) add executables to the Path system environment. We will add the base AnacondaN/*
files and AnacondaN/Scripts/*
to the path as so:
- Right-click on the Windows icon in the bottom left
- Select System
- Select Advanced System Settings (must be administrator)
- Select the Advanced Tab
- Select "Environment Variables..." at the bottom right
- Add semicolon-separated directories to Path in the lower box labeled System Variables
Explicitly Running a Specific Python Version
All of these are valid ways to run Python on the RDP:
python
python2
python3
By default, running python
will run Python 2. This behavior should not be relied on. On most other systems, Python 3 is the default instead.
It is the same case with pip
which can be run via pip
, pip2
, pip3