ImportError: No module named controller.api python?

Question:

In file sched.py I import controller.api:

# -*- coding: utf-8 -*-
import controller.api

But python raises an error:

python sched.py                                                                                                                                                 
Traceback (most recent call last):                                                                                                                                                                           
  File "sched.py", line 2, in <module>                                                                                                                                                                       
    import controller.api   

File sched.py is placed in the controller folder.

Inside controller I have file api.py

Why does Python gives me this error?

Asked By: OPV

||

Answers:

You need to place an empty file named __init__.py in your ‘controller’ directory. This file denotes that directory as being a Python module. See:

https://docs.python.org/3/tutorial/modules.html

Answered By: CryptoFool

In my case (using PyCharm) I renamed the file and for some reason was throwing the ‘No Module Name’ error although I renamed it with the same name specified in my openapi file. Deleting and creating the file again with the correct name fixed the error

Answered By: José Bustamante
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.