Live data from Hacker News

How the Python import system works

tenthousandmeters.com

81–90 of 209 posts

Re: How the Python import system works

#81
post #72

Years ago, I thought I'd try learning Python, I'd heard it was supposed to be easy, good for beginners and everything. I read one of those beginner Python type books and followed along with a roguelike tutorial. Everything was going pretty alright, until I started trying to split everything into different files and use imports. I ended up just giving up. I read programming in lua, and rewrote my entire project in lua…

Strange, Python’s import is not very difficult to use.

It was probably something I did. The original tutorial I followed had everything in one file and didn't get into anything about imports. I started splitting everything up arbitrarily and started tossing imports into the files that complained about missing dependencies and ended up getting overwhelmed because nothing worked.

I'm sure if I'd taken the time to try and fix it I eventually could have and at this point i've had more experience with a bunch of different languages, so I'm sure it's not as bad as I remember.

I imagine it's one of those cases where if i were to go back and laugh about how stupid I was, but ya know, those first impressions.

Re: How the Python import system works

#82

Fun fact: you can overload the Python import system to work with other languages that you create . I 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.

Hylang also does this. Macropy too.

Re: How the Python import system works

#83

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.

probably, you may add to the list

- understand difference between Python in IDE and Python in shell

So many times people do `pip install ` and still not able to use in IDE

Re: How the Python import system works

#84
post #76

Fun fact: you can overload the Python import system to work with other languages that you create . I 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.

I don't get it. Where do you define which Lisp-to-Python translator to use? It certainly doesn't seem to know on its own. $ touch foo.l $ python3 >>> import foo ModuleNotFoundError: No module named 'foo'

I'm guessing something else needs to be imported first.

Re: How the Python import system works

#85

There are two things that are annoying about Python's import system. Number one is that relative imports are weird. My intuition about imports is good enough that I never bothered to learn all the rules explicitly, but sometimes something simple is just not possible and it bites me. I think the case is importing files relative to a script (and not running with python -m ...). Number two is, in order to do package man…

Once I wrapped my head around when you can and can't use relative imports, I've been pretty ok with them. The think that irks me is whether they work changes based on where you've invoked Python from. `./bin/my_script.py` behaves differently from `./my_script.py`.

Coming from JS, that was a pretty frustrating realization.

Re: How the Python import system works

#86
post #54

Earlier quoted context omitted.

> 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?

Suppose you have two classes, A and B. They are sufficiently complex to merit their own modules. Suppose you have some method of A which does something special if it gets an instance of B, and vice versa. Now you have a circular import problem; glhf

IMHO that's code smell. Modules shouldn't depend on each other, because that creates a web of tangled dependency where you have to understand everything before you can understand one of them. Circular dependency is to modules what goto is to control flow.

Besides, if you are in a "well, fuck it, deadline is tomorrow" mode, you can always do something horrible like:

    if 'classB' in type(obj).__name__: ...

Re: How the Python import system works

#87
post #76

Fun fact: you can overload the Python import system to work with other languages that you create . I 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.

I don't get it. Where do you define which Lisp-to-Python translator to use? It certainly doesn't seem to know on its own. $ touch foo.l $ python3 >>> import foo ModuleNotFoundError: No module named 'foo'

Close!

  git clone https://github.com/shawwn/pymen -b ml
  cd pymen
  touch foo.l
  bin/pymen
  (import foo)
It's a bit of a WIP (notice this is on the `ml` branch, not mainline), but it does work. >:)

You need nodejs to be installed too, ha.

Re: How the Python import system works

#88

Fun fact: you can overload the Python import system to work with other languages that you create . I 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.

NodeJS allows this as well. I think this is pretty much a must-have feature for any serious dynamic language. Edit: A must have for any prolific dynamic language. But now I’m not sure that’s true, because even though it apparently works in Python, it’s certainly not widely used. In NodeJS this feature is used quite heavily for typescript, coffeescript (etcetera) interop.

> But now I’m not sure that’s true, because even though it apparently works in Python, it’s certainly not widely used.

I mean, it is used—even in the standard library—but often for alternative packaging (e.g., loading python modules in zip files) rather than alternative languages. It may be used less prominently than in Node, but it definitely is used for a variety of things.

Re: How the Python import system works

#89

One of the reasons I gradually fell out of love with Python. To get Python right you need to remember more protocol than Queen Victoria's master of tea. And it is truly protocol in the sense that there is always this arbitrariness hanging around it.

What do you use now?

Re: How the Python import system works

#90
Probably half of the commenters here know this, but since we're here, this is my go-to boilerplate for starting a python script. (Probably won't work on Windows.)

    #!/bin/sh
    # Run the interpreter with -u so that stdout isn't buffered.
    "exec" "python3" "-u" "$0" "$@"

    import os
    import sys
    curdir = os.path.dirname(os.path.realpath(sys.argv[0]))
    # Add enough .. to point to the top-level project directory.
    sys.path.insert(0, '%s/../..' % curdir)

    Your main program starts here ...
Post reply on HN