2 KiB
2 KiB
Exercise 2: Importing
Goal
Get comfortable with different ways of importing objects from a package.
Preparation
-
[if not done already] fork the repository
https://github.com/ASPP/2023-heraklion-ODD and clone it to the laptop in a new directory
-
You will be editing files within this directory
-
Open a terminal and change to this directory, so that you can run the scripts that you are editing
Tasks
-
Work on the file
brewing/example_usage_within_package.py
:- import and call the
make_example_potion
function from the modulebrew_potions.py
function ➔ open a terminal, change to thebrewing
directory and run the scriptexample_usage_within_package.py
: what happens? - change the import statement so that you can call the
make_example_potion
function like this:br.make_example_potion
➔ run the script again: what happens?
- import and call the
-
Work on the file
example_usage_outside_package.py
, which is in the directory where you cloned the repo:- import the brewing package inside the
example_usage_outside_package.py
that lives at the top level directory of the repo ➔ in the terminal, change to the top level directory of the repo, add the import statement and run the scriptexample_usage_outside_package.py
: what happens? - import and call only the
make_example_potion
function from the modulebrew_potions.py
, which is in thebrewing
directory ➔ run the script again: what happens? - does it work? if yes, why? If no, why not?
- change the import statements so that it works, then check whether
brewing/example_usage_inside_package.py
from 1.2 still works.
- import the brewing package inside the
-
Work on the file
scripts_and_notebooks/example_usage_different_folder.py
:- try to import and call only the
make_example_potion
function from the modulebrew_potions.py
➔ in the terminal, change to thescripts_and_notebooks
directory and run the scriptexample_usage_different_folder.py
: what happens? - does it work? if yes, why? If no, why not?
- try to import and call only the