Python模块自动加载器? [英] Python modules autoloader?
问题描述
如何自动加载保存在不同目录和子目录中的所有模块?
How can I autoload all my modules that kept in different directories and sub directories?
我已经看到了使用__import__
的 answer ,但是仍然不是我想到的自动加载.
I have seen this answer which is using __import__
, but it is still not the autoload that I have in mind.
我正在考虑类似于 PHP自动加载器的内容.甚至更好的东西,例如 Composer自动加载器.
I'm thinking something similar to PHP autoloader. Even better something like Composer autoloader.
从我到目前为止收集的研究来看,自动加载器似乎在Python中并不流行(不能肯定,因为我是Python的新手).在Python中自动加载某些东西是不值得鼓励的吗?
It seems that autoloader is not a popular thing in Python from the research I have gathered so far (can't be sure as I'm new in Python). Is autoloading something not encourage-able in Python?
到目前为止,我的自动加载代码,
My autoload code so far,
import os
import sys
root = os.path.dirname(__file__)
sys.path.append(root + "/modules")
sys.path.append(root + "/modules/User")
sys.path.append(root + "/modules/Article")
# IMPORTS MODULES
module = __import__("HelloWorld")
my_class = getattr(module, "HelloWorld")
# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):
results = []
helloWorld = my_class()
results.append(helloWorld.sayHello())
output = "<br/>".join(results)
print output
...
如您所见,我仍然需要这些行来加载模块,
As you can see that I still need to have these lines in order to load the modules,
sys.path.append(root + "/modules")
sys.path.append(root + "/modules/User")
sys.path.append(root + "/modules/Article")
如果我有大量的文件夹和子文件夹怎么办?我要列出所有这些吗?最终它会进入一长串,不是吗?
What if I have tons of folders and sub folders? Am I going to list them all? It is going to a long list eventually, isn't?
此外,使用__import__
似乎并没有太大的区别,
Also, using __import__
seems does not make much of difference from this,
import os
import sys
root = os.path.dirname(__file__)
sys.path.append(root + "/modules")
sys.path.append(root + "/modules/User")
sys.path.append(root + "/modules/Article")
# IMPORTS MODULES
import hello
import HelloWorld
from HelloWorld import HelloWorld
# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):
后者对我来说看起来更好更整洁了.
The latter looks nicer and neater to me.
有什么想法吗?
推荐答案
TL; DR:忘记它并使用显式导入.
TL;DR : Forget about it and use explicit imports.
更长的答案:
Python不是PHP-既不是技术上的POV,也不是设计/哲学上的.
Python is not PHP - neither from the technical POV nor from the design / philosophical one.
Python哲学的一部分(aka" Python的禅宗")是显式比隐式更好",可读性很重要"和面对模棱两可,拒绝诱惑的诱惑"-这使得像PHP的自动加载"这样的功能"高度不符合Python,因为1.它不如显式"import mymodule"或"from mymodule import SomeName"; 2.可读性较差(您不清楚从何处导入名称); 3.当两个或多个模块定义相同名称时(完全是完美的)有效且在Python中相当普遍),则必须尝试猜测您想要哪个(从技术上讲这是不可能的).
Parts of Python's philosophy (aka "Python's Zen") are "explicit is better than implicit", "Readability counts", and "In the face of ambiguity, refuse the temptation to guess" - which makes a "feature" like PHP's "autoload" highly unpythonic, as 1. it's way less explicit than a "import mymodule" or "from mymodule import SomeName", 2. it's also way less readable (you don't know clearly where a name is imported from), and 3. when two or more modules define a same name (which is perfectly valid and quite common in Python), it would have to try and guess which one you want (which would be technically impossible).
从技术POV来看,您无法以可靠的方式在Python中实现这种功能 -参见上述第3点.尽管PHP的require
语句和Python的import
语句看起来很相似,但它们的工作方式完全不同,执行模型"也完全不同.
From a technical POV, there's no way you could implement such a feature in Python in a reliable way - cf point 3. above. While PHP's require
statement and Python's import
statement may look similar, they really work in a totally different way, and the "execution models" are also totally different.
这篇关于Python模块自动加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!