Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named “code.X”; code is not a package, Python

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?

like image 944
mazix Avatar asked Dec 08 '25 17:12

mazix


1 Answers

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.
like image 157
Nora Avatar answered Dec 11 '25 05:12

Nora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!