diff --git a/ch04packaging/03Packaging.ipynb.py b/ch04packaging/03Packaging.ipynb.py index c6119d74..24c99ea7 100644 --- a/ch04packaging/03Packaging.ipynb.py +++ b/ch04packaging/03Packaging.ipynb.py @@ -85,19 +85,23 @@ # # ``` # repository_name -# |-- src -# | `-- package_name -# | |-- __init__.py # optional; required for exporting things under package's namespace -# | |-- python_file.py -# | |-- another_python_file.py -# `-- tests -# |-- fixtures -# | `-- fixture_file.yaml -# `-- test_python_file.py -# |-- LICENSE.md -# |-- CITATION.md -# |-- README.md -# `-- pyproject.toml +# ├── src +# │   └── package_name +# │      ├── __init__.py # optional; required for exporting things under package's namespace +# │      ├── python_file.py +# │   └── another_python_file.py +# ├── tests +# │ ├── fixtures +# │ │   └── fixture_file.yaml +# │   └── test_python_file.py +# ├── docs +# │ ├── index.rst +# │ ├── conf.py +# │   └── ... +# ├── LICENSE.md +# ├── CITATION.md +# ├── README.md +# └── pyproject.toml # ``` # # @@ -546,20 +550,14 @@ def process(): # %% # %%writefile greetings_repo/tests/test_greeter.py -import os +from pathlib import Path import yaml from greetings.greeter import greet def test_greet(): - with open( - os.path.join( - os.path.dirname(__file__), - 'fixtures', - 'samples.yaml' - ) - ) as fixtures_file: + with open(Path(__file__).parent / 'fixtures' / 'samples.yaml') as fixtures_file: fixtures = yaml.safe_load(fixtures_file) for fixture in fixtures: answer = fixture.pop('answer') @@ -607,7 +605,7 @@ def test_greet(): # %% # %%writefile greetings_repo/tests/test_greeter.py -import os +from pathlib import Path import pytest import yaml @@ -615,13 +613,7 @@ def test_greet(): from greetings.greeter import greet def read_fixture(): - with open( - os.path.join( - os.path.dirname(__file__), - 'fixtures', - 'samples.yaml' - ) - ) as fixtures_file: + with open(Path(__file__).parent / 'fixtures' / 'samples.yaml') as fixtures_file: fixtures = yaml.safe_load(fixtures_file) return fixtures @@ -647,6 +639,15 @@ def test_greeter(fixture): # cd greetings_repo # pytest --doctest-modules +# %% [markdown] +# The `ImportError` appears here `pytest` requires `__init__.py` files to discover test cases when using relative imports (though the library works fine without it). + +# %% magic_args="--no-raise-error" language="bash" +# +# cd greetings_repo +# touch src/greetings/__init__.py +# pytest --doctest-modules + # %% [markdown] # Finally, we typically don't want to include the tests when we distribute our software for our users. # We can also add pytest as an "optional" dependency for the developers of our package.