February 2022
Intermediate to advanced
274 pages
6h 28m
English
The Python search path is simply a list of directories Python stores in the sys.path variable. During any import statement, Python looks through the list for modules or packages matching the requested import. We can use a small test to see what sys.path looks like during a test run:
| | import sys |
| | |
| | |
| | def test_sys_path(): |
| | print("sys.path: ") |
| | for p in sys.path: |
| | print(p) |
When we run it, notice the search path:
| | $ pytest -s tests/test_sys_path.py |
| | ========================= test session starts ========================== |
| | collected 1 item |
| | |
| | tests/test_sys_path.py sys.path: |
| | /path/to/code/ch12/script_src/tests |
| | /path/to/code/ch12/script_src/src ... |
Read now
Unlock full access