Python Basics

Python is an interpreted language similar to that used in MATLAB. It has a very clean syntax, and is widely regarded as the easiest programming language to learn. It is also one of the most popular and widely used -- see the 2019 IEEE programming language survey. Python supports the principal programming language constructs, including: procedural, object-oriented, and functional programming. Python also runs on nearly all hardware capable of supporting high-level programming languages, and all hardware capable of running an operating system. Code written in Python is therefore highly portable.

Python code runs at speeds comparable to MATLAB -- both being interpreted languages -- and, like MATLAB, also offers the ability to access a JIT (just in time compiler) to speed up intensive computations, and may be compiled to an executable object as well using Cython or Nuitka.

Python was originally developed by Guido van Rossum, and has developed into two major branches: Python 2.x.x and Python 3.x.x. Eventually, the two major branches will merge into one. Currently, most operating systems, the Mac OS, and Linux in particular, ship with Python 2.x.x. as the default, and most legacy Python libraries and applications were developed in the 2.x.x dialect, but most of what I use is also available in the Python 3.x.x branch, so I am now mostly using this branch.

An extensive collection of Python tutorials and examples is available on the WWW, and by far the best way to learn Python, or any other programming language for that matter, is to use it.

 

Installation on Windows


Unlike Linux and the Mac OS, Windows doesn't come with Python pre-installed.  Moreover, Python was developed on the Unix and Linux platforms, so many of it's dependencies originate on these platforms as well.  This situation created significant challenges for the end user.  Fortunately, dedicated work of the last several years has addressed these difficulties to the extent that now it's trivial to get Python up and running on Windows by using one of the "full-stack" options discussed below.

If you want access to the latest versions of all of the thousands of Python modules available from https://www.python.org/, while a bit more challenging to implement than the full-stack options, with a little care, it's relatively easy to do these days.  Here's how I installed Python along with my favorite modules on a Windows 7 machine:

  1. First, install the latest C++ development tools for Windows.  I used: https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017 This step allows Python to compile many of the modules and components that it needs, especially those involving numerical computation. 
  2. Download the installer for the latest version of the Python branch you want to install – I used the python-3.6.2-amd64.exe installer – and then execute it to install Python.  Be sure to choose the "Add to Path" option.  You can always add Python and it's dependencies to the path later, but this is a bit complicated, and should be avoided if possible.  I accidentally installed the 32-bit version of Python, and had to uninstall it before installing the 64-bit Python, and I was never offered the add-to-path option during the second attempt, so I did it manually by, more or less following the steps decribed here: https://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7.
  3. Update the Python installer, PIP, from the command line (open a CMD terminal, by entering cmd in the "Search programs and files" text box under the Start menu, and then enter pip install -U pip on the command line.  This should update the PIP installer or return that you've already got the latest version.
  4. Next, install the following crucial packages:
  5. If you plan to use the LaTeX typseting engine to add typeset symbols to your plots, you'll need to install the LaTeX typsetting engine and modify the Path variable as I described above.  I used the MikTeX distribution's basic installer: basic-miktex-2.9.6361-x64.exe.  This version of MikTeX only installs the bare minimum number of packages at first, and then fetches and installes the ones it needs to compile your document the first time you typeset it.  Hence, the first time you typeset a document, it will take a while to complete, but will be much faster for any future documents using the same LaTeX packages.  Once you've got MiKTeX installed, you can update the installed packages Ior install new ones) using the MiKTeX Admin Package Manager, which is installed when you iinstall MiKTeX. 

You should then be good to go! 

What if you've messed up somewhere, like I did, and need to fix things manually?

I had to create a PYTHONPATH environmental variable by right-click opening Computer from the Start menu, and selecting Properties, and then Advanced tab.  In the Advanced tab, select Environmental Variables, and create the new PYTHONPATH variable under System Variables as decribed at the link given in 2. above.  My PYTHONPATH variable contains: C:\Program Files\Python36\python;C:\Program Files\Lib;C:\Program Files\Python36\DLLs;C:\Program Files\Python36\Lib\lib-tk;C:\Program Files\Python36\include;C:\Program Files\Python36\libs;C:\Program Files\Python36\Scripts;C:\Program Files\Python36\tcl;C:\Program Files\Python36\Tools;

 As I mentioned earlier, PIP couldn't find the compiler needed to build parts of scipy and also numpy, so I Googled a bit and found this website: \url{http://www.manejandodatos.es/2013/10/error-installing-new-python-packages-windows-7/, which directed me to Christoph Gohlke's "Unofficial Windows Binaries for Python Extension Packages" website: \http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy-cluster.  I installed Christoph's pre-compiled binaries for  numpy (the larger numpy-1.13.1+mkl-cp36-cp36m-win\_amd64.whl or else it won't work) and scipy, and then also installed his version of matplotlib as well for good measure, but I don't think this was necessary.   These are wheel files (.whl) which PIP can install without invoking a compiler. 

