1

I have the following project structure:

Project
|
---code
|  |
|  ---__init__.py
|  ---X.py
|  ---Y.py
|  ---Z.py
|
----resources
    |
    ---__init__.py
    ---csv/
         |
         --- file1.csv
         --- file2.csv
         ---__init__.py 

Inside X.py and Y.py I have an import from code.Z import Z (where Z is the name of the class inside, and also a filename. When I want to run Z.py, it gives: `ModuleNotFoundError: No module named 'code.Z'; 'code' is not a package.

What's wrong?

mazix
  • 2,582
  • 8
  • 36
  • 55

2 Answers2

1

This is what relative imports are for.

from . import Z # use the class as Z.Z
from .Z import Z # use the class as Z

Detailed explanation on StackOverflow of the whole system.

Karl Knechtel
  • 56,349
  • 8
  • 83
  • 124
0

Have you tried:

# Importing the module:
import Z

# Calling the class in the module like this:
my_object = Z.Z()

You shouldn't even need the reference to the folder 'code', as your Z.py module is in the same folder as the files you're calling from...

Python not recognising the 'code'-folder as a package could have to do with the Project not being on path, but I am not quite sure how the details work here...

PaxTerra
  • 1
  • 1