Exercises#
These exercises are meant to prepare the DTU Python supporters for a variety of things that they may encounter.
It also covers a bit more than what the courses expects, we need to be prepared for a variety a things.
Note
These exercises are intended with low guidance, we strive to let our supporters be explorative, and that they ensure they know where to find information.
As such, supporters should be able to find the information on-line if they encounter problems they have not experienced before. Or, use their peers to get a solution. So please use search engines for solving the problems.
Best practices#
After all steps of an installation/upgrade of packages it is important to understand and ensure that the environment is behaving as expected.
There can be numerous paths to confirm this, here are some check-points that can be useful:
check the
pip
/conda
list of packages and ensure that the package version and package is installed as requested.open up a Python shell and ensure it can be imported
ensure the module’s version after import is as requested. Most likely the module has a variable:
<modulename>.__version__
which can be checked for current imported version.do the above points also in their editor to ensure that their editor does not launch in a virtual environment.
For instance, after having installed numpy==1.22
one can check:
pip list
# or "grep"'ing for a specific package
pip list | grep numpy
conda list
# or "grep"'ing for a specific package
conda list | grep numpy
Once the package is listed in the package list, check version and import:
python3 -c "import numpy ; print(numpy.__version__)
this will fail with errors if it can not import it, and if it succeeds, it will
print the version, if the variable (__version__
) is part of the package.
As will be clear in Exercise 4, the above test is also important in the IDE that the questioner is working with.
Warning
Please always use a virtual environment (pip
/conda
) when performing these
exercises. It ensures that you do not mess up your own installation.
Exercise 1#
Install the official Python distribution.
Windows only: If your terminal will not recognize the
python
executable, most likely your forgot to check theAdd ... to PATH
. It might be a good idea if you also try and install Python like this (forcefully) to know how the error message looks.Install conda in its minimal installation (MiniConda).
In the semester 2023; the recommended usage will be
pip
. Likely there will be some students with a priorconda
installation. It is thus important that you know how to have aconda
installation and navigate a regular Python installation.Create a new environment in a local folder.
Understand that:
Environments created in local folders are not automatically listed when listing available environments
How to activate a local environment.
How to install packages into a local environment
Packages can be installed from an external command, such as
conda install -n <path to environment> <packages>
,Packages will be installed directly if the environment is already the activated one:
conda activate <path to environment>
How can you see if you are in a
conda
environment?Ensure you can navigate between both the
conda
and the official Python installation.
Hint
The command python -c "import sys ; print(sys.exec_prefix)"
can give a hint at
which Python interpreter is being used.
Exercise 2#
Create two virtual environments, using venv.
# first environment
numpy=1.24
matplotlib=3.6
# second environment
numpy=1.23
matplotlib=3.6
Check that you can easily swap between these two environments.
Note
One cannot copy paste the above in pip
, ensure you change the package specification
to match the package installers terminology.
Note
If you have gone through all exercises, then do this again with conda
!
Exercise 3#
Install any package, and figure out its location in the file-system. It is important you check against an importable package.
Hint
__file__
pip
Exercise 4#
For the currently known recommend IDE’s:
In order of priority, if time is limited, only do the first.
VS Code (please also read the page linked for some additional information)
Spyder
PyCharm
Figure out the following:
How to swap environment (interpreter) in the IDE instead of the default Python executable
How to check the packages that are installed (some IDE’s allows calling Pip directly in the
IPython
console)
Exercise 5#
Run through the 02002 installation instructions. Then run through these extra steps: https://lab.compute.dtu.dk/cp/02002students/-/wikis/testing
It would also be great to test some of these things in a Jupyter Notebook, to see how well they fare in a more constrained environment.
Exercise 6#
Go to pip dependencies and provoke the output
shown, in a virtual environment. Understand all output of the commands, especially the warnings and errors.
Use pip check
as well.
Also resolve the pip check
errors.
Exercise 7 (not necessary)#
Complete exercise 2 using virtualenv
which works slightly different from venv
.
Exercise 8 (not necessary)#
Install the package pyparsing at a specific Git commit c8b7664
using pip
.