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
- Read the following 3 articles about pip and virtualenv. There is a lot of overlap but they will give you a lot of information that is relevant not only for Python development. Dead Simple Python: Virtual Environments and pip (dev.to), Python Virtual Environments: A Primer (realpython.com) and What Is Pip? A Guide for New Pythonistas (realpython.com).
Topic
- Package management in Python.
- pip and virtualenv.
Exercises
virtualenv
, pip
and Python 3 installed.
Installing pylint is also recommended ;-).
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?
$PATH
now.
Hint.$PATH
. How it differs? What about your prompt?
Solution.dateparser
in the virtual environment
(check that you still have the environment active by looking at your prompt).
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?
requirements.txt
.
Become a good Pythonista.
requirements.txt
file
with the dependencies.
requirements.txt
file and check
that the program can be executed.
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?
Hint.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.
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 :-)