Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Relative import issues
-
Since the 1.5 update relative imports appear to be partly broken. For instance, with a folder
testpkg
containing the files__init__.py
,foomod.py
,barmod.py
andbarkmod.py
, the following code for__init__.py
will 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__.py
file 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 testpkg
from 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:
→ Attempted relative import in non-package even with init.py
-
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.
PS:
Figured it out... apparently SOMEHOW myos.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.