为什么没有__init__.py也能导入成功? [英] Why can I import successfully without __init__.py?

查看:26
本文介绍了为什么没有__init__.py也能导入成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

__init__.py到底有什么用?是的,我知道这个文件将一个目录变成了一个可导入的包.但是,请考虑以下示例:

What exactly is the use of __init__.py? Yes, I know this file makes a directory into an importable package. However, consider the following example:

project/
   foo/
      __init__.py
      a.py
      bar/
         b.py

如果我想把a导入到b中,我必须添加如下语句:

If I want to import a into b, I have to add following statement:

sys.path.append('/path_to_foo')
import foo.a

无论是否使用 __init__.py,这都会成功运行.但是,如果没有 sys.path.append 语句,无论有没有 __init__.py 都会出现no module"错误.这使得看起来只有系统路径很重要,而 __init__.py 没有任何影响.

This will run successfully with or without __init__.py. However, if there is not an sys.path.append statement, a "no module" error will occur, with or without __init__.py. This makes it seem lik eonly the system path matters, and that __init__.py does not have any effect.

为什么没有 __init__.py 这个导入会起作用?

Why would this import work without __init__.py?

推荐答案

__init__.py 与 Python 能否找到你的包无关.您以这样一种方式运行您的代码:默认情况下您的包不在搜索路径上,但如果您以不同方式运行它或以不同方式配置您的 PYTHONPATHsys.path.append 本来是不必要的.

__init__.py has nothing to do with whether Python can find your package. You've run your code in such a way that your package isn't on the search path by default, but if you had run it differently or configured your PYTHONPATH differently, the sys.path.append would have been unnecessary.

__init__.py 过去是创建包所必需的,在大多数情况下,您仍然应该提供它.但是,从 Python 3.3 开始,没有 __init__.py 的文件夹可以被视为 隐式命名空间包,一种用于将包拆分到多个目录的功能.

__init__.py used to be necessary to create a package, and in most cases, you should still provide it. Since Python 3.3, though, a folder without an __init__.py can be considered part of an implicit namespace package, a feature for splitting a package across multiple directories.

在进口加工过程中,进口机械将继续像在 Python 中一样遍历父路径中的每个目录3.2.在寻找名为foo"的模块或包时,对于父路径中的每个目录:

During import processing, the import machinery will continue to iterate over each directory in the parent path as it does in Python 3.2. While looking for a module or package named "foo", for each directory in the parent path:

  • 如果找到 /foo/__init__.py,则导入并返回常规包.
  • 如果没有,但是 /foo.{py,pyc,so,pyd} 被找到,一个模块被导入并返回.扩展的确切列表因平台而异以及是否指定了 -O 标志.这里的清单是代表.
  • 如果不是,但是/foo被发现并且是​​一个目录,它被记录并继续扫描父目录中的下一个目录路径.
  • 否则,扫描将继续使用父路径中的下一个目录.
  • If <directory>/foo/__init__.py is found, a regular package is imported and returned.
  • If not, but <directory>/foo.{py,pyc,so,pyd} is found, a module is imported and returned. The exact list of extension varies by platform and whether the -O flag is specified. The list here is representative.
  • If not, but <directory>/foo is found and is a directory, it is recorded and the scan continues with the next directory in the parent path.
  • Otherwise the scan continues with the next directory in the parent path.

如果扫描完成而没有返回模块或包,并且 在至少记录了一个目录,然后创建了一个命名空间包.

If the scan completes without returning a module or package, and at least one directory was recorded, then a namespace package is created.

这篇关于为什么没有__init__.py也能导入成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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