Disclaimer

This documentation is intended for industrial engineering students at the Vrije Universiteit Brussel.

Motivation

Engineering Programming is more about teaching a fast prototyping tool than programming paradigms. Such tools are needed if the bottleneck is programming time or if solutions that maybe won’t even be successful needs to be tested. This documentation uses the Python programming language as a fast prototyping tool. The main reason is that the language has less overhead syntax than conventional ones. For example, printing "Hello, world!" in Java is:

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world!"); } }

While in Python:

print("Hello, world!")

The simplicity of Python comes with a price. Python is a dynamically typed language meaning that values are checked during execution. A poorly typed Python operation might cause the program to halt or signal an error at run time. In contrast, e.g. C# is a statically typed language in which programs are checked before being executed. A poorly typed C# program will be rejected before it starts. Most enterprises cannot offer software that may halt at run time and prefers to use statically typed programming languages over dynamic langages. This makes Python less enterprise-friendly than e.g. C#.

Python is also referred to as a weakly typed language. This generally means that the language has loopholes in the type system and that the type system can be subverted by invalidating any guarantees. A strongly typed language is the inverse thereof.

Strong typed does not mean statically typed, e.g. the C language has static typing since the code is type checked at compile time but there are many type loopholes. You can pretty much cast a value to any type of the same size.

Python has poor performance due to its very dynamic nature. In short, you write less but Python has more work. For example, the same n-body simulation takes 850 seconds in Python, 26 seconds in JavaScript, 22 in Java and 8 seconds in C++. Luckily, if performance is an issue, Python has libraries that under-the-hood uses efficient programming languages for faster execution. The following figure shows the performance of several programming languages for scientific computing.

micro benchmark from julialang.org

While it isn’t the fastest, Python maintains concise code which reduces the time to spend programming considerably. Example of how to set from matrix A all smallest values to 0 without for loop using the NumPy Python library:

A[A == np.amin(A)] = 0

Just imagine doing the same in C++.

Finally, while Python is not the most popular programming language it has an incredible growth since 2012:

StackOverflow Survey 2017

The more people use a programming language the more documentation, libraries and job opportunities there will be.

The above-cited features together with the increasing popularity of Python makes it a good choice for fast prototyping.

Installation

Anaconda is data science and machine learning platform for the Python programming language that is used in this documentation. It is designed to make the process of creating and distributing projects simple, stable and reproducible across systems and is available on Linux, Windows, and OSX. Anaconda curates major data science packages for Python. It comes packaged with Anaconda navigator for a GUI experience for managing libraries, Spyder for an Integrated Development Environment (IDE), and much more.

The most recent major version of Python is Python 3, which we shall be using in this documentation. However, Python 2, although not being updated with anything other than security updates, is still quite popular. When installing Anaconda select the latest version of Python 3.

If Anaconda is installed correctly, no additional tweaks are needed throughout this documentation.

Windows

  1. Download the Anaconda installer.
  2. Start the installer.

To prevent permission errors, do not launch the installer from the Favorites folder. If you encounter issues during installation, temporarily disable your anti-virus software during install, then re-enable it after the installation concludes. If you installed for all users, uninstall Anaconda and re-install it for your user only and try again.

  1. Click next.
  2. Read the licensing terms and click “I agree”.
  3. Select an install for “Just me” unless you are installing for all users (which require Windows Administrator privileges) and click next.
  4. Select a destination folder to install Anaconda and click the next button.

Install Anaconda to a directory path that does not contain spaces or unicode characters. Do not install as Administrator unless admin privileges are required.

  1. Choose whether to add Anaconda to your PATH environment variable. We recommend not adding Anaconda to the PATH environment variable, since this can interfere with other software. Instead, use Anaconda software by opening Anaconda Navigator from the Start Menu.
  2. Choose whether to register Anaconda as your default Python. Unless you plan on installing and running multiple versions of Anaconda, or multiple versions of Python, accept the default and leave this box checked.
  3. Click the install button. If you want to watch the packages Anaconda is installing, click show details.
  4. Click the next button.
  5. Optional: To install Visual Studio Code, click the “install Microsoft VS Code” button. After the install completes click the next button. Or to install Anaconda without VS Code, click the skip button.
  6. After a successful installation you will see the “Thanks for installing Anaconda” dialog box.
  7. Verify the installation by opening Anaconda Navigator from your Windows Start menu. If Navigator opens, you have successfully installed Anaconda.

