[英]Confused how to import modules in python
If I want to use a 3rd party module like a python s3 module (boto http://boto.s3.amazonaws.com/index.html ) or http://developer.amazonwebservices.com/connect/entry.jspa?externalID=134 . 如果我想使用第三方模块(例如python s3模块)(boto http://boto.s3.amazonaws.com/index.html )或http://developer.amazonwebservices.com/connect/entry.jspa?externalID = 134 。
Once I download the .py files, what do I do? 下载.py文件后,该怎么办?
Where does the python interpreter look when I import a module? 导入模块时python解释器在哪里?
Is there a 'lightweight' way of installing a module so it makes deploying to a server easier? 是否有“轻巧”的模块安装方式,从而使部署到服务器更容易?
Look in the import statement reference , a long and involved description. 查看import语句参考 ,这是一个冗长而复杂的描述。 The simple method is to include the module's location in
sys.path
. 简单的方法是将模块的位置包括在
sys.path
。
I'm quoting the starting paragraph only: 我仅引用开始段落:
Once the name of the module is known (unless otherwise specified, the term “module” will refer to both packages and modules), searching for the module or package can begin.
一旦知道模块的名称(除非另有说明,否则术语“模块”将同时指代软件包和模块),就可以开始搜索模块或软件包。 The first place checked is
sys.modules
, the cache of all modules that have been imported previously .首先检查的是
sys.modules
, 这是先前已导入的所有模块的缓存 。 If the module is found there then it is used in step (2) of import.如果在那里找到模块,则将其用于导入步骤(2)。
If the module is not found in the cache, then
sys.meta_path
is searched (the specification forsys.meta_path
can be found in PEP 302).如果在缓存中没有找到模块, 然后
sys.meta_path
搜索 (对于本说明书sys.meta_path
可以在PEP 302中找到)。 The object is a list of finder objects which are queried in order as to whether they know how to load the module by calling theirfind_module()
method with the name of the module.该对象是查找程序对象的列表,这些查找程序对象通过调用带有模块名称的
find_module()
方法来查询它们是否知道如何加载模块。 If the module happens to be contained within a package (as denoted by the existence of a dot in the name), then a second argument to find_module() is given as the value of the path attribute from the parent package (everything up to the last dot in the name of the module being imported).如果模块恰好包含在包中(由名称中的点表示),则将find_module()的第二个参数作为父包中path属性的值给出(直到导入模块名称中的最后一个点)。 If a finder can find the module it returns a loader (discussed later) or returns None.
如果查找程序可以找到模块,则它将返回加载程序(稍后讨论)或返回无。
If none of the finders on
sys.meta_path
are able to find the module then some implicitly defined finders are queried.如果
sys.meta_path
上的所有sys.meta_path
均无法找到该模块,则将查询一些隐式定义的查找器。 Implementations of Python vary in what implicit meta path finders are defined.Python的实现在定义隐式元路径查找器方面有所不同。 The one they all do define, though, is one that handles
sys.path_hooks
,sys.path_importer_cache
, andsys.path
.他们都定义了一个处理
sys.path_hooks
,sys.path_importer_cache
和sys.path
。
It looks in the directory that you are currently working from. 它会在您当前正在使用的目录中查找。 For example, if you are trying to import it from inside
/test/my_file.py
, you can place the module in /test/
and simply import module_name
例如,如果您尝试从
/test/my_file.py
内部导入它,则可以将模块放在/test/
,只需import module_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.