Other labs: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.

Přeložit do češtiny pomocí Google Translate ...

Lab #8 (Apr 6 – Apr 10)

Before class


  • Package management in Python.
  • pip and virtualenv.


Make sure you have virtualenv, pip and Python 3 installed. Installing pylint is also recommended ;-). Hint.
Update your clone of teaching/nswi177/2020-summer/upstream/examples as we will be using it again. Hint.

Investigate the contents of builds/simple directory. The Python program uses dateparser module to parse human readable dates, such as 1 day and 2 hours ago.

Make sure you understand the whole program before continuing with the next exercise.


Try running the timestamp2iso.py program.

Unless you have already installed the python3-dateparser package system-wide, it should fail with ModuleNotFoundError: No module named 'dateparser'. The chances are that you do not have that module installed.

If you have installed the python3-dateparser, uninstall it now and try again (just for this demo). But double-check that you would not remove some other program that may require it.


We could now install the python3-dateparser with DNF but we would opt for installing things locally instead.

We could also install it globally with pip install dateparser but you should not install global-wide programs and libraries by other means than via your package manager.

That way, we will not cluter the system and keep things more organized.

But you already know that as you have read the articles above, right?

Print which directories are on your $PATH now. Hint.
Create virtual environment for this program and activate it. Hint.
Again, look at your $PATH. How it differs? What about your prompt? Solution.

Run the program in the virtual environment you just activated.

Will it work?

Let’s now install dateparser in the virtual environment (check that you still have the environment active by looking at your prompt). Hint.
Run the program again. It ought to work now ;-). Hint.

When there are more dependencies to install, it is better to store them in a file. By convention, the file is named requirements.txt.

Run pip install -r requirements.txt to use the file instead. What is its output?

Deactivate the virtual environment. Check again $PATH and $PS1. Hint.
From now on, whenever you code in Python, always prepare a virtual environment and always put dependencies into requirements.txt. Become a good Pythonista.
Let’s move into builds/rest project.
Prepare a new virtual environment for this project. Do not forget to deactive the old one if you have not done yet so.
Look at the required imports and prepare the requirements.txt file with the dependencies.
Install the dependencies from the requirements.txt file and check that the program can be executed.
Let’s move into builds/install project now. It is basically the simple one again but with some extra distribution-related features.
First of all, prepare a new virtual environment for this project and activate it. Install the dependencies.
For proper distribution, we have moved the source code into a separate module. Investigate the matfyz/ subdirectories and refresh why __init__.py is needed (even when it is empty).

To launch the program now, we need to execute it in its modular form, that is via

python -m matfyz.nswi177.timestamp2iso 1 day ago

Look inside the setup.py file. It contains information about your program and can be used for easy installation of your program.

Try running

./setup.py build && ./setup.py install

What has happened?


Look inside <your-virtual-env-directory>/bin/timestamp2iso. This is automatically created wrapper for your program. Notice that it does not have any filename extension and (when installed system-wide) looks like a normal program.

Not bad for about 60 lines of code ;-)


While the work for creating setup.py may seem to complicate things a lot, it actually saves time.

Virtually any Python developer would be now able to install your program and have a clear starting point when investigating other details.

Note that if you have installed some program via DNF system-wide and that program was written in Python, somewhere inside it was setup.py that looked very similar to the one you have just seen. Only instead of installing the script into your virtual environment, it was installed globally.

There is no other magic behind it. Really.


Have you noticed that there is file named .gitignore in this directory? Probably not, it is a hidden file, after all.

Find out what is this file good for.

Prepare setup.py for the builds/rest program too. It is really not necessary to remember the contents of setup.py: just remember where to find a suitable template and that is all :-)