© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
A. DanialPython for MATLAB Developmenthttps://doi.org/10.1007/978-1-4842-7223-7_2

2. Installation

Albert Danial1  
(1)
Redondo Beach, CA, USA
 

Installing MATLAB is pretty straightforward: download the installation package from the MathWorks’ website, run the installer, then either authenticate with your MathWorks credentials or provide licensing information. Installing Python is equally easy on Windows and Linux, but installing the recommended Anaconda distribution on macOS is more involved because it requires one to also install the Xcode development environment. Finally, configuring a Python installation that MATLAB can use seamlessly is harder still. In this chapter, I’ll cover installation steps, explain why Python configuration is more problematic than MATLAB, then show how to create an installation that can run the examples in this book.

2.1 Downloads

Python installers for macOS and Windows are freely available from python.​org . Linux and macOS distributions generally come with Python preinstalled.

It is best to avoid using a Python installation that comes with your operating system, however. If you install new modules or upgrade existing ones, you might break functionality your OS relies on. You are best off leaving the OS-provided Python version alone.

Regardless of your computer’s operating system, my recommendation is to download and install the Anaconda Python distribution from anaconda.​com. Unless you are making one installation for multiple people, install it into a directory or folder that you own. Unlike the “vanilla” Python installer from python.​org, Anaconda comes with NumPy, SciPy, matplotlib, Pandas, and many other modules of interest to scientists and engineers. Anaconda also includes the Spyder IDE, which, while not as capable as the MATLAB IDE, has useful editing, debugging, and profiling options. Spyder even includes a MATLAB theme.

2.1.1 Match Your Python and MATLAB Versions!

If you plan to call Python from MATLAB (explained in Chapter 6), you’ll need to take care to install a version of Python supported by your version of MATLAB. Visit the MathWorks’ website1 to see which Python versions your release of MATLAB supports. You may need to go to Anaconda’s archive2 area to get an older distribution if the current one is too new for your MATLAB version.

This book uses MATLAB 2020b and Python 3.8.8.

2.1.2 Verify That Python Runs

After you’ve installed Python, open a terminal window on Linux or macOS or an Anaconda terminal on Windows and type
> python --version

The result should show Python 3.8.8—or higher, if your MATLAB version supports newer releases.

2.2 Post-Install Configuration and Checkout

The following instructions assume you’re using the Anaconda Python distribution.

The first step after installing the downloaded bundle from anaconda.org is to update the installation with the latest security updates and packages. The easiest way to do that is to issue the command conda update in a terminal (pick an “Anaconda Terminal” on Windows). The command will print an error message containing the command that will actually work. It looks like this on Linux:
(base) > conda update
CondaValueError: no package names supplied
# If you want to update to a newer version of Anaconda, type:
#
# $ conda update --prefix /usr/local/anaconda3/2020.07 anaconda
and this on Windows:
(base) C:>conda update
CondaValueError: no package names supplied
# If you want to update to a newer version of Anaconda, type:
#
# $ conda update --prefix C:ProgramDataAnaconda3 anaconda

The correct update commands for these particular installations are therefore

Linux:
conda update --prefix /usr/local/anaconda3/2020.07 anaconda

and

Windows:
conda update --prefix C:ProgramDataAnaconda3 anaconda

After the update, run ipython either from the command line on Linux and macOS or an Anaconda Prompt terminal on Windows to start a read-evaluate-print-loop (REPL) similar to MATLAB’s console-like interactive environment.

Our first step will be to make a simple variable assignment and then invoke the whos command to see what’s in your environment:

MATLAB:

Python:

>> a = 1;

>> whos

  Name Size Bytes Class

  a    1x1  8     double

In : a = 1

In : whos

Variable Type Data/Info

----------------------------

a        int  1

Already we see a difference—the Python a is an integer scalar, while the MATLAB a is a two-dimensional array, sized 1 x 1, with a type of double.

This highlights a conceptual difference between the two languages: MATLAB’s default data container is a double-precision matrix, while Python’s is a scalar whose type is inferred from the value on the right-hand side. Chapter 4 covers MATLAB and Python data containers in depth.

2.3 Creating and Running a Python Program

A Python program consists of Python source code in a text file. The file name does not have to have a .py extension, but this is the most common practice. There are several ways to run a Python program:
  • On all operating systems, open a terminal which has the Python executable on the path, then type python (or python3) followed by the name of the source file.

  • On Windows, create an association between the .py file extension and the full path to the Python executable. (In general, this is not a good idea since it will complicate your life when you try to switch virtual environments which will be discussed later.)

  • On Linux and macOS, add a pound-bang line pointing to your preferred Python executable and then make the file itself executable with the chmod command:

> chmod a+rx my_program.py
When they are invoked as commands, Unix-like operating systems examine the first line of executable text files for the characters #!—the pound-bang, or shebang. If these exist, the operating system invokes the rest of the first line to interpret the remaining lines in the file. Examples are
#!/usr/bin/env python3
#!/usr/bin/python3
#!/apps/python/3.8.8/bin/python

