Skip to content

Commit 04f1543

Browse files
committed
Cleans up the tree and fixes doctest ImportError
1 parent 725ea72 commit 04f1543

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

ch04packaging/03Packaging.ipynb.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,23 @@
8585
#
8686
# ```
8787
# repository_name
88-
# |-- src
89-
# | `-- package_name
90-
# | |-- __init__.py # optional; required for exporting things under package's namespace
91-
# | |-- python_file.py
92-
# | |-- another_python_file.py
93-
# `-- tests
94-
# |-- fixtures
95-
# | `-- fixture_file.yaml
96-
# `-- test_python_file.py
97-
# |-- LICENSE.md
98-
# |-- CITATION.md
99-
# |-- README.md
100-
# `-- pyproject.toml
88+
# ├── src
89+
# │   └── package_name
90+
# │      ├── __init__.py # optional; required for exporting things under package's namespace
91+
# │      ├── python_file.py
92+
# │   └── another_python_file.py
93+
# ├── tests
94+
# │ ├── fixtures
95+
# │ │   └── fixture_file.yaml
96+
# │   └── test_python_file.py
97+
# ├── docs
98+
# │ ├── index.rst
99+
# │ ├── conf.py
100+
# │   └── ...
101+
# ├── LICENSE.md
102+
# ├── CITATION.md
103+
# ├── README.md
104+
# └── pyproject.toml
101105
# ```
102106
#
103107
#
@@ -546,20 +550,14 @@ def process():
546550
# %%
547551
# %%writefile greetings_repo/tests/test_greeter.py
548552

549-
import os
553+
from pathlib import Path
550554

551555
import yaml
552556

553557
from greetings.greeter import greet
554558

555559
def test_greet():
556-
with open(
557-
os.path.join(
558-
os.path.dirname(__file__),
559-
'fixtures',
560-
'samples.yaml'
561-
)
562-
) as fixtures_file:
560+
with open(Path(__file__).parent / 'fixtures' / 'samples.yaml') as fixtures_file:
563561
fixtures = yaml.safe_load(fixtures_file)
564562
for fixture in fixtures:
565563
answer = fixture.pop('answer')
@@ -607,21 +605,15 @@ def test_greet():
607605
# %%
608606
# %%writefile greetings_repo/tests/test_greeter.py
609607

610-
import os
608+
from pathlib import Path
611609

612610
import pytest
613611
import yaml
614612

615613
from greetings.greeter import greet
616614

617615
def read_fixture():
618-
with open(
619-
os.path.join(
620-
os.path.dirname(__file__),
621-
'fixtures',
622-
'samples.yaml'
623-
)
624-
) as fixtures_file:
616+
with open(Path(__file__).parent / 'fixtures' / 'samples.yaml') as fixtures_file:
625617
fixtures = yaml.safe_load(fixtures_file)
626618
return fixtures
627619

@@ -647,6 +639,15 @@ def test_greeter(fixture):
647639
# cd greetings_repo
648640
# pytest --doctest-modules
649641

642+
# %% [markdown]
643+
# The `ImportError` appears here `pytest` requires `__init__.py` files to discover test cases when using relative imports (though the library works fine without it).
644+
645+
# %% magic_args="--no-raise-error" language="bash"
646+
#
647+
# cd greetings_repo
648+
# touch src/greetings/__init__.py
649+
# pytest --doctest-modules
650+
650651
# %% [markdown]
651652
# Finally, we typically don't want to include the tests when we distribute our software for our users.
652653
# We can also add pytest as an "optional" dependency for the developers of our package.

0 commit comments

Comments
 (0)