Python 模块自动加载器? [英] Python modules autoloader?

查看:42
本文介绍了Python 模块自动加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自动加载保存在不同目录和子目录中的所有模块?

How can I autoload all my modules that kept in different directories and sub directories?

我已经看到这个 answer 正在使用 __import__,但它仍然不是我想到的自动加载.

I have seen this answer which is using __import__, but it is still not the autoload that I have in mind.

我在想类似于 PHP 自动加载器的东西.更好的是Composer autoloader.

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 哲学的一部分(又名Python 的 Zen")是显式优于隐式"、可读性很重要"和面对模棱两可,拒绝猜测的诱惑"——这使得像 PHP 的自动加载"这样的特性"变得非常不pythonic,因为 1. 它不像 a"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).

从技术观点来看,您无法以可靠的方式在 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屋!

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