如果import语句只包含文件名,python如何找到模块文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15252040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does python find a module file if the import statement only contains the filename?
提问by Asciiom
Everywhere I see Python code importing modules using import sysor import mymodule
我到处都看到 Python 代码使用import sys或import mymodule
How does the interpreter find the correct file if no directory or path is provided?
如果没有提供目录或路径,解释器如何找到正确的文件?
采纳答案by dm03514
http://docs.python.org/3/tutorial/modules.html#the-module-search-path
http://docs.python.org/3/tutorial/modules.html#the-module-search-path
6.1.2. The Module Search Path
When a module named
spamis imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file namedspam.pyin a list of directories given by the variablesys.path.sys.pathis 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 variablePATH).- The installation-dependent default.
Note: On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path.
After initialization, Python programs can modify
sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.
6.1.2. 模块搜索路径
当
spam导入命名的模块时,解释器首先搜索具有该名称的内置模块。如果未找到,它将搜索spam.py在变量给出的目录列表中命名的文件sys.path。sys.path从这些位置初始化:
- 包含输入脚本的目录(或未指定文件时的当前目录)。
PYTHONPATH(目录名称列表,语法与 shell 变量相同PATH)。- 依赖于安装的默认值。
注意:在支持符号链接的文件系统上,包含输入脚本的目录是在符号链接之后计算的。换句话说,包含符号链接的目录不会添加到模块搜索路径中。
初始化后,Python 程序可以修改
sys.path. 包含正在运行的脚本的目录位于搜索路径的开头,在标准库路径之前。这意味着将加载该目录中的脚本而不是库目录中的同名模块。这是一个错误,除非更换是有意的。有关更多信息,请参阅标准模块部分。
For information on the "installation-specific default", see documentation on the sitemodule.
有关“安装特定的默认”的信息,请参阅文件的site模块。
回答by alestanis
Python has a path variable just like the one you have inside your terminal. Python looks for modules in folders inside that path, or in the folder where your program is located.
Python 有一个路径变量,就像你在终端中的那个变量一样。Python 在该路径内的文件夹或程序所在的文件夹中查找模块。
回答by Silas Ray
It uses the PYTHONPATH, set as an environment variable, to find packages (folders containing __init__.pyfiles) and modules (or, if already loaded once, retrieves the module object from sys.modules).
它使用设置为环境变量的PYTHONPATH来查找包(包含__init__.py文件的文件夹)和模块(或者,如果已经加载一次,则从 中检索模块对象sys.modules)。
回答by reptilicus
Also, you can see what the current path is by using the sysmodule
此外,您可以使用sys模块查看当前路径是什么
import sys
print(sys.path)

