我何时需要使用__init__.py? [英] When do I require using __init__.py?

查看:77
本文介绍了我何时需要使用__init__.py?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下结构:

/blog
    /app
        /static
            staticfile.py
        /templates
            templatefile.py
    run.py

run.py 中,

from app.static import staticfile

print("you run me")

staticfile.py 我有, print(从静态文件运行)

当我运行 python run.py 时,

ran from staticfile
you run me

我没有 __init __。py 在任何地方都没有虚拟环境。没有 __ init __。py 文件,我的参考如何工作?是因为我使用的是Python 3.4吗?如果重要的话,我在Windows 7中。

I do not have __init__.py anywhere and I do not have a virtual environment. How does my reference work without the __init__.py file? Is it because I'm using Python 3.4? I am in Windows 7 if it matters.

编辑:最好,我可以说这是因为我在3.4中。我显式使用了 C:\python27\python run.py ,但直到我放入 __ init __。py 时,它都失败了。

Best I can tell it is because I am in 3.4. I explicitly used C:\python27\python run.py and it failed until I put __init__.py.

推荐答案

3.4词汇表中有此项

The 3.4 Glossary has this entry


命名空间软件包

namespace package

PEP 420软件包仅用作子软件包的容器。
命名空间软件包可能没有物理表示形式,而
特别不同于常规软件包,因为它们没有
init .py文件。

A PEP 420 package which serves only as a container for subpackages. Namespace packages may have no physical representation, and specifically are not like a regular package because they have no init.py file.

根据我对PEP的阅读,包含在 sys.path 上但不包含 __ init __。py 是潜在的名称空间目录。运行 run.py 时,'。'表示 /博客添加到 sys.path 的前面。因此,将搜索 / blog中的目录作为潜在的命名空间目录。

From my reading of the PEP, directories contained in directories on sys.path but not containing __init__.py are potential namespace directories. When run.py is run, '.', representing /blog, is added to the front of sys.path. Therefore, the directories in `/blog' are searched as potential namespace directories.

PEP也说


如果开发人员知道她的程序包将永远不会成为
名称空间程序包的一部分,则将其作为
常规程序包(具有初始化 .py)。常规
软件包位于路径上时,可以立即进行创建和加载。
对于命名空间包,在创建包之前必须先扫描路径中的所有条目

If a developer knows that her package will never be a portion of a namespace package, then there is a performance advantage to it being a regular package (with an init.py ). Creation and loading of a regular package can take place immediately when it is located along the path. With namespace packages, all entries in the path must be scanned before the package is created.

换句话说,将在 sys.path 的每个目录中搜索 app.static 包中可能存在的其他模块。 / blog )停止。我会继续添加您认为必需的 __ init __。py 文件。

In other words, every directory on sys.path is searched for possibly other modules in the app.static package, instead of stopping with '.' (/blog). I would go ahead and add the __init__.py files that you thought were required.

PEP还注意到了可能的意外影响。

The PEP also noted the possible surprise effect.


请注意,导入警告缺少 init .py文件的目录
将不再引发。这样的目录现在将作为
导入到一个命名空间包中,而在以前的Python版本中,将引发ImportWarning

Note that an ImportWarning will no longer be raised for a directory lacking an init.py file. Such a directory will now be imported as a namespace package, whereas in prior Python versions an ImportWarning would be raised.

这篇关于我何时需要使用__init__.py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