找到你要的答案

Q:What is the reason python needs __init__.py for packages? [duplicate]

Q:Python需要__init__是什么原因。Py的包吗?[重复]

This question already has an answer here:

I understand that python needs the __ init __.py file in order to recognize the directory as a python package, that way we can import sub modules into our program.I can see the similarity to classes and how init can be used to execute necessary code off the bat.

However, in the python docs, this line confuses me,

This is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

as seen here https://docs.python.org/2/tutorial/modules.html#packages

Could someone please clarify this?

这个问题在这里已经有了答案:

我的理解是,Python需要__ init __。为了py文件到目录作为一个Python包,这样我们可以导入的子模块,我们的节目。我可以看到相似的类,如何初始化可以用于执行必要的代码的蝙蝠。

然而,在Python文档,这让我困惑,

This is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

因为在这里看到HTTPS:/ /文档。Python。org / 2 /教程/模块的HTML #包。

有人能澄清一下吗?

answer1: 回答1:

The documentation is very clear on this - your project structure could look like this:

app
 - common
  - init.py
 - resources
 - string
 - src

If Python implicitly treated the directories as packages, the "string" directory could present a name clash with Python's built-in string module (https://docs.python.org/2/library/string.html). This means that when calling import string, the module is ambiguous.

__init__.py also adds a bit of functionality: code there is executed when initializing the package and can therefore be used to do package setup of some kind.

文档是非常清楚的-你的项目结构可以像这样:

app
 - common
  - init.py
 - resources
 - string
 - src

如果Python隐式处理目录包,“字符串”目录可能出现命名冲突与Python的内置字符串模块(https:/ /文档。Python。org / 2 /图书馆/串。HTML)。这意味着调用导入字符串时,该模块是不明确的。

__init__ .py也增加了一点功能:代码有执行初始化程序时,因此可以用来做某种包安装。

answer2: 回答2:

this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

Suppose you have a directory where you do work for school, some of this involving python. You have a directory for math, which you have called math. You also have a python module you wrote, so the top level directory "school" has been added to the python path so you can use it anywhere

School/    
   math/
      hw1.txt
      integrate.py
   MyPythonModule/
      __init__.py
      someClass.py
      someFunc.py

When you are using python later and you are searching for MyPythonModule, python will open up School/

Then it sees math/ and MyPythonModule/ If you are using math in your python program, and there was not a way to distinguish between module ../lib/site-packages/math/ and non module ../School/math/ then python will treat your file ../School/math/ as the package math; breaking code without you knowing why.

这样做是为了防止具有通用名称的目录,例如字符串,从无意中隐藏在模块搜索路径后面的有效模块。

假设你有一个目录,你做的工作,这部分涉及到Python。你有一个数学的目录,你称之为数学。你也有一个Python模块的话,那么上一级目录中的“学校”已经加入到Python路径,你可以在任何地方使用

School/    
   math/
      hw1.txt
      integrate.py
   MyPythonModule/
      __init__.py
      someClass.py
      someFunc.py

当你以后使用Python和你正在寻找MyPythonModule,Python会开放学校/

Then it sees math/ and MyPythonModule/ If you are using math in your python program, and there was not a way to distinguish between module ../lib/site-packages/math/ and non module ../School/math/ then python will treat your file ../School/math/ as the package math; breaking code without you knowing why.

answer3: 回答3:

If you have a directory named string that isn't a package, in a location where Python searches for modules and packages (such as the current working directory), Python shouldn't try to import that when you do import string. The __init__.py requirement lets Python know it should keep going rather than treating that directory as a package.

如果你有一个目录的字符串不是一个包,在一个位置的Python搜索模块和包(如当前工作目录),Python不应该进口,当你输入的字符串。的__init__ .py要求让Python知道应该继续治疗,而不是作为一个包的目录。

python  module  package  init