News for hackers. Stuff that matters.
Re:How the Python import system works by blakesley on Sunday July 25, 2021 @01:14AM (#27946887)
> 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 by rocqua on Sunday July 25, 2021 @04:02AM (#27947561)
I missed one detail. The files and the script are in the same folder.

I say script here because the apparent intention is that scripts and modules are separate. That is why it's not easy to import functions from a file in the same directory as a script.

This restriction is very unexpected. And it is not borne out well by the fact that it is not obvious this distinction exists.

Re:How the Python import system works by formerly_proven on Sunday July 25, 2021 @06:00AM (#27948024)
When you run a script, the directory containing it is on the path. You can just write "from somefile import something". What you can't do is "from .somefile import something" because it is not a package.

When you run a module, the root package containing it is on the path by definition and imports work.

Re:How the Python import system works by rocqua on Sunday July 25, 2021 @06:15AM (#27948087)
I didn't know that.

Does that still work if you call the script from a different directory (so `python bar/foo.py`)?

Imma guess it doesn't work with symlinked scripts. But I don't think it should without doing an install, in which case a module does make sense.

Re:How the Python import system works by formerly_proven on Sunday July 25, 2021 @06:23AM (#27948124)
The directory containing the file is added to the path, and the named file is imported as the __main__ module. That's why "if __name__ == '__main__':" works in script files.

I don't think Python will resolve symlinks here, so if that needs to work, you probably need to do it yourself before touching other code / files.