Live data from Hacker News

How the Python import system works

tenthousandmeters.com

71–80 of 209 posts

Re: How the Python import system works

#71
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 and actually finished it.

Some day I'd like to go back and maybe learn Python but I really didn't enjoy my experience with it. I even found C headers easier to figure out than Python imports.

Re: How the Python import system works

#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.

Re: How the Python import system works

#73

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.

Can I just say that the introduction to Lisp you made in your README.md is really good! I keep trying to get into Lisp (and JavaScript, and TypeScript, etc etc) but I've been a sysadmin my whole professional life and also a chronic pain sufferer. That translates into mostly having energy only for work and that's it, not much motivation to learn after work or on the weekend. In my DevOps job, I write Terraform, plus r…

Not mine! That was all Scott Bell. It's forked from Lumen: https://github.com/sctb/lumen

But, I did make an interactive tutorial here: https://docs.ycombinator.lol/

If you have any questions about it, I'd be happy to answer. This stuff is pure fun mixed with a shot of professionalism.

For what it's worth, as someone with narcolepsy, I relate quite a lot to your chronic pain. (https://twitter.com/theshawwn/status/1392213804684038150) For me, it mostly translated into wandering aimlessly from job to job, since I thought no one would have me. I hope that you find your way -- there's nothing wrong at all with taking it slow and spending years on something that takes others a few months. Everyone is different, and it's all about the fun.

Re: How the Python import system works

#74
post #58

I'm just grateful I'm not a core Python dev after reading this thread. I've never seen so much negativity concentrated in one place in quite some time, for a feature of a programming language which is fairly innocuous.

I love Python, but to be fair, the dependency/import system has not aged well, and the various projects trying to fix it are proof of that.

Re: How the Python import system works

#75
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

Won't this work just fine if you instead of writing:

  from a import A
write

  import a
and in the code (which is presumably not at module level) check against

  isinstance(obj, a.A)

?

Re: How the Python import system works

#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'

Re: How the Python import system works

#77
post #51

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.

Wait, what? > that you create As in existing language supplied eg. Perl, Java, etc., or literally anything? Like bootstrapping your own home made language from scratch?

> As in existing language supplied eg. Perl, Java, etc., or literally anything? Like bootstrapping your own home made language from scratch?

Should work with anything as long as you can ultimately generate Python bytecode (and provide a module object). The import system is not simple, but it's really open.

Re: How the Python import system works

#78

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.

Re: How the Python import system works

#79
I created this fun hack that taught me a LOT about the import system. Basically it allows you to import anything you want, even specify a version, and it will fetch it from PyPI live. Might be interesting to flesh this out in a way that's deployable.

Basically instead of

    import tornado
and hoping and praying that the user has read you README and pip installed the right version of tornado, you can do

    from magicimport import magicimport
    tornado = magicimport("tornado", version = "4.5")
and you have exactly what you need, as long as there is an internet connection.

https://github.com/dheera/magicimport.py

Post reply on HN