OSX

  1. Download the Anaconda installer.
  2. Answer the prompts on the introduction, read me and license screens.
  3. Click the install button to install Anaconda in your home user directory (recommended).
  4. OR, click the change install location button to install in another location (not recommended). On the destination select screen, select install for me only.

If you get the error message “you cannot install Anaconda in this location”, reselect “install for me only”.

  1. Click the continue button.
  2. Optional: To install Visual Studio Code, click the “install Microsoft VS Code” button. After the install completes click the continue button. Or to install Anaconda without VS Code, click the continue button.
  3. After a successful installation you will see the “installation was completed successfully” dialog box.
  4. Verify the installation by opening Anaconda Navigator from Launchpad. If Navigator opens, you have successfully installed Anaconda.

Linux

  1. Download the Anaconda installer.

  2. Answer the prompts on the introduction, read me and license screens.

  3. Enter the following to install Anaconda for Python 3.7:

    bash ~/Downloads/Anaconda3-5.3.0-Linux-x86_64.sh

Include the bash command regardless of whether or not you are using bash shell. If you did not download to your downloads directory, replace ~/Downloads/ with the path to the file you downloaded. Choose “install Anaconda as a user” unless root privileges are required.

  1. The installer prompts “In order to continue the installation process, please review the license agreement.” click enter to view license terms.
  2. Scroll to the bottom of the license terms and enter “yes” to agree.
  3. The installer prompts you to click “enter” to accept the default install location, CTRL-C to cancel the installation, or specify an alternate installation directory. If you accept the default install location, the installer displays PREFIX=/home/<user>/anaconda3 and continues the installation. It may take a few minutes to complete.
  4. The installer prompts “Do you wish the installer to prepend the Anaconda3 install location to PATH in your /home/<user>/.bashrc ?” Enter “yes”.

If you enter “No”, you must manually add the path. Otherwise Anaconda will not work.

  1. Optional: The installer describes Microsoft Visual Studio Code and asks if you would like to install VS Code. Enter “yes” or “no”. If you selected “yes”, follow the instructions on screen to complete the VS Code installation.
  2. Close and open your terminal window for the installation to take effect, or you can enter the command source ~/.bashrc.
  3. Verify the installation by opening Anaconda Navigator by typing anaconda-navigator in a terminal window. If Navigator opens, you have successfully installed Anaconda.

If you install multiple versions of Anaconda, the system defaults to the most current version, as long as you haven’t altered the default install path.

Spyder IDE

Spyder is a scientific environment written in Python, for Python, and designed by and for scientists, engineers and data analysts. It features a combination of the editing, analysis, debugging, and profiling functionality of a comprehensive development tool with the data exploration, interactive execution, deep inspection, and visualization capabilities of a scientific package. Furthermore, Spyder offers built-in integration with many popular scientific packages, including NumPy, SciPy, Pandas, IPython, QtConsole, Matplotlib, SymPy, and more.

Spyder IDE is included in Anaconda.

Create a project

To create a Project, click the “New Project” entry in the “Projects” menu, choose whether you like to associate a project with an existing directory or make a new one, and enter the project’s name and path:

create new project dialog box

Project explorer

Once a project is opened, the “Project Explorer” pane is shown, presenting a tree view of the current project’s files and directories. This pane allows you to perform all the same operations as a normal file explorer.

project explorer

Variable explorer

The variable explorer shows the namespace contents (all global object references, such as variables, functions, modules, etc.) of the currently selected session, and allows you to interact with them through a variety of GUI-based editors. For example, variables can be listed as:

variables explorer

Matrices can be shown as:

matrix explorer

Troubleshooting

Download problems

Cause

The Anaconda installer files are large (over 300 MB), and some users have problems with errors and interrupted downloads when downloading large files.

Solution

Download the large Anaconda installer file, and restart it if the download is interrupted or you need to pause it.

No shortcuts

