Skip to content

Python

Introduction

The Python programming environment is being used more and more for education and research at the Delft University. This manual provides some information about Python and related software. Also instructions are given for setting up a Python environment on your computer.

The focus for the installation and maintenance will be around the conda system (see below). This has been chosen by the MI and CI group as the default installation and packaging system for Python. Depending on your needs you can select Miniconda or Anaconda to install this system. Read here for the instructions.

Python environment

“Python is an interpreted, high-level, general-purpose programming language” [Wikipedia]. Its functionality can easily be extended through the use of packages. Python can be used interactively - where every command is interpreted and executed per line - and with scripts. The scripts can be viewed as the Python program you are developing. Eventually executing the script will also be done in the same way as interactively: line-by-line. For every program (Python script) that is executed a new instance of Python is started with its own isolated environment. Default separate programs cannot access each others memory. These programs can run at the same time next to each other.

IPython is a command line program which provides a more powerfull interactive environment for Python. It includes graphics and allows for fast and convenient testing of Python code. It is also the core of the Jupyter project.

Jupyter is a web-based application for running Python scripts (and other languages like Julia, R and more…) in a notepad environment. Jupyter also provides an easy to use filemanager to manage the scripts etc..

Spyder is an GUI IDE for programming in Python.

Conda is a very convenient package manager and virtual environment system to main all you Python packages and other python related systems. It also allows you to create a separate virtual environment for each project thereby resolving possible conflicts with for instance conflicting packages or versions thereof.

Miniconda is an installer for the conda system. This is a CUI and provides you with base installation for conda.

Anaconda is also an installer for the conda system and provides the conda application in a GUI. Also major Python related applications are also installed and provided by the GUI. Compared to Miniconda this installation is much larger.

Python packages

As noted before python packages extend the functionality of Python. Most of the packages are written in Python and maintained by enthusiastic developers. Typically these packages are imported at the start of a program. A small example is given:

import datetime as dt

print(dt.datetime.now())
2019-02-06 14:46:25.492227

This very small python program first imports a package named datetime and makes a reference to this package with the name dt. In the print-command the function datetime.now() [give the current date and time] from this package is called. More information about this package can be found here: https://docs.python.org/3/library/datetime.html

Installation

For the installation of Python and Python-packages you can choose between Miniconda and Anaconda. Both systems will install the conda system where Miniconda will provide you with a minimal python environment and Anaconda will give you a full Python environment included with popular packages and related software like Jupyter and Spyder. Afterwards its very easy to extend the installation with other packages and software. The Miniconda installer will install the conda system for use in a command-line interface. Afterwards it allows you to also install GUI software like Jupyter and Spyder. The Anaconda installer installs the conda system with a GUI. If you prefer a lean installation and are at ease with the command-line you may want to use the Miniconda installer. If you however want a easy-to-use GUI and a more complete installation (as in huge) try out the Anaconda installer.

The instructions on how to install Miniconda can be found in the miniconda manual. For Anaconda follow the instructions on the Anaconda website:

Note

with both installations always select an user-based install. This means on all OS’s that the software will be installed in your home directory and will be only accessible to your account. Experience learns that system-based installation tend to give problems with access rights to installed packages.

Using Python

Because Python is an interpreted programming language you will always need to run your program with Python. You write your program as a script which — when run with Python — is executed line-by-line. Your script can not be compiled and executed as a stand-alone application.

Note

The following instructions are written for Linux and macOS

To show you how to run your program we will create a small example script and store that as test.py:

print('Hello, World!')

Followed by the following instruction on the command-line:

$ python test.py
Hello, World!

There is a way to automate this process by extending your script:

#!/usr/bin/env python
print('Hello, World!')

On the first line this construct with #! is called a shebang which tells the kernel to run the file with the command that follows the shebang. In this case /usr/bin/env is executed with the argument python. In short env will check which version of Python is the default and will run the script file with that version. Finally you allow users to stand-alone run the Python script by setting the execute-bit of the file with chmod:

$ chmod u+x test.py

Tip

read the man-page for more information: $ man chmod

Now you can execute (not really, it is still interpreted by Python) your script:

$ ./test.py

Note

Notice the ./ prefix which tells your OS to execute the file found in your current (denoted by the single dot .) directory.

Editors

Because a Python scripts is ‘just’ plain text it can be written in any text editor that can store plain text files. You might however would like to have a more specialized editor for programming in general (IDE or Python in particular. As mentioned earlier the Spyder package is part of the Anaconda environment is is a nice Python IDE. A very powerfull alternative is PyCharm with a helpfull git integrations for maintaining your code directly in a git repository. An academic license is available:

Finally you should have a look at Visual Studio Code. This free open source editor from Microsoft can be easily extended with Python extensions (among many other packages) and also has very nice features like integration with git. The editor is available on every platform.