简体   繁体   English

Python路径查找要导入的模块/类

[英]Python pathing to find modules/classes to import

I am new to Python and I am looking to find out how Python discovers the path to modules it imports from. 我是Python的新手,我想了解Python如何发现从中导入模块的路径。 This would be equivalent to CLASSPATH in Java and PERL5LIB in Perl. 这将等效于Java中的CLASSPATH和Perl中的PERL5LIB

Eg the import block in a script I am looking at looks something like this: 例如,我正在查看的脚本中的import块看起来像这样:

import os
import resource

from localnamespace.localmodule import some_class

I understand that os and resource are native to Python (are part of the core language API) but still the interpreter must have some pointer where to find them. 我知道osresource是Python固有的(是核心语言API的一部分),但是解释器仍然必须具有一些指针才能在哪里找到它们。 As for localnamespace.localmodule , how do we tell the interpreter where to find this module because it the directory in which this script is does not have a subdirectory named localnamespace . 至于localnamespace.localmodule ,我们如何告诉解释器在哪里可以找到该模块,因为该脚本所在的目录中没有名为localnamespace的子目录。

TLDR TLDR

In summary, the search process goes something like: 总之,搜索过程类似于:

1) Previously imported in sys.modules ? 1)以前导入sys.modules吗?

2) If no, can I find it in the script / interpreter directory? 2)如果否,我可以在脚本/解释器目录中找到它吗?

3) If no, can I find it in any of the directories in the PYTHONPATH environment variable? 3)如果否,我可以在PYTHONPATH环境变量的任何目录中找到它吗?

4) If no, ImportError 4)如果否, ImportError

The Longer Answer 更长的答案

Referring to the documentation an import statement first looks at sys.modules which is a dictionary of currently or recently loaded modules. 参考文档, import语句首先查看sys.modules ,它是当前或最近加载的模块的字典。

It it can't find the module there it searches through sys.meta_path - the actual paths here vary between implementations. 它无法找到通过sys.meta_path搜索的模块-此处的实际路径因实现而异。 In general, import paths will be defined in sys.path , which is a list of directories including those in the environment variable PYTHONPATH . 通常,导入路径将在sys.path定义,该目录是目录的列表,包括环境变量PYTHONPATH中的目录。

The documentation for sys.path describes itself as: sys.path的文档将其自身描述为:

A list of strings that specifies the search path for modules. 字符串列表,用于指定模块的搜索路径。 Initialized from the environment variable PYTHONPATH, plus an installation-dependent default. 从环境变量PYTHONPATH初始化,再加上与安装有关的默认值。

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. 在程序启动时初始化后,该列表的第一项path [0]是包含用于调用Python解释器的脚本的目录。 If the script directory is not available (eg if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. 如果脚本目录不可用(例如,如果交互式调用解释器或从标准输入中读取脚本),则path [0]为空字符串,该字符串将引导Python首先搜索当前目录中的模块。 Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH. 请注意,由于PYTHONPATH的结果,在插入条目之前插入了脚本目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM