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

查看:372
本文介绍了为什么没有__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,都将发生无模块"错误.这使得似乎只有系统路径才重要,而__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是否可以找到您的包无关.您已经以默认方式不在您的搜索路径上运行程序包的方式运行了代码,但是如果以不同的方式运行它或以不同的方式配置PYTHONPATH,则sys.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:

  • 如果找到<directory>/foo/__init__.py,则将导入常规包并返回.
  • 如果没有,但是找到了<directory>/foo.{py,pyc,so,pyd},则导入并返回一个模块.扩展的确切列表因平台而异 以及是否指定了-O标志.这里的清单是 代表.
  • 如果没有,则找到<directory>/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天全站免登陆