The first of these runs the env command to select the first python3 executable in your $PATH. Doing so is considered a best practice.

Pound-bang lines are ignored on Windows.

Interestingly, the path to the MATLAB executable can also be used on the pound-bang line to run MATLAB code as a stand-alone batch script:
#!/apps/matlab/2018a/bin/matlab -nojvm -nodisplay -nosplash -nodesktop
a = rand(3);
b = rand(3);
fprintf('Random eigenvalues:');
eig(a,b)

2.4 The Curse of Choice

MATLAB enjoys a fundamental advantage over Python with its hegemony of toolboxes. The fact that there is only one Optimization Toolbox means you don’t have to decide which to use.

Freedom of choice is great, but in the world of open source software, choice adds configuration and installation complexity often summarized as “dependency hell.” As a trivial example, consider a software package M that depends on Z version 2.4. You download M and Z 2.4 and install them. Later, you also need package N which depends on an older version of Z, version 2.1. All is well if your software configuration system allows multiple versions of the same package; you just need to install both versions of Z and make sure M uses Z v2.4 while N uses Z v2.1. If you simply replace Z v2.4 with Z v2.1, N will continue working, but you’ll break M.

Scale this small scenario by two or three orders of magnitude, include forced package and operating system updates for security reasons, and you’ll have a sense of the difficulty involved in maintaining large software packages.

MATLAB’s seamless updates and toolbox installations look especially appealing in light of Python’s module installation and update challenges.

2.5 Virtual Environments

To simplify package and plug-in installation, many open source languages—Python, Perl, Ruby, Node, R, and Rust, to name a few—come with package managers that determine which dependencies are needed to install a requested package, then recursively download and install those dependencies. The challenge for Python’s package managers, pip, or conda if you’re using the recommended Anaconda distribution, is that a Python installation permits only one version of a package or module. This means Python modules with conflicting dependencies cannot coexist in the same installation! Well, technically they can coexist on the file system, but modules lacking compatible dependencies will fail, leading to a broken Python installation.

Python’s solution to the problem of module dependency conflicts is to support the creation of, and easy switch between, multiple shared installations known as virtual environments . A virtual environment looks and acts like an independent Python installation even though under the hood common libraries and modules are shared with other environments. Each virtual environment has its own copy of modules that could conflict with those in other environments.

2.5.1 matpy, the Virtual Environment Used in This Book

For the most part, MATLAB works well with any Python version supported for that release. In my case, the successful pairing is MATLAB 2020b and Python 3.8. There are two problematic cases though. First, the Python GeoPandas module depends on a prodigious list of shared libraries which have MATLAB counterparts. On Linux, this can be overcome by matching GLIBC libraries in the virtual environment with that used by MATLAB. Success on Windows and macOS can be a hit-or-miss proposition. I was able to use GeoPandas from MATLAB on a Windows laptop successfully for several months until I applied security updates, after which importing GeoPandas failed with a DLL load error.

The second case involves loading compiled Python modules into MATLAB, as done in Section 14.​12, to boost MATLAB’s performance. On Linux, MATLAB can only import such modules if they are compiled in an environment with a compatible version of GLIBC.

It is a credit to the conda developers that such a fundamental dependency challenge can be overcome. I was able to create a virtual environment with an older version of GLIBC to match MATLAB’s simply by “pinning” versions of the needed libraries. With this change, MATLAB can use GeoPandas without issues. This command creates the virtual environment matpy which allows one to run all Python and MATLAB+Python examples in this book:

Linux (Ubuntu 20.04):
conda create --name matpy python=3.8.8 libgcc-devel_linux-64=8.4.0 
   libgcc-ng=8.4.0 geopandas matplotlib pulp cartopy austin faker 
   pint poliastro psycopg2 pymongo pythran redis redis-py simanneal 
   netCDF4 descartes h5py statsmodels pyyaml psutil lxml dask 
   distributed paramiko sympy requests pyflakes uncertainties seaborn 
   cython pytest scikit-umfpack ipython -c conda-forge
conda activate matpy
pip3 install scalene
Windows 10:
conda create --name matpy python=3.8.8 geopandas matplotlib pulp ^
   cartopy faker pint poliastro psycopg2 pymongo pythran redis ^
   redis-py statsmodels simanneal netCDF4 descartes h5py pyyaml ^
    psutil lxml dask distributed paramiko sympy requests pyflakes ^
    cython uncertainties seaborn pytest ipython -c conda-forge
conda activate matpy
pip3 install wxPython scalene
macOS (Catalina, Big Sur):
conda create --name matpy python=3.8.8 geopandas matplotlib pulp 
   cartopy austin faker intel-openmp=2021.2.0 llvm-openmp=10.0.0 
   libllvm10=10.0.1 llvmlite=0.36.0 
   pint poliastro psycopg2 pymongo pythran redis redis-py simanneal
   netCDF4 descartes h5py statsmodels pyyaml psutil lxml dask 
   distributed paramiko sympy requests pyflakes uncertainties seaborn
   cython pytest ipython -c conda-forge
