Skip to content

Commit 7cf4cae

Browse files
authored
Merge pull request #152 from braboj/h4pb9y-codex/add-relative-import-example
Add relative import and fix absolute import
2 parents db9e3bf + dc3330a commit 7cf4cae

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ You can also run the examples from the command line. For example, to run the
6868
python examples/A01_introduction/hello_world.py
6969
```
7070

71+
To see how relative imports work within packages, run the `import_relative.py` example using the `-m` flag:
72+
73+
```bash
74+
python -m examples.A21_import_system.import_relative
75+
```
76+
7177

7278
## License
7379

examples/A21_import_system/import_absolute.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Shows how to enforce and use absolute imports.
22
# ------------------------------------------------------------------------------
33
# Example script demonstrating absolute imports.
4+
from __future__ import absolute_import
5+
46
# Using absolute imports
57
import asyncio
68

7-
# Enforce absolute imports
8-
from __future__ import absolute_import
9-
109
# Absolute imports
1110
from foo.api.submodule1 import func1
1211
from foo.core.submodule2 import func2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Shows how to use relative imports within a package.
2+
# ------------------------------------------------------------------------------
3+
# Demonstrates importing modules from the current package using relative syntax.
4+
5+
from .foo.api.submodule1 import func1
6+
from .foo.core.submodule2 import func2
7+
# Relative imports are scoped to packages.
8+
9+
# Call the imported functions
10+
func1()
11+
func2()

0 commit comments

Comments
 (0)