应该使用`import os.path`还是`import os`? [英] Should I use `import os.path` or `import os`?
问题描述
根据官方文档, os .path
是一个模块。因此,导入它的首选方式是什么?
According to the official documentation, os.path
is a module. Thus, what is the preferred way of importing it?
# Should I always import it explicitly?
import os.path
或...
# Is importing os enough?
import os
请不要回答import os
为我工作。我知道,它适用于我现在(从Python 2.6)。我想知道的是关于这个问题的任何官方建议。因此,如果您回答此问题,请发布您的参考。
Please DON'T answer "importing os
works for me". I know, it works for me too right now (as of Python 2.6). What I want to know is any official recommendation about this issue. So, if you answer this question, please post your references.
推荐答案
os.path
以有趣的方式工作。看起来 os
应该是一个包含子模块 path
的包,但实际上 os
是一个正常的模块,用 sys.modules
来注入 os.path
下面是发生了什么:
os.path
works in a funny way. It looks like os
should be a package with a submodule path
, but in reality os
is a normal module that does magic with sys.modules
to inject os.path
. Here's what happens:
-
当Python启动时,它将一组模块加载到
sys.modules
。它们不会绑定到脚本中的任何名称,但是当您以某种方式导入时,可以访问已创建的模块。
When Python starts up, it loads a bunch of modules into
sys.modules
. They aren't bound to any names in your script, but you can access the already-created modules when you import them in some way.
-
sys.modules
是模块被高速缓存的dict。当你导入一个模块,如果它已经被导入到某个地方,它会得到实例存储在sys.modules
。
sys.modules
is a dict in which modules are cached. When you import a module, if it already has been imported somewhere, it gets the instance stored insys.modules
.
os
是Python启动时加载的模块之一。它将路径
属性分配给特定于os的路径模块。
os
is among the modules that are loaded when Python starts up. It assigns its path
attribute to an os-specific path module.
c> sys.modules ['os.path'] = path ,以便您可以执行 import os.path
它是一个子模块。
It injects sys.modules['os.path'] = path
so that you're able to do "import os.path
" as though it was a submodule.
我倾向于认为 os.path
作为一个我想使用的模块,而不是 os
模块不是真的一个子模块的 os
,我导入它喜欢它是一个和我总是做 import os.path
。这与如何记录 os.path
是一致的。
I tend to think of os.path
as a module I want to use rather than a thing in the os
module, so even though it's not really a submodule of a package called os
, I import it sort of like it is one and I always do import os.path
. This is consistent with how os.path
is documented.
顺便说一下,这种结构导致很多Python程序员早期对模块和包和代码组织的困惑,我想。这真的有两个原因
Incidentally, this sort of structure leads to a lot of Python programmers' early confusion about modules and packages and code organization, I think. This is really for two reasons
-
如果你认为
os
一个包,并知道你可以做import os
,并且可以访问子模块os.path
,你可能会感到惊讶稍后当您无法导入import twisted
并自动访问twisted.spread
,而无需导入。
If you think of
os
as a package and know that you can doimport os
and have access to the submoduleos.path
, you may be surprised later when you can't doimport twisted
and automatically accesstwisted.spread
without importing it.
令人困惑的是, os.name
是一个正常的东西,一个字符串和 os。 path
是一个模块。我总是用空的 __ init __。py
文件来结构我的包,所以在同一级别我总是有一种类型的东西:一个模块/包或其他东西。几个大型Python项目采用这种方法,这倾向于制作更多的结构化代码。
It is confusing that os.name
is a normal thing, a string, and os.path
is a module. I always structure my packages with empty __init__.py
files so that at the same level I always have one type of thing: a module/package or other stuff. Several big Python projects take this approach, which tends to make more structured code.
这篇关于应该使用`import os.path`还是`import os`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!