[英]How to import modules from parent directory/different package?
Working on a project that I cloned from GitHub, I came across the problem that import
statements about modules in parent directories or different packages weren't recognized throwing ImportError
.在处理我从 GitHub 克隆的项目时,我遇到了一个问题,即无法识别有关父目录或不同包中的模块的
import
语句并抛出ImportError
。 I posted this question about my specific occasion and continued researching about it.我发布了这个关于我的特定场合的问题,并继续研究它。
Finally, I've come to a conclusion that I would like to share along with some resources for similar future problems.最后,我得出了一个结论,我想与一些资源一起分享,以解决类似的未来问题。
import
a module from a parent directory or from a different package ?import
模块?(or just some information about the question) (或只是有关该问题的一些信息)
According to python docs , importing a module works like this:根据python docs ,导入模块的工作方式如下:
When a module named spam is imported, the interpreter first searches for a built-in module with that name.
当导入名为 spam 的模块时,解释器首先搜索具有该名称的内置模块。 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 给出的目录列表中搜索名为 spam.py 的文件。 sys.path is initialized from these locations:
sys.path 从这些位置初始化:
- 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).
PYTHONPATH(目录名称列表,语法与 shell 变量 PATH 相同)。
- The installation-dependent default.
安装相关的默认值。
So, in the case that you simply want to import
a module, eg my_pkg.my_module.py
, which resides in a different location than the module importing it, my_pkg
should be in the list that sys.path
returns .因此,如果您只想
import
一个模块,例如my_pkg.my_module.py
,它与导入它的模块位于不同的位置, my_pkg
应该在sys.path
返回的列表中。 For this to happen, there are (as far as I know) three ways:为此,有(据我所知)三种方式:
PYTHONPATH
variable.PYTHONPATH
变量。 (as pointed out here ) - Not preferred sys.path
at runtime .sys.path
。 (as pointed out here ) - Not preferred setup.py
file that resolves the import dependencies of your modules (and doing many other important things, too) .setup.py
文件来解决你的模块的导入依赖(以及做许多其他重要的事情) 。 - Proper way So, the first 2 ways above are generally considered not to be a good practice , but rather a temporary - bad solution that will lead to further problems during development process.所以,上面的前两种方法通常被认为不是一个好的做法,而是一个临时的坏解决方案,会在开发过程中导致进一步的问题。 The third one, though, is the best way to build a distributable , well-organized project that will be easily used by other people.
然而,第三种方法是构建一个可分发的、组织良好的项目的最佳方式,该项目将很容易被其他人使用。 Some of the benefits of
setup.py
file are illustrated here along with whole functionality of it. 此处说明了
setup.py
文件的一些好处以及它的全部功能。
With a setup.py
file you can clearly configure which are the packages that contain the modules to be imported to solve ImportError
and do many other useful things about your project.使用
setup.py
文件,您可以清楚地配置哪些包包含要导入的模块,以解决ImportError
并为您的项目做许多其他有用的事情。
Below are cited two very illustrative tutorials about how to use import statements and how to create a setup.py
file, that I strongly recommend to take a look at:下面引用了两个非常有说明性的教程,关于如何使用import 语句以及如何创建
setup.py
文件,我强烈建议您看一下:
EDIT: Also, you can find plenty of information about how to configure, package and distribute your project here:编辑:此外,您可以在此处找到有关如何配置 package 和分发您的项目的大量信息:
Personally, I find it very useful in order to build a pretty good first setup.py
file.就个人而言,我发现它对于构建一个非常好的第一个
setup.py
文件非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.