Skip to content

Commit 1b9fc23

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 8a0751b + 7cf4cae commit 1b9fc23

File tree

10 files changed

+94
-3
lines changed

10 files changed

+94
-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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Using a custom module defined in the same directory
2+
# ------------------------------------------------------------------------------
3+
# A *module* is simply a Python file that defines variables, functions, or
4+
# classes. This example imports ``mymodule`` and calls its functions. Modules
5+
# help organize related code and can be reused across projects.
6+
7+
import mymodule
8+
9+
print(mymodule.greet('World'))
10+
print('Area of circle:', mymodule.area_circle(3))
11+
print('Module name:', mymodule.__name__)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Using built-in Python modules
2+
# ------------------------------------------------------------------------------
3+
# Python ships with a rich *standard library* of modules that you can use
4+
# without installing anything extra. This example demonstrates ``math`` and
5+
# ``random``.
6+
7+
import math
8+
import random
9+
10+
print('Square root of 16 is', math.sqrt(16))
11+
print('Value of pi is', math.pi)
12+
print('Random number between 1 and 6:', random.randint(1, 6))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Importing from a package
2+
# ------------------------------------------------------------------------------
3+
# A *package* is a directory containing an ``__init__.py`` file and one or more
4+
# modules. Packages allow related modules to be grouped together. This example
5+
# imports the ``hello`` function from ``my_package`` defined in the same
6+
# directory.
7+
8+
from my_package import hello
9+
10+
hello()

examples/A10_modules/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Examples related to Python modules and packages
2+
# ------------------------------------------------------------------------------
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Example package with a single submodule
2+
# ------------------------------------------------------------------------------
3+
# Packages are directories containing an ``__init__.py`` file. This file is
4+
# executed when the package is imported and can expose objects from submodules.
5+
# Packages allow modules to be organized hierarchically.
6+
7+
from .submodule import hello
8+
9+
__all__ = ['hello']
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Submodule inside ``my_package``
2+
# ------------------------------------------------------------------------------
3+
# This module defines the ``hello`` function. ``__init__.py`` re-exports this
4+
# function so it can be imported directly from ``my_package``.
5+
6+
7+
def hello():
8+
print('Hello from submodule')

examples/A10_modules/mymodule.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Example custom module with variables and functions
2+
# ------------------------------------------------------------------------------
3+
# Modules are Python files that can define variables, functions and classes.
4+
# They allow code to be organized into reusable components. When a module is
5+
# imported, its top-level code executes once.
6+
7+
PI = 3.14159
8+
9+
10+
def greet(name):
11+
"""Return a greeting string."""
12+
return f"Hello, {name}"
13+
14+
15+
def area_circle(radius):
16+
"""Return the area of a circle with the given radius."""
17+
return PI * radius * radius
18+
19+
20+
if __name__ == '__main__':
21+
# Code executed only when running this module directly
22+
print(greet('module'))
23+
print('Area:', area_circle(2))

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)