I've been writing in Python for 4 years, and I still don't fully understand the import system. I have found that relative imports tend to fail even if you do them exactly by the book. This is especially true when I am working in Jupyter Lab/Notebook.
Many have recommended appending to the sys.path
variable to overcome this issue (although I don't know why the imports don't just work as advertised). So I decided to read up on how this whole sys.path
thing works and read that it is initialized like so:
6.1.2. The Module Search Path
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified).
- PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
- The installation-dependent default.
What does the first bullet mean? Notice the lack of "input script" in the preceding paragraph of the section. Is it referring to spam.py
? If so, why use the word "input". I don't see any inputting of anything happening.
Can anyone clear up this exceptionally vague/ambiguous language?
question from:
https://stackoverflow.com/questions/65945451/what-does-the-directory-containing-the-input-script-mean 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…