Live data from Hacker News

How the Python import system works

tenthousandmeters.com

1–10 of 209 posts

Re: How the Python import system works

#4
Python importing quirks can be time consuming. Things beginners will encounter:

- three modules cannot depend on each other in a circular way

- relative imports are fragile ("module not found")

- the __all__ definitions in the __init__ file make modules available under different full names

- how to reload a module in a jupyter notebook if edited

and so on.

Re: How the Python import system works

#5

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?

When in doubt, put some more dots in front of your import statements. Or remove them? Maybe I need an extra __init__.py somewhere? Oh I'm importing from a subfolder, what do I need to do to get that work again? I can't remember.

Re: How the Python import system works

#6

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?

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!

Re: How the Python import system works

#7

Python importing quirks can be time consuming. Things beginners will encounter: - three modules cannot depend on each other in a circular way - relative imports are fragile ("module not found") - the __all__ definitions in the __init__ file make modules available under different full names - how to reload a module in a jupyter notebook if edited and so on.

> three modules cannot depend on each other in a circular way

What would the purpose of circular modules like this be? You may as well collapse into a single module and the situation would not be any different would it?

Re: How the Python import system works

#8

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?

I’ve only been writing Python for about a year, but I’ve found it much harder to grasp how dependency resolution and imports work than other languages I’ve picked up (JVM, Node, Go, C).

Re: How the Python import system works

#9

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?

I thought not knowing that made me a noob.

Re: How the Python import system works

#10

Python importing quirks can be time consuming. Things beginners will encounter: - three modules cannot depend on each other in a circular way - relative imports are fragile ("module not found") - the __all__ definitions in the __init__ file make modules available under different full names - how to reload a module in a jupyter notebook if edited and so on.

I’m always amazed just how tolerant javascript’s import system is when I have circular imports. I guess maybe because it doesn’t care about modules and just cares about specific elements that are being imported/exported.

When I do have a nasty circular dependency Webpack usually does a bad job telling me what’s wrong.

Though I should still treat circular imports as, at the very least, an organization code smell.

Post reply on HN