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

Topic

  • Package management in Python.
  • pip and virtualenv.

Exercises

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

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.

4.

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.

5.

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?

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

Run the program in the virtual environment you just activated.

Will it work?

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

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?

13.
Deactivate the virtual environment. Check again $PATH and $PS1. Hint.
14.
From now on, whenever you code in Python, always prepare a virtual environment and always put dependencies into requirements.txt. Become a good Pythonista.
15.
Let’s move into builds/rest project.
16.
Prepare a new virtual environment for this project. Do not forget to deactive the old one if you have not done yet so.
17.
Look at the required imports and prepare the requirements.txt file with the dependencies.
18.
Install the dependencies from the requirements.txt file and check that the program can be executed.
19.
Let’s move into builds/install project now. It is basically the simple one again but with some extra distribution-related features.
20.
First of all, prepare a new virtual environment for this project and activate it. Install the dependencies.
21.
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).
22.

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
23.

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?

Hint.
24.

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 ;-)

25.

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.

26.

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.

27.
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 :-)