Python Programming Environments

As discussed in Chapter 1, there are several ways to establish a good programming environment that uses Python:

  1. We can download and install a Python distribution, such as the Anaconda Distribution and then use the IPython command line shell that comes with that distribution. This allows the user to execute Python interactively, one code line at a time.

  2. Install a Python distribution, such as Anaconda, that comes with a Jupyter server then use Jupyter notebooks to compose executable code and text blocks.

  3. Install a Python distribution, such as Anaconda, then use a plain text editor (notepad, gedit, TextEdit) application to compose code and then execute the code from the command line using the appropriate command line console for your operating system.

  4. Install a Python distribution, then install an IDE application (PyCharm, Spyder, VSCode) that bundle a smart text editor, an interactive console, and enhanced debugging tools into a nice GUI.

  5. Use a cloud computing platform such as CoCalc, pythonanywhere, or Google Colab that allows the user to use Jupyter notebooks without installing any software, except for a browser. The cloud service maintains the Python distribution.

The last method was discussed in detail in Chapter 1. We will consider the other methods here.

Installing a Python Distribution

There are several Python distributions used for scientific and engineering application development. The most common ones include:

We will focus on the Anaconda distribution here. Setting up a working programming environment based on the Anaconda distribution should be straightforward.

  1. Navigate to the Anaconda download page.

  2. Fill in your email information and submit.

  3. Choose the download for your operating system.

  4. After the installer downloads, open it up and follow the instructions. Accept the default locations for files.

Certainly, one major benefit of programming in Python is the large number of packages available. These packages, such as matplotlib, allow for developing powerful scientific applications, but they are not all preinstalled when you perform the default installation of your distribution. You will need to install the packages to be used by this textbook.

Each package will have a set of dependencies (other required packages). To ensure that these dependencies are also installed a package manager app is used. Anaconda comes with a package manager called conda. Instead of using conda directly, we will use a conda graphical user interface app called Navigator, which is installed with the Anaconda distribution. Navigator will help you to manage the installed packages without learning all the details of the conda app.

Locate the Navigator app that was installed with the Anaconda distribution and start it up. Figure 1 shows the Navigator home screen. The rectangular application tiles are additional apps that can be installed using Navigator. We will ignore these apps for now.

Figure 1: Navigator home screen.

Now click on the Environments tab. Some Python packages have inconsistent dependencies. To avoid this problem, the Anaconda distribution allows separate programming environments to be set up depending on the project you are working on. These programming environments exist in separate file structures on your computer and are isolated from each other. Figure 2 shows the default Environments screen. By default, there is one environment set up labelled “base (root)”. While we can add packages to this environment, if we want, a better practice is to create a separate environment for each major programming project. We will create an environment that should work for all the examples and problems in the textbook.

Figure 2: Default Environment screen.

To create a new environment,

  1. Click on the “Create” tool at the bottom of the Environments screen.

  2. Type in an appropriate name, such as “CompSciBook”, and select the version of Python you wish to use. The default Python version should be acceptable for now.

  3. Select “Not installed” from the package filter drop down menu at the top.

  4. Search for the matplotlib, which is the first package listed in Table A1‑1.

  5. Select the package

  6. Click on “Apply”.

Before repeating this process for the other packages, you should add an additional channel for Navigator to use.

  1. Click on Channels

  2. Type in conda-forge

  3. Click on Update channels

Now repeat the above process for adding packages for the other packages that are not already installed. You will find that installing matplotlib will automatically install numpy, since it is a dependency.

Table 1: Required packages for textbook problems.
Package
matplotlib
numpy
pandas
scipy
seaborn
statsmodels
numdifftools

There are several ways to execute Python code using the created environment. One method is to start Navigator, go to the Environments page, and select the green arrow, pointed out in Figure 3.

Figure 3: The execute Python button is shown by the arrow.

Clicking the arrow tool gives you access to several choices for running Python. The Terminal gives you access to the operating system command prompt, which will allow you to execute a Python program from a file. The IPython selection opens up the IPython interactive interpreter from which you can execute individual Python commands.

Using Jupyter Notebooks with The Anaconda Distribution

Jupyter Notebook is an application for interactive code development and commentary. The computational notebook format is a file that has cells for executable code and output and cells for text that can be formatted using the Markdown language. The notebooks open in a browser where the executable code is run on a cloud-based server or alternatively on your computer, if the appropriate Jupyter Notebook application has been installed.

When the Anaconda distribution is installed on your computer, the Jupyter Notebook application should be installed by default and will execute code from a browser using the base environment. One way to create a Jupyter Notebook file is to

  1. start Navigator

  2. go to the Environments window

  3. click on the base environment

  4. click on the green arrow for the environment drop-down menu

  5. select “Open with Juptyer Notebook”, as shown in Figure 4.

Figure 4: The Navigator environments window. The arrow points out the selection to start a Jupyter notebook using the base environment.

To create a Jupyter notebook that uses another environment, such as the CompSciBook environment, use Navigator to install the Jupyter package into the selected environment. You can now start a Jupyter Notebook session from selected environment tab in Navigator.

Use the Spyder IDE with the Anaconda Distribution

As the complexity of the coding project increases, there will likely come a time when it is advantageous to shift the programming environment away from computational notebooks (Jupyter Notebook, Google Colab notebooks, etc.) towards a more robust integrated development environment (IDE) that includes a code smart text editor, an interactive test run environment, and enhanced debugging tools. One such IDE that comes with the Anaconda distribution is the Spyder app that will be installed automatically in the base environment. The Spyder documentation offers a good overview of the basic features available (Spyder Doc Contributors, 2024). Figure 5 shows the default opening screen.

a.

b.

c.

d.
Figure 5: :Default Spyder windows. a) Toolbar (outlined in red); b) Text Editor; c) Help Window; d) IPython console.

The Spyder text editor will color Python key words and variable names that have been used. It will check syntax for errors.

One method of using Spyder with a different environment is to use Navigator to install the Spyder package in the appropriate environment. A link to the specific version of Spyder will be added n the application listing. You can also change the environment used by Spyder in the Preferences tab.

References

ActiveState Software Inc. (2024). ActiveState Python Distribtution. ActiveState. https://www.activestate.com/products/python/

Anaconda, Inc. (2024). Anaconda Distribution. Anaconda. https://www.anaconda.com/download

Enthought, Inc. (2024). Enthought Downloads. https://assets.enthought.com/downloads/

Spyder Doc Contributors. (2024). Quickstart—Spyder 5 documentation. https://docs.spyder-ide.org/current/quickstart.html