Reputation: 2586
I have a django project with srtucture like this:
main_project
----main_project
<------libs
<<---------exceptions.py
----project_a
----project_b
In the views.py of project_a I am trying to import a folder named libs of main_project and a file from libs called exceptions.py, but I am getting the error
ImportError: No module named libs.exceptions
My code is :
from main_project.libs.exceptions import (
APIException400,
APIException405,
APIException403,
exception_handler_dispatcher,
)
Can someone tell me what am I missing here? With reference to https://stackoverflow.com/a/31407131/5080347 answer I even tried :
from main_project.main_project.libs.exceptions import (
APIException400,
APIException405,
APIException403,
exception_handler_dispatcher,
)
but doesn't work.
Upvotes: 0
Views: 76
Reputation: 10619
It seems like you forgot to add __init__.py
to libs
directory.
The
__init__.py
is used to initialize Python packages. Check the documentation to better understand how things are working.
Your structure should looks as follow:
project/
|
|-- __init__.py
|
|-- module_a/
| |-- __init__.py
| |
| |-- file1.py
| |
| |-- file2.py
|
|-- module_b/
| |
| |-- __init__.py
| |
| |-- file1.py
| |
| |-- submodule/
| | |--__init__.py
| | |
| | |-- file1.py
Upvotes: 1
Reputation: 918
When you import using from main_project.libs.exceptions
, python expects that main_project
is the package and libs
and exceptions
are submodules. So there must be a file named __init__.py
in those directories. The init.py files are required to make Python treat the directories as containing packages. For further reading please refer to here.
Upvotes: 1