Live data from Hacker News

How the Python import system works

tenthousandmeters.com

161–170 of 209 posts

Re: How the Python import system works

#161
post #136

Earlier quoted context omitted.

Why not just add a shebang , chmod +x and then you're done?

#!/usr/bin/python This is sometimes what you want, but it will always look at this exact path, and won't play nicely with virtualenv/conda. #!/usr/bin/env python This works - it will use Python found in $PATH. Unfortunately you can't add any more parameters to the interpreter. The contraption I wrote allows adding arbitrary parameters - I was burnt one too many times by Python silently buffering my debug messages, so…

Reading `man env` in Linux shows that you can use:

  #!/usr/bin/env -S python $ARGS
For example:

  #!/usr/bin/env -S python -i
  
  print("Entering interactive mode...")
I'm not sure if it works in other OS.

Re: How the Python import system works

#162

Earlier quoted context omitted.

Earnest question: why are you all trying to use relative imports? What problem is that solving for you? I've never even bothered to try it out because it seems potentially problematic in the way all relative references can be, e.g., relative file paths.

So much this. There's no good reason to use relative imports. They're less readable, more dangerous and don't solve a single problem.

IIRC, it was done for security, to avoid inadvertently picking up a library from an unexpected place. For instance, drop a file named test.py in the same directory as your python 2 script and have "fun" figuring out what went wrong.

Re: How the Python import system works

#164
post #139

Earlier quoted context omitted.

Earnest question: why are you all trying to use relative imports? What problem is that solving for you? I've never even bothered to try it out because it seems potentially problematic in the way all relative references can be, e.g., relative file paths.

People have a script run with python, and want to use code in other files. This is not supported in python. For reasons beyond my understanding, you are supposed to put the script with python, or with the shebang, in a different directory. Alternatively, you can always use `python -m` to run your code.

> People have a script run with python, and want to use code in other files. This is not supported in python.

Come again? This is what "import" does, obviously. This is not a clear explanation of your problem.

Re: How the Python import system works

#165
post #51

Earlier quoted context omitted.

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?

Literally anything. 'Tis a homemade homegrown lisp, grown by Scott Bell for several years till I took it all for myself. Nom nom. It starts with reader.l: https://github.com/shawwn/pymen/blob/ml/reader.l where the raw character stream is turned into a bunch of nested arrays. E.g. (+ 1 2) becomes ["+", 1, 2] Then it's punted over to compiler.l https://github.com/shawwn/pymen/blob/ml/compiler.l where it's passed throug…

I have a homemade language that's not a lisp, but is lispy in some ways. I've only got to the point where I expand the code to an abstract syntax tree, and deciding how to go from there is the hard part for me right now. It's never crossed my mind to just compile it to valid python code. Thanks for the inspiration!

Re: How the Python import system works

#166

Earlier quoted context omitted.

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.

If you have A -> B -> C -> A, then you need all three of A, B and C to be defined if you're going to use any one of those modules. When that's the case, the only thing you're gaining by using modules is the organisation of the file.

Sure, but organizing code in files is one of the most important parts of programming!

Re: How the Python import system works

#167

Earlier quoted context omitted.

See also https://github.com/drathier/stack-overflow-import for stupid Python import tricks. "from stackoverflow import quick_sort will go through the search results of [python] quick sort looking for the largest code block that doesn’t syntax error in the highest voted answer from the highest voted question and return it as a module. If that answer doesn’t have any valid python code, it checks the next highest voted…

That stackoverflow library is so gloriously bonkers. Is it useful in practice?

No. Perhaps for a hackathon.

Re: How the Python import system works

#168
Hi! I'm the author of this article. Thanks for posting it.

I've been programming in Python for quite a while but didn't really understand how the import system works: what modules and packages are exactly; what relative imports are relative to; what's in sys.path and so on. My goal with this post was to answer all these questions.

I welcome your feedback and questions. Thanks!

Re: How the Python import system works

#169

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.

There are other purposes as well.

In the first .com wave we used a similar mechanism, just with TCL instead of Python, to ship our scripts encrypted.

Only our modified loader, written in C, could handle them.

Re: How the Python import system works

#170

Earlier quoted context omitted.

Always an informative (and fun) read: https://stackoverflow.com/questions/14132789/relative-import...

Nice, do you know of anything similarly-comprehensive that is updated for Python 3? IIRC, Python 3 simplified things a good deal by removing "implicit" relative imports, but I'm a little foggy on exactly what that means.

Explicit relative imports use a "." to indicate the file/package is from the current directory and not found elsewhere in sys.path:

  from . import foo
  from .foo import bar
Implicit relative imports don't have such an indicator:

  from foo import bar
In py2, that second one could be a relative import or from anywhere in sys.path, while in py3 those implicit relative imports were removed so that means it'll only only look in sys.path and not the local directory.

https://stackoverflow.com/questions/48716943/what-is-python-...

Post reply on HN