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,540
  • 8
  • 39
  • 56

2 Answers2

2

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
  • 62,466
  • 11
  • 102
  • 153
1

There are two possible problems here:

  1. make sure Project is on the python path, otherwise it cannot find code
  2. code is an internal module in the python standard library which might cause name clashes, see https://docs.python.org/2/library/code.html . To avoid this, change the folder name to src, or anything else but code.
Nora
  • 11
  • 1