4 ms·
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 imp
by formerly_proven 5y ago
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.
- rocqua 5y agoI 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.
- formerly_proven 5y agoThe 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.