]> xn--ix-yja.es Git - alex.git/commitdiff
Add information about sys.path including the current directory
authoralex <alex@pdp7.net>
Mon, 11 Sep 2023 20:04:23 +0000 (22:04 +0200)
committeralex <alex@pdp7.net>
Mon, 11 Sep 2023 20:04:23 +0000 (22:04 +0200)
programming/python/python_modules_primer.md

index c42f2a31ed2ccb4c562e119484b984e1d4986d23..365f05a295277e1a1718489bf9f7ae13c5237619 100644 (file)
@@ -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.