After installing on Windows, in the Windows Start menu I cannot see Anaconda prompt, Anaconda Cloud and Navigator shortcuts.

Cause

This may be caused by the way Windows updates the Start menu, or by having multiple versions of Python installed, where they are interfering with one another. Existing Python installations, installations of Python modules in global locations, or libraries that have the same names as Anaconda libraries can all prevent Anaconda from working properly.

Solution

If start menu shortcuts are missing, try rebooting your computer or restarting Windows Explorer.

If that doesn’t work, clear $PYTHONPATH and re-install Anaconda. Other potential solutions are covered in the “Conflicts with system state” section of this blog post.

Failed to create menus or add PATH

During installation on a Windows system, a dialog box appears that says “Failed to create Anaconda menus, Abort Retry Ignore” or “Failed to add Anaconda to the system PATH.” There are many possible Windows causes for this.

Solution

Try these solutions, in order:

  1. Do not install on a PATH longer than 1024 characters.
  2. Turn off anti-virus programs during install, then turn back on.
  3. Uninstall all previous Python installations.
  4. Clear all PATHs related to Python in sysdm.cpl file.
  5. Delete any previously set up Java PATHs.
  6. If JDK is installed, uninstall it.

Conda: command not found

Cause

Problems with the PATH environment variable can cause “conda: command not found” errors or a failure to load the correct versions of python.

Solution

  1. Find the location of your Anaconda binary directory.
  2. In your home directory, in the .bashrc file, add a line to add that location to your PATH.
  3. Close and then re-open your terminal windows.

E.g. a user with the user name “bob” on a Linux machine whose Anaconda binary directory is ~/anaconda would add this line to the .bashrc file:

export PATH="/home/bob/anaconda/bin:$PATH"

Spyder errors

Cause

This may be caused by errors in the Spyder setting and configuration files.

Solution

  1. Close and relaunch Spyder and see if the problem remains.
  2. On the menu, select Start, then select Reset Spyder Settings and see if the problem remains.
  3. Close Spyder and relaunch it from the Anaconda Prompt:
    1. From the Start menu, open the Anaconda Prompt.
    2. At the Anaconda Prompt, enter Spyder.
    3. See if the problem remains.
  4. Delete the directory .spyder2 and then repeat the previous steps from Step 1. Depending on your version of Windows, .spyder2 may be in C:\Documents and Settings\Your_User_Name or in C:\Users\Your_User_Name.

Replace Your_User_Name, with your Windows user name as it appears in the Documents and Settings folder.

Managing packages

On the Navigator Environments tab, the packages table in the right column lists the packages included in the environment selected in the left column.

Packages are managed separately for each environment. Changes you make to packages only apply to the active environment.

Click a column heading in the table to sort the table by package name, description, or version.

Navigator Environments

The Update Index button updates the packages table with all packages that are available in any of the enabled channels.

Filtering the packages table

By default, only Installed packages are shown in the packages table. To filter the table to show different packages, click the arrow next to Installed, then select which packages to display: Installed, Not Installed, Upgradable or All.

Selecting the Upgradable filter lists packages that are installed and have upgrades available.

Finding a package

In the Search Packages box, type the name of the package.

Installing a package

  1. Select the Not Installed filter to list all packages that are available in the environment’s channels but are not installed.

Only packages that are compatible with your current environment are listed.

  1. Select the name of the package you want to install, or in the Version column, click the blue up arrow.

  2. Click the Apply button.

If after installing a new package it doesn’t appear in the packages table, select the Home tab, then click the Refresh button to reload the packages table.

Upgrading a package

  1. Select the Upgradable filter to list all installed packages that have upgrades available.

  2. Click the checkbox next to the package you want to upgrade, then in the menu that appears select Mark for Upgrade.

OR

  1. In the Version column, click the blue up arrow.

  2. Click the Apply button.

Installing a different package version

  1. Click the checkbox next to the package whose version you want to change.

  2. In the menu that appears, select Mark for specific version installation. If other versions are available for this package, they are displayed in a list.

  3. Click the package version you want to install.

  4. Click the Apply button.

Removing a package

  1. Click the checkbox next to the package you want to remove.
  2. In the menu that appears, select Mark for Removal.
  3. Click the Apply button.