I've been programming in Python professionally for more than five years. I consider myself quite a good programmer. I still don't have good grasp on Python's import system. Does anyone else have similar experience?
How the Python import system works
21–30 of 209 posts
Re: How the Python import system works
#22I've been programming in Python professionally for more than five years. I consider myself quite a good programmer. I still don't have good grasp on Python's import system. Does anyone else have similar experience?
For a project I was working on where you could dynamically call distributed tasks (we we're using ecs) I added a subclass of module and package that dynamically created package structures and modules from a database call.
So the new modules and packages would load from the database instead of from a python file and python the class would be dynamically generated to be something like albiet more advanced.
Class MyDistributedTask: Def run(self, input, execution_engine=ecs): Run task
So users of our package could import directly import their package_name.TaskName
And run it directly as long as they imported our package which contained a custom module loader to our db to discover.
Learned a ton about importing.
Re: How the Python import system works
#23Unless anyone knows of magical tools to help solve import issues?
Re: How the Python import system works
#24My favorite is Javascript's. Modules are objects containing functions and data, require returns such objects. Simple, elegant. Completely reifies the behind-the-scenes complexity of Python's import, making it easily understandable.
Re: How the Python import system works
#25I've been programming in Python professionally for more than five years. I consider myself quite a good programmer. I still don't have good grasp on Python's import system. Does anyone else have similar experience?
Re: How the Python import system works
#26I've been programming in Python professionally for more than five years. I consider myself quite a good programmer. I still don't have good grasp on Python's import system. Does anyone else have similar experience?
same! I've always been shielded from it with Django's conventions. (the ecosystem I mainly work in). I used a lot of '.' and '..' imports but I think something changed in python3 that made that strategy a lot less forgiving... now I _really_ should read the entirety of this article!
Skip the relative imports in the article. No need to read it entirely anymore ;-)
Re: How the Python import system works
#27I use this for my Python-based Lisp: https://github.com/shawwn/pymen/blob/ml/importer.py
import foo
checks for "foo.l", and if found, compiles it to foo.py on the fly and imports that instead.It's so cursed. I love it.
Re: How the Python import system works
#28Slightly off-topic: One of the simplest import systems I've seen were in q/kdb (like with most things in that language, everything is as simple as possible) Imports work by simply calling `\l my_script.q` which is similar to simply taking the file `my_script.q` and running it line by line (iirc, it does so eagerly, so it reruns the entire file whenever you do `\l my_script.q`, even if the file has been loaded before,…
Suppose that you are importing a larger project. Where your one import (say of your standard database interface) pulls in a group of modules (say one for each table in your database), all of which import a couple of base libraries (to make your database objects provide a common interface) that themselves pull in common Python modules (like psychopg2) which themselves do further imports of standard Python modules.
The combinatorial explosion of ways to get to those standard Python modules would make the redundant work of parsing them into a significant time commitment without a caching scheme.
From the point of view of a small script, this kind of design may seem like overkill. But in a large project, it is both reasonable and common.
Re: How the Python import system works
#29It's certainly a lot better than Ruby's require which just executes code and alters global virtual machine state. Not too different from C's #include. My favorite is Javascript's. Modules are objects containing functions and data, require returns such objects. Simple, elegant. Completely reifies the behind-the-scenes complexity of Python's import, making it easily understandable.