conda activate matpy
pip3 install scalene

The MATLAB command must be started from a terminal session which has matpy activated, for example:

console:
conda activate matpy
matlab

2.5.2 Commands to Manage Virtual Environments

The following conda commands3 use the matpy virtual environment as an example:

List existing virtual environments
conda env list
Create a virtual environment
conda create --name matpy
Create a virtual environment using a YAML file
conda env create -f environment.yml
Create a virtual environment with pinned versions
conda create --name matpy python=3.8.8 libgcc-devel_linux-64=8.4.0 libgcc-ng=8.4.0
Create a virtual environment in a specified directory
conda create --prefix /path/to/env/name matplotlib=3.1 numpy=1.16
Show where virtual environments are installed
conda info --envs
Activate a virtual environment
conda activate matpy
Deactivate the current virtual environment
conda deactivate
Search for a package
conda search libgcc-devel_linux-64
List packages in a virtual environment
conda list -n matpy
Delete a virtual environment
conda remove --name matpy --all

2.5.3 Keeping Your Virtual Environment Current

Anaconda updates their packages frequently. To stay current with security updates and bug fixes, run conda update periodically. The conventional way to do this is with

console:
(matpy) $ conda update --all

However, don’t do this yet! Our matpy conda environment was created with pinned versions of modules whose shared libraries need to be consistent with their MATLAB counterparts. Without taking precautions, update --all may install newer versions of shared libraries that cause MATLAB to fail when it runs Python code.

The necessary precaution requires adding a file called pinned in the relevant conda environment’s conda-meta directory . Where is this directory? Output from conda info will tell you the location. conda-meta is in the directory shown for active env location. For example:

console:
(matpy) $ conda info
      active environment  :  matpy
     active env location  :  /usr/local/anaconda3/2020.07/envs/matpy
             shell level  :  2
        user config file  :  /home/al/.condarc
             ..lines deleted..

Therefore, for this particular environment, the necessary file is /usr/local/anaconda3/2020.07/envs/matpy/conda-meta/pinned. Create this file with a text editor and add lines to match pinned entries that were used when creating the matpy environment (see Section 2.5.1). On macOS, pinned would contain

macOS:
python=3.8.*
intel-openmp=2021.2.*
llvm-openmp=10.0.*
libllvm10=10.0.*
llvmlite=0.36.*

The asterisks in the third numeric position means any value may be used. It’s safe to run conda update --all after this file is in place.

Note

conda environments like matpy are easy to create, modify, experiment with, and destroy. Once you have an environment you want to keep for a while, remember to add a pinned file before you update it.

2.6 ipython, IDEs

MATLAB is more than a language; its IDE is an integral part of the overall MATLAB experience. MATLAB developers learning Python may be disappointed that an equally capable IDE does not exist for Python.While several powerful Python IDEs do exist--PyCharm, Visual Studio Code with Microsoft’s excellent Python extension, and Spyder--only Spyder provides an experience resembling MATLAB’s IDE. Rather than explaining Spyder’s use in this book, please see the book’s Github repository, https://github.com/Apress/python-for-matlab-development, for links to videos showing how MATLAB IDE interactions such as editing, debugging, and plotting are done with Spyder. For the text in this book, I will use the ipython REPL which comes with each of the three distributions mentioned in the previous section. While the python command itself is a REPL, its user interface is primitive compared to ipython’s.

After verifying that you have a suitably recent version of Python (Section 2.1.2), start an ipython session either by typing ipython at the command line (Linux, macOS) or selecting an iPython session through the application navigator (on Windows: Start/Programs/).
> ipython
In : import numpy
In : import matplotlib.pyplot
In : import scipy.interpolate
In : import pandas
In : import statsmodels
In : import h5py
In : import dask

We’ll use many additional modules, but if these import without errors, your installation will be sufficient for rigorous numerical analysis.

2.6.1 Autoload Modules When ipython Starts

MATLAB will automatically run commands in startup.m in the directory identified by the command userpath . This is useful for adding directories to your MATLAB search path, setting format to something other than the default and so on.

ipython has a similar mechanism to run commands at the start of each session. This is even more necessary in Python than MATLAB because most Python sessions begin with module imports that should be done every time; importing these automatically is a terrific convenience. First identify the ipython “profile” directory:
In : import IPython
In : IPython.paths.locate_profile()
Out: '/home/al/.ipython/profile_default'
ipython start-up commands can be added to any file ending with .py in the startup directory below the profile directory. If multiple .py files exist there, they are executed in alphabetical order. I’ve customized my ipython start-up with the file
/home/al/.ipython/profile_default/startup/00-start.py
which contains
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot, show, scatter, imshow, figure, savefig

2.7 Python and MATLAB Versions Used in This Book

All Python code in this book used 3.8.8 from Anaconda 2020.07.

All MATLAB code used Release 2020b. This version of MATLAB predates Python 3.9 which was released in October 2020 and therefore does not support 3.9.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.157.106