python 自动加载,Python模块自动加载器?

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

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

I'm thinking something similar to PHP autoloader. Even better something like Composer autoloader.

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 = "
".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?

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.

Any ideas?

解决方案

TL;DR : Forget about it and use explicit imports.

Longer answer:

Python is not PHP - neither from the technical POV nor from the design / philosophical one.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值