In a recent issue of CEP, Dr. Jacob Albrecht published an interesting article with the title “Step Into the Digital Age with Python” (CEP, September 2021, pp 47 – 53). Dr. Albrecht gives an excellent overview of the many uses of Python in Chemical Engineering. He also provides several examples of typical engineering calculations and the packages used to solve them. The examples are captured in Jupyter Notebooks.
Notebooks, containing a mix of text and code, are great for documenting as well as running code while showing the results in tables and graphs. If you never used a Jupyter Notebook you should definitely try it. Dr Albrecht’s code is a very good start and freely available for download on GitHub (https://github.com/chepyle/Python4ChEs ). In order to follow along in this blog post, I would suggest you download the code from GitHub and put the folder Python4ChEs somewhere on your hard disk. However, in order to modify and run these notebooks yourself, you need to have Python installed along with the required packages. In this post I show how to do this both on a Mac (macOS) and a PC (Windows).
Background
Python is an interpreted language with a straightforward syntax and powerful handling of lists and dictionaries. You can write interesting programs with native Python, but its real strength comes from the seamless interface to thousands of packages. These packages are not part of a standard installation but must be installed and imported as needed. While most projects make use of a core set of packages, other projects need many more. Since some packages depend on other packages, there needs to be a mechanism for keeping track of dependencies and avoiding version conflicts.
Virtual Environments to the Rescue
A Virtual Environment (venv) is an elegant solution for avoiding possible problems with package conflicts. It is a mechanism to collect all packages needed for a project in one folder associated with one selected version of Python. After the environment is activated all packages subsequently installed onto the system end up in the active environment, respecting both the Python version and other packages already in the environment. This magic is performed by Python’s package installer and manager (pip). But before I show you how to set up and activate a virtual environment, let’s go through installation of Python itself.
Installing Python and the venv
There are several ways of getting Python on your system but I’m going to show the one I prefer and has worked well for me. It starts with a WEB browser directed to https://www.python.org/downloads/. You are presented with a page from which you can download a version of Python. The latest version is shown in a big yellow button where it says “Download Python x.xx.x”. At the time of writing this post, x.xx.x = 3.10.0. Now I deal with Mac OS and Windows separately since installation differs slightly.
MacOS
The download comes down as a package (python-3.10.0…pkg). I double click on this file and go through the installation. You will find that the installation goes directly into the “Applications” folder ready to be used. However, when you look there you might find that you also have other, older versions of Python installed. So which one is being used? I’ll show you how we select and decide.
At this point we open a Terminal window. Navigate into the Python4ChEs folder you created earlier. We are now creating an environment for this project. But first, check what version of python is being used. On my laptop it looked like this:
MacBook:python4ches myusername$ python –version
Where I typed what’s shown in Bold face. There is a good chance the answer comes back:
Python 2.7.16
or something like that. The reason being that MacOS comes loaded with Python but for older machines like mine it has version 2 which is not what you want. We will fix this with the following commands.
MacBook:python4ches myusername$ python3.10 -m venv env
This creates a Virtual Environment (venv) in the folder env. We will now activate this environment.
MacBook:python4ches myusername$ source env/bin/activate
Now if you type a $ python –version you’ll find that we are using the latest version. You will also see that the command prompt ($) is preceded by (env). On my MacBook it looks as follows:
(env) MacBook:python4ches myusername$
The first thing to do is to update the package manager (called pip) to the latest version:
(env) MacBook:python4ches myusername$ python -m pip install –upgrade pip
From this point on we can install packages that we need. We’ll start with two that I always use.
(env) MacBook:python4ches myusername$ pip install numpy
(env) MacBook:python4ches myusername$ pip install matplotlib
There is a file inside the python4ches folder called requirements.txt. It lists all the packages used for the project. You batch install packages directly from this file but I have found it straightforward to do it manually just as I showed for numpy and matplotlib. You then get the latest versions and pip magically keeps track of potential version conflicts between the packages.
After all the required packages are installed we have two more to do before we can use Jupyter Notebooks.
(env) MacBook:python4ches myusername$ pip install jupyter
This installs the Notebook app and all the files required for it. The very last install I do is not with pip but with python:
(env) MacBook:python4ches myusername$ python -m ipykernel install –name=env4che
This last command just assures that I get a “kernel” for a Jupyter notebook that is associated with my virtual environment.
Now I can start a notebook page by typing:
(env) MacBook:python4ches myusername$ jupyter notebook
It starts a local server and opens a page in my web browser. I can then navigate into the “Ten_problems” folder where I find a bunch of notebooks. Open any one of them and examine and execute. Make sure the “env4che” kernel is selected since this will assure that you use the packages in the correct environment (You can have many venv’s all with different packages and versions of python too).
Windows
Installation on Windows is very similar to that on MacOS with a few subtle differences that I now point out.
The downloading process downloads an .exe file like “python-3.10.0-amd64.exe”. Double click on this file and an installation window appears. Select the “Install Now” option. It installs Python in
C:\Users\myusername\Appdata\Local\Programs\Python\Python310
and says that Setup was successful.
Close the download window and open a terminal (Command prompt). Navigate to the python4ches folder and install the virtual environment the following way:
C:\users\myusername\python4ches>\users\myusername\appdata\local\programs\python\python310\python.exe -m venv env
Now activate the environment:
C:\users\myusername\python4ches> env\scripts\activate.bat
Go ahead and update pip.
(env) C:\users\myusername\python4ches> env\scripts\python.exe -m pip install –upgrade pip
From this point forward installation of both packages and jupyter are the same as for MacOS, in other words using pip install package_name.
Installation of an associated kernel for the jupyter notebook is done as follows:
(env) C:\users\myusername\python4ches> env\scripts\python.exe -m ipykernel install –name=env4che
Shutting down
After we are done running the the notebooks it is a good idea to shut down the local server. The command:
Control-C followed by y will stop the server and shut down all kernels.
To exit from the virtual environment we simply type:
deactivate
We are now back to a regular command prompt with no servers running.
Generalizing
You only have to install Python once (unless you specifically want another version than what you now use) but you can create as many virtual environments as you need. It is definitely a good idea to install a new venv for each major project you create. And it is a very good idea to have a separate environment for projects you download from GitHub. The reason is that each project will have its own set of requirements (e.g. as in requirements.txt) and might rely on specific versions of the various packages used. If you had all packages installed in one place on your system the risk for conflict is real. With separate environments you can come back to old projects and know that they work even if you might have installed new versions of several packages (in other venv’s) since then. If you create your environments as “env” along side of your project files you know which environment to activate for each project.
Summary
I have shown here how you can download Python and arrange packages needed for a project in separate folders called virtual environments. At first glance it might appear cumbersome to go through these steps for each new project you create, but believe me, you will be happy you did once you start collecting or creating new Python projects.