There are several ways to start a Python program, which will result in different values for the module search path sys.path
.
View v3.8.2 version
--Python setup and usage, 1. Command line and environment. Https://docs.python.org/ja/3/using/cmdline.html
Put simply
--If you specify a file etc. directly, the directory related to it comes to the beginning of sys.path
--If you specify a module with -m
, the current directory becomes the first element.
--In either case, the module name is the string " __main__ "
.
The summary is as follows. The author didn't know that he could launch directories (packages) and .zip files.
python Arguments after |
What is done | Its module name(__name__ ) |
sys.path First element of |
---|---|---|---|
<.py file> |
That file | __main__ |
<The directory where the file is located> |
<directory> |
<directory>/__main__.py |
__main__ |
<directory> |
<.zip file> |
.zip file Inside__main__.py |
__main__ |
<.zip file> |
-m <module> |
<module>.py |
__main__ |
Current directory(Empty string) |
-m <package> |
<package>/__main__.py |
__main__ |
Current directory |
-c <command> |
__main__ |
Current directory | |
- |
Script passed as standard input | __main__ |
Current directory |
-I
If you specify -I
in the boot options, you have specified -E -s
and the interpreter boots in ** isolated mode **.
At this time, the above ** first element is not added to sys.path
(due to the influence of -s
). ** **
So basically, you can (absolutely) import only modules installed with pip install
.
Note that even the module next to the execution script cannot be imported [^ 1].
[^ 1]: The module next to the execution script cannot be imported relative to it. This is because relative import from the __main__
module is not possible.
Recommended Posts