简体   繁体   English

Python __init__.py 与 sys.path.append/insert

[英]Python __init__.py vs sys.path.append/insert

I know there are a ton of how-to import Python modules not in path, but I have yet to come across using Python's __init.py__ vs sys.path.insert.知道如何对进口Python模块不在路径,但我还没有遇到过使用Python的__init.py__ VS sys.path.insert。 Which method is better?哪种方法更好? Are there any obvious drawbacks to either, like performance?两者是否有任何明显的缺点,例如性能? Is one more "Pythonic?"还有一个“Pythonic”吗?

One scenario I can think of, is I have a program that users download and put in whatever directory, so I don't know the absolute path (unless I get it programatically).我能想到的一种情况是,我有一个程序供用户下载并放在任何目录中,所以我不知道绝对路径(除非我以编程方式获得它)。 The folder structure is文件夹结构是

working dir
    __init__.py
    foo.py
    src/
        my_utils.py
        __init__.py

I don't see any difference between using __init__.py or changing sys.path.我看不出使用 __init__.py 或更改 sys.path 有什么区别。 Is there any scenario you can think of where it would make a difference?有什么场景你能想到它会有所作为吗?

Part 2 of my question is, why do you have to do anything to be able to import modules from subdirectories?我的问题的第 2 部分是,为什么您必须做任何事情才能从子目录导入模块? I'm fairly new to Python, so maybe I'm not understanding why fiddling with the path or creating init files is boilerplate.我对 Python 相当陌生,所以也许我不明白为什么摆弄路径或创建init文件是样板。 To me, it seems like an unnecessary complication.对我来说,这似乎是一种不必要的复杂化。 If I have "dir" in my current working directory and say "import dir.my_utils," I don't see why I would have to list everything I would want to be able to import in __init__.py.如果我在当前工作目录中有“dir”并说“import dir.my_utils”,我不明白为什么我必须列出我希望能够在 __init__.py 中导入的所有内容。

Apologies if this is a duplicate, but I did search before posting.如果这是重复的,请道歉,但我在发布之前确实进行了搜索。

Edit: Here's another useful link: Automatically call common initialization code without creating __init__.py file编辑:这是另一个有用的链接: 自动调用通用初始化代码而不创建 __init__.py 文件

__init__.py is used by the python interpreter to treat directories as packages. __init__.py被 python 解释器用来将目录视为包。 Packages play an important role in avoiding namespace collisions.包在避免命名空间冲突方面发挥着重要作用。 If you read the section 6.4 Packages from Python Modules , it helps to prevent directories with a common name from hiding other valid modules that occur later in the search path.如果您阅读了第6.4节来自Python 模块的,它有助于防止具有通用名称的目录隐藏稍后出现在搜索路径中的其他有效模块。

Hence, the package mechanism simplifies the task of importing packages.因此,包机制简化了导入包的任务。 By using an __init__.py you can also do something like from package.subpackage import * which would be difficult or tedious to do if we were to keep appending to sys.path (in fact we will have to append all the possible modules).通过使用__init__.py您还可以执行诸如 from package.subpackage import *操作,如果我们要继续附加到sys.path (实际上我们必须附加所有可能的模块),这将是困难或乏味的。

As to answer the second part to your question - why do we need to do anything to treat directories as packages - well, there needs to be some way to tell python what should be allowed to import and what should not be.至于回答你的问题的第二部分——为什么我们需要做任何事情来将目录视为包——嗯,需要有一些方法来告诉 python 什么应该被允许导入,什么不应该被允许。 Also, you do not need to append anything to sys.path explicitly in case you have imported all the required modules at the beginning and that the modules you require to be imported are already present on the PYTHONPATH environment variable.此外,如果您在开始时导入了所有必需的模块并且需要导入的模块已经存在于PYTHONPATH环境变量中,则您不需要显式地向 sys.path 附加任何内容。

Hopefully this answer sheds some light on your query.希望这个答案能对您的查询有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM