1

I have the following structure:

ds/
    BST.py
    BSTNode.py

When I do

from .BSTNode import BSTNode

inside BST.py, and I run from the IDLE BST.py from within ds I receive the following error:

SystemError: Parent module '' not loaded, cannot perform relative import

Why am I receiving it? Note that I am not asking how to fix the problem, because it would work if I simply do

from BSTNode import BSTNode

i.e. without the leading .. I just want to understand better how the Python's import system works.

Note that theoretically I should not need any __init__.py within ds because I am using Python 3.5. If I am not clear enough, just ask!

Thank you!!!!

nbro
  • 15,395
  • 32
  • 113
  • 196
  • http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python – DevLounge Feb 02 '16 at 19:35
  • @Apero It doesn't help. The first answer says that the problem is that by running a module I am running it as "main", and therefore there's no package information. How do I add package information to my module? I am not running the module from the terminal but from the IDLE. And I would like that it works in both situations. – nbro Feb 02 '16 at 19:38
  • http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py – DevLounge Feb 02 '16 at 19:43
  • @Apero I still was not able to solve my problem. I set the `__package__` attribute manually to `ds` after checking that `.../ds` in on the path, but I still have the following error: `SystemError: Parent module 'ds' not loaded, cannot perform relative import` when importing like `from .BSTNode import BSTNode`. – nbro Feb 02 '16 at 20:16
  • Does adding a blank `__init__.py` help? I have never tried relative imports because too many people have problems trying to use them. – Terry Jan Reedy Feb 02 '16 at 22:03
  • @TerryJanReedy I have already it. It's incredible how something such important doesn't work decently. I am so frustrated. – nbro Feb 02 '16 at 22:32
  • 1
    me to, seems like one of the ugliest faces of python .... – enthus1ast Aug 22 '16 at 13:23

0 Answers0