Relative import issues
Since the 1.5 update relative imports appear to be partly broken. For instance, with a folder
testpkgcontaining the files
barkmod.py, the following code for
__init__.pywill not work:
import foomod # ImportError: No module named foomod from . import barmod # ValueError: Attempted relative import in non-package from .barkmod import dog # ValueError: Attempted relative import in non-package
From what I've read in various places on the internet, any directory with a
__init__.pyfile is considered a package, meaning that the above code should be valid. Many well-known Python libraries (e. g. setuptools) also use this syntax.
Interestingly enough this only happens when one of the package files is executed directly. When running
import testpkgfrom the interpreter, thus executing
__init__.py, there are no exceptions and all imports are handled correctly.
I think this works as intended, see this StackOverflow answer:
Alright, checked some more, and the explicit relative imports seem to indeed work correctly.
__package__is also very useful.
What I'm now wondering about is the first line in my example, since it doesn't explicitly import relatively, but just from the regular PATH locations. I'm almost 100% certain this was different in pysta 1.4, because this script's PATH-relative imports worked just fine, even if the script and its modules were in a subfolder. Now (at least I think it's caused by the 1.5 update) this no longer works, and the modules either need to be in a regular PATH location, or the folder name needs to also be given in the import statements.
Figured it out... apparently SOMEHOW my
os.environ["PATH"]got messed up and no longer had "
." in it. Added that back in, restarted the app, and everything worked perfectly. Sorry for the trouble.
Though I'd be very interested in what did change my PATH in the first place.