I know there are already a lot of questions similar to this one but unfortunately I haven't found out how to apply to my problem.
The structure of my project is as follows:
Project
|___ maincode
|___ __init__.py
|___ losses.py
|___ bin
|___ __init__.py
|___ train.py
and then in train.py
I have the following code:
# Allow relative imports when being executed as script.
if __name__ == "__main__" and __package__ is None:
__package__ = "maincode.bin"
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from .. import losses
def main(args=None):
#some code here
if __name__ == '__main__':
main()
Now if I execute the script train.py
from the project main directory Project/
, i.e. running the following in the terminal:
maincode/bin/train.py
then I obtain the following error:
SystemError: Parent module 'maincode.bin' not loaded, cannot perform relative import
I tried both with Python 2.7.6 and Python 3.5.2 and obtained the same error.
Could you please help? Thank you in advance!