Python modules autoloader?

无人久伴 提交于 2019-12-05 06:10:18

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.

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