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

查看:76
本文介绍了Python __init__.py与sys.path.append/insert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道那里-insert1-path>操作方法导入 Python模块不在路上,但是我还没有遇到使用Python的 __init.py__ vssys.path.insert.哪种方法更好?两者是否都有明显的缺点,例如性能?还有一个"Pythonic"吗?

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. Which method is better? Are there any obvious drawbacks to either, like performance? Is one more "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

我看不到使用__init__.py或更改sys.path之间的任何区别.您有什么可以想到的方案在哪些方面有所作为?

I don't see any difference between using __init__.py or changing sys.path. Is there any scenario you can think of where it would make a difference?

我的问题的第二部分是,为什么您必须做任何事情才能从子目录导入模块?我对Python还是很陌生,所以也许我不明白为什么摆弄路径或创建 init 文件是样板.对我来说,这似乎是不必要的并发症.如果我在当前工作目录中有"dir"并说"import dir.my_utils",我不明白为什么我必须列出我希望能够在__init__.py中导入的所有内容.

Part 2 of my question is, why do you have to do anything to be able to import modules from subdirectories? I'm fairly new to Python, so maybe I'm not understanding why fiddling with the path or creating init files is boilerplate. 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.

很抱歉,如果这是重复的,但我确实在发布之前进行了搜索.

Apologies if this is a duplicate, but I did search before posting.

这是另一个有用的链接:

Here's another useful link: Automatically call common initialization code without creating __init__.py file

推荐答案

__ init __.py 将目录视为软件包.包在避免名称空间冲突中起着重要的作用.如果您从 Python模块阅读 6.4软件包部分,>,这有助于防止具有通用名称的目录隐藏以后在搜索路径中出现的其他有效模块.

__init__.py is used by the python interpreter to treat directories as packages. 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.

因此,包机制简化了导入包的任务.通过使用 __ init __.py ,您还可以从 package.subpackage import * 中执行类似的操作,如果我们要继续附加到,这将是困难或乏味的sys.path (实际上,我们将不得不附加所有可能的模块).

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).

回答您问题的第二部分-为什么我们需要做任何事情来将目录视为软件包-嗯,需要有某种方式告诉python应该允许导入什么以及不应该的.另外,如果您已经在开始时导入了所有必需的模块,并且您需要导入的模块已经存在于 PYTHONPATH 环境变量中,则无需将任何内容显式地添加到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. 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.

希望这个答案对您的查询有所帮助.

Hopefully this answer sheds some light on your query.

这篇关于Python __init__.py与sys.path.append/insert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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