Python modules autoloader?

北战南征 提交于 2019-12-07 04:01:02

问题


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

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?


回答1:


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.




回答2:


Automatic source code reloading in the way that PHP does it isn't practical in Python. I would suggest you go have a read of:

  • https://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

If you really really must have something, see the details of the automatic code reloader you can use with mod_wsgi described in that. Never use it on a production system though.

If you are doing development, you frankly may be better off using mod_wsgi-express. See:

  • https://pypi.python.org/pypi/mod_wsgi

One of the things mod_wsgi-express has is the --reload-on-changes option, which implements the automatic code reloading for you.



来源:https://stackoverflow.com/questions/32068957/python-modules-autoloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!