0

I've done some searching, but nothing either works or applies to this specific case.

I have a file structure like this:

- my_project
   - app.py
   - my_project
      - services
         - begin.py
         - data_analysis.py
         - model_creation.py
         - output.py

and I am trying to simply import each of the modules into the app.py file so I can run a flask application, but I keep getting an import with only one of the imports (and it is always the same one). For example, if I ran python app.py, I would get:

File "C:\Users\me\my_project\app.py", line 9, in <module>
    from my_project.services.data_analysis import analyze
ModuleNotFoundError: No module named 'my_project.services.data_analysis'

I would think it has to do with relative imports or something, only its just one of the files that is having the issue, not several/all of the files. Any ideas on why I'm getting this error?

EDIT: modified project structure.

EDIT 2: this is unique as when running app.py, it still allows for things such as from .my_project.begin import start or from my_project.model_creation import create, but no relative or non-relative import will work for just the data_analysis.py module.

gwaugh
  • 33
  • 1
  • 5
  • Does this answer your question? [Python - ModuleNotFoundError: No module named](https://stackoverflow.com/questions/61532337/python-modulenotfounderror-no-module-named) – TheFungusAmongUs Jul 29 '22 at 18:58

2 Answers2

2

add __init__.py files to your directories and subdirectories. It tells python to treat directories as modules.

example:


• my_project
   - app.py
   • my_project
      • services
         - __init__.py
         - begin.py
         - data_analysis.py
         - model_creation.py
         - output.py

In app.py:

from my_project.services.data_analysis import analyze 

I’m under the impression that services is a directory inside the second my_project folder. if it’s not, then you would import like so from services.data_analysis import analyze

Jordan
  • 540
  • 4
  • 10
  • I am trying to add an `__init__.py` to the `services` directory, but it still isn't recognizing `from .data_analysis import analyze` or `from data_analysis import analyze`. – gwaugh Jul 30 '22 at 01:31
  • Updated to add an example – Jordan Jul 30 '22 at 12:34
0

My solution to this was to copy the module file that was causing the issue, and then deleting the original file and renaming the new one. This resulted in the error immediately going away.

gwaugh
  • 33
  • 1
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 01 '22 at 12:25