0

I have created a python class as well as a python script, which is suppossed to call this class. The entire code lies inside a Code folder, which contains a Classes and a Scripts folder.

The class is stored in:

Classes
    > myClass.py

Whereas the script is stored in:

Scripts
    > myScript.py

I try to call the class within my script, using:

from ..Classes.myClass import MY_CLASS

Now, the error message that I receive is: ImportError: attempted relative import with no known parent package

I think this is strange, as the two dots should indicate to python that my parent directory is one level up the hierarchy. But apparently I am missing out on sth crucial here. Is there an easy fix to this problem, or do I actually have to include my Classes folder inside my Scripts folder?

Luk
  • 1,009
  • 2
  • 15
  • 33
  • Does this answer your question? [Importing from a relative path in Python](https://stackoverflow.com/questions/7505988/importing-from-a-relative-path-in-python) – Kashinath Patekar May 14 '20 at 10:43
  • actually it does, ya. I think it is strange how python does not recognize relative pathes. But well. – Luk May 14 '20 at 15:15

1 Answers1

0

Place your modules in a package:

my_app
├── Scripts
│   ├── __init__.py
│   ├── myClass.py
├── Classes
│   ├── __init__.py
│   ├── myScript.py
└── __init__.py
ruohola
  • 21,987
  • 6
  • 62
  • 97