Python modules autoloader?

2020-07-08 06:35发布

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?

2条回答
Anthone
2楼-- · 2020-07-08 07:30

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.

查看更多
够拽才男人
3楼-- · 2020-07-08 07:31

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:

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:

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

查看更多
登录 后发表回答