From 52caef286f01456232d21e3b8a68b9a49d239f1b Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 11 Sep 2023 22:04:23 +0200 Subject: [PATCH] Add information about sys.path including the current directory --- programming/python/python_modules_primer.md | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/programming/python/python_modules_primer.md b/programming/python/python_modules_primer.md index c42f2a3..365f05a 100644 --- a/programming/python/python_modules_primer.md +++ b/programming/python/python_modules_primer.md @@ -124,3 +124,36 @@ __all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo", ``` `/usr/lib64/python3.9` contains the modules in [the Python standard library](https://docs.python.org/3/library/). + +### Importing your Python files + +If you create a file with the `a.py` name: + +``` +def f(): + return 2 +``` + +, and another with the `b.py` name: + +``` +import a + +print(a.f()) +``` + +, then: + +``` +$ python b.py +2 +``` + + +This works, because `sys.path` contains `''`, which means "the current directory". + +(`sys.path` is very similar to the `PATH` variable. However, `sys.path` contains the current directory by default, whereas `PATH` does not.) + +When `import a` is executed, then Python searches the directories in `sys.path` for an `a.py` file; it is found when checking the `''` path. +When `import datetime` is executed, Python searches in the current directory (because `''` comes first in the path), doesn't find it, but then finds it in the following `/usr/lib64/python3.9` directory. +Python iterates over the `sys.path` directories, and loads the *first* matching file. -- 2.47.3