Live data from Hacker News

How the Python import system works

tenthousandmeters.com

91–100 of 209 posts

Re: How the Python import system works

#91

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…

Relative imports used to work much more naturally IMHO in python2 but then they broke it in python3 because Guido wanted scripts and modules to always be separate codebases. So, whereas it used to be easy to have a module that could also be run as a script inside a package, this is now very difficult to implement. To the extent that any python2 code that does this, should probably be refactored when being ported to python3.

Re: How the Python import system works

#92
post #86
post #54

Earlier quoted context omitted.

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__: ...

I think bad code gives raise to more dependencies in general and so circular dependencies.

But the truth is sometimes it has happened to me and the only solution I found was creating an small module with maybe one or two functions which is not exactly ideal.

Re: How the Python import system works

#93

Earlier quoted context omitted.

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/139…

Thanks for sharing this. I didn't expect to read that whole gist but I did and I'm glad I did. Happy for you.

Re: How the Python import system works

#94
post #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(…

That's crafty; it reminds me of the suggested similar tactic with tclsh since tcl honors backslashes in comment strings(!): https://wiki.tcl-lang.org/page/exec+magic

Re: How the Python import system works

#95
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's on of those things people using Python for so long forget about: Some people try to run individual files and cd around the place. I never do that anymore. I have a test suite and breakpoints and that's it. But before you've learned those tools it feels natural to "run that file there" and then say "oh hey why doesn't it work any more?"

Re: How the Python import system works

#96
post #72

Earlier quoted context omitted.

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'v…

No, your impression was right. Reading this blog post made me realize how little I know about the python import system (and I use python daily), and at the same time how little I want to learn it. It is completely unintuitive and probably one of the worst aspects of otherwise beautiful and useful language. Fortunately, sys.path hack works reliably - one can just add that one line and imports work as expected.

Re: How the Python import system works

#98

Earlier quoted context omitted.

The purpose is you want to use code in other modules. If you keep doing that for a while, a circular dependency will happen. This is the dumbest thing thing in Python. All other languages I know have solved it.

> The purpose is you want to use code in other modules. So put them in the same module? Circular modules don’t give you the benefits of modules, do they? Not an expert in modularity.

Putting all your code in one file does indeed solve all import problems, but creates far bigger ones.

In case you didn't know, each source code file is a module in Python.

Re: How the Python import system works

#99

Earlier quoted context omitted.

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/139…

Does "-5e4" really evaluate to "-5000" in this language?

Re: How the Python import system works

#100

Earlier quoted context omitted.

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/139…

Does "-5e4" really evaluate to "-5000" in this language?

Hah! Good catch! That readme typo has been in there since Lumen’s inception.

It evaluates to -500000, as you’d expect.

(Just kidding, it’s -50000. Amusingly, the https://docs.ycombinator.lol version gets it right, since it has to; every expression is actually evaluated in the browser.)

Post reply on HN