[英]Python Imports, Paths, Directories & Modules
Let me start by saying I've done extensive research over the course of the past week and have not yet found actual answers to these questions - just some fuzzy answers that don't really explain what is going on. 首先让我说我已经对过去一周的过程进行了广泛的研究,但还没有找到这些问题的实际答案 - 只是一些模糊的答案并没有真正解释发生了什么。 If that's just cause I missed what I was looking for, I'm sorry - please just point me in the correct direction.
如果这只是因为我错过了我想要的东西,我很抱歉 - 请指出我正确的方向。
My directory structure is: 我的目录结构是:
TestProject/
runtest*
testpackage/
__init__.py
testmod.py
testmod2.py
testsubs/
testsubmod.py
A couple notes: 几个笔记:
Things I've observed: 我观察到的事情:
And the questions: 问题是:
Thanks in advance. 提前致谢。
First off, you will find all the information you need in section 6 of The Python Tutorial . 首先,您将在The Python Tutorial的第6部分找到所需的所有信息。
(1) Does python deal differently with imports on packages & modules that exist in the pythonpath than when you are trying to import from your current directory?
(1)python路径中存在的包和模块上的python与您尝试从当前目录导入时的处理方式不同吗?
No, it doesn't. 不,它没有。 Actually, Python always searches
sys.path
when importing modules. 实际上,Python在导入模块时总是搜索
sys.path
。 Modules in the current directory are only found since sys.path
contains an entry with the empty string, meaning the current directory. 仅找到当前目录中的模块,因为
sys.path
包含带有空字符串的条目,表示当前目录。
(2) Why doesn't
import testpackage
give me access totestpackage.testmod
?(2)为什么不
import testpackage
让我访问testpackage.testmod
? When I importos
, I can then accessos.path
(etc).当我导入
os
,我可以访问os.path
(etc)。
For efficiency, import testpackage
only loads testpackage/__init__.py
. 为了提高效率,
import testpackage
只加载testpackage/__init__.py
。 If you need testpackage.testmod
, you have to explicitely import it: 如果你需要
testpackage.testmod
,你必须明确地导入它:
import testpackage # Just imports testpackage, not testpackage.testmod!
import testpackage.testmod # Import *both* testpackage and testpackage.testmod!
If you always want to export testmod
, import it within __init__.py
, this is what os
( os/__init__.py
) does. 如果你总是想导出
testmod
,在__init__.py
导入它,这就是os
( os/__init__.py
)所做的。 This way, testpackage.testmod
is always available implicitely if you import testpackage
. 这样,如果导入
testpackage
, testpackage.testmod
总是隐含可用。
Since Python is cross-platform, there is actually no way to consistently and automatically load modules in a directory, because some filesystems are case-insensitive (Windows!). 由于Python是跨平台的,因此实际上无法在目录中一致地自动加载模块,因为某些文件系统不区分大小写(Windows!)。 Python would not know whether to load
os/path.py
as os.path
or os.Path
, etc. Python不知道是否将
os/path.py
加载为os.path
或os.Path
等。
(3) With a package, should I stick to using a single
__init__.py
in the base directory, or should I be nesting them in subsequent directories?(3)对于一个包,我应该坚持在基目录中使用单个
__init__.py
,还是应该将它们嵌套在后续目录中?
You always need an __init__.py
for each subpackage. 每个子包总是需要
__init__.py
。 There were discussions on dropping this requirement, but it was decided to keep it as it is. 有关于放弃这一要求的讨论,但决定保持原样。
(4) How can I import a module specifying the package name?
(4)如何导入指定包名称的模块? IE from
testmod.py
, I would like to importtestpackage.testmod2
rather than justtestmod2
.来自
testmod.py
IE,我想导入testpackage.testmod2
而不仅仅是testmod2
。
This should work. 这应该工作。 Just ensure that you run the code from the top-level directory.
只需确保从顶级目录运行代码。 If the current directory is
testpackage
, testmod
does not know that it's in a package. 如果当前目录是
testpackage
,则testmod
不知道它在包中。
The preferred way is to use relative intra-package imports, though: 但首选的方法是使用相对的包内导入:
from . import testmod2
This prevents name clashes if there is a global module named testmod2
and enables you to use the names of well-known modules within your package without problems. 如果存在名为
testmod2
的全局模块,则可以防止名称冲突,并且可以使用包中的已知模块的名称而不会出现问题。
(5) What is the proper way to import submodules from the subsubs directory?
(5)从subsubs目录导入子模块的正确方法是什么? The only solution I see is to append that directory to the pythonpath from
__init__.py
, but I don't know if that's the correct way.我看到的唯一解决方案是将该目录从
__init__.py
附加到pythonpath,但我不知道这是否正确。
No, don't do that! 不,不要那样做! Never, ever put a directory to
sys.path
when one of it's parent directories already is in sys.path
! 永远不要把一个目录下
sys.path
当它的一个父目录已经在sys.path
! This could cause your modules to be loaded twice, which is a bad thing! 这可能导致您的模块加载两次,这是一件坏事!
Usually you should be able to load modules from subpackages using absolute or relative imports: 通常,您应该能够使用绝对或相对导入从子包加载模块:
import testpackage.testsubs.testsubmod
from testpackage.testsubs import testsubmod
from .testsubs import testsubmod
Just make sure to create a __init__.py
within testsubs/
! 只需确保在
testsubs/
创建__init__.py
!
testpackage
directory. testpackage
目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.