Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

python - What does "The directory containing the input script" mean?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From what I can see, the first bullet is referring to the directory containing the script that you are trying to run. More specifically, it's referring to the script that is trying to import the module.

Python "code" is simply a script running on top of an interpreter. You "input" the script into the interpreter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...