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?
Reloading modules in Python. reload () reloads a previously imported module. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object.
It’s called %autoreload. It’s not enabled by default, so you have to load it as an extension: And each time you execute some code, IPython will reimport all the modules to make sure that you are using the latest possible versions. %autoreload 0 - disables the auto-reloading. This is the default setting.
Check the file ~/.ipython/ipythonrc - you can list all modules you want to load at the startup. Have a .pythonstartup in your home directory and load modules there and point PYTHONSTARTUP env to that file. Python commands in that file are executed before the first prompt is displayed in interactive mode.
It won’t be so slow that you will feel it (unless you have modules that take seconds to import), but it will obviously run faster if you disable the auto-reloading. As pointed out in the documentation, %autoreload is not 100% reliable, and there might be some unexpected behaviors.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With