Once I did the above, I had no trouble installing additional modules such as numba.  And my example python codes using matplotlib with LaTeX typesetting worked perfectly!

What IDE should I use?

Once you've got PIP up to date, you can install Spyder: pip install spyder.  Spyder is a very nice IDE which incorporates the IPython command interface which behaves much like MATLAB.  However, before Spyder will work, you have to install pyqt5:  pip install pyqt5, which is a python wrapper for the ubiquitously used QT C++ graphics and widgits library.  Spyder requires pyqt5 to function correctly.  Without pyqt5, entering spyder on the command line in the cmd app will try to launch Spyder, but fail after a second or two with no error messages generated.  I installed pyqt5 after installing Spyder, but it would probably be better to install it first. 

Two Things to Note During This Process

  1. After installing any applications using a Windows installer, you usually have to reboot your computer.
  2. After modifying any environmental variables, you'll at least have to close and reopen a new cmd instance.

Python Links

In the following, the term 'stack' refers to a collection of applications, such as an editor, package manager, debugger, and source control interface, and also a set of Python function libraries. The main open source repository of Python codes is maintained by https://www.python.org/ which provides a command-line based package manager called PIP to provide install and uninstall functionality. Other organizations, such as Enthought and Continuum Analytics provide their own packages. Enthought provides proprietary packages of their own as well.

The Enthought Canopy Python stack (Free for students and faculty and seems to work well on both Windows and Mac) Enthought no longer supports Canopy. See: https://support.enthought.com/hc/en-us/articles/360038600051-Canopy-GUI-end-of-life-transition-to-the-Enthought-Deployment-Manager-EDM-and-Visual-Studio-Code

The Continuum Analytics Anaconda Python stack is free and open source to all.

The Matplotlib Plotting Library is included in both the Canopy and Anaconda stacks. Check out the Gallary. The SciPy Scientific Library contains a comprehensive library of numerical algorithms with functionality similar to that found in MATLAB. The entire SciPy library is provided in both the Canopy and Anaconda stacks.

Continuum Analytics provides the Anaconda Python Stack (Links to an external site.). This stack comes with a package manager (Conda), and Spyder IDE, but no debugger (last time I checked). Continuum Analytics was co-founded by Travis Oliphant who was the primary developer of NumPy and SciPy. I was primarily using the Anaconda stack on my MacBook Pro, but have recently switched to my own Python stack assembled using Home Brew. I also use Canopy on a Windows 7 machine. On my Linux machines, I use the standard python.org libraries and PIP package manager. My current opinion is that the jury is still out regarding which approach is best. Update: I've been using the python.org approach on my Mac for a few years now, and am pretty comfortable with it.

My latest advance is to finally incorporate one of the Python virtual environment systems; in my case, I've chosen to use pyenv. Virtual environment systems enable you to switch between any number of Python versions on the same machine – globally. In other words, you can switch between the 3.x.x. and 2.x.x branches and any older versions of each at will from the command line. How this works is explained on the GitHub repository, but the main concept to be aware of is that the entire Python stack for a given version will be installed from python.org in your home directory. You can also install a Python stack into any subdirectory you like, and limit its scope to that directory and those under it. This is helpful for application developers who plan on distributing their Python app. In this way, developers can specify particular versions of any Python module included.

One hickup using pyenv I discovered recently was that matplotlib, while working perfectly within the Spyder IDE, failed when the containing Python code was executed on the command line. The error complained about requiring a Python 'framework' to be installed. I found the fix quickly through a Google search here.

A few of my Python codes

Additional Resources

Python Tutorials by Mokhtar Ebrahim