Python Forum
Getting Import error in Python program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting Import error in Python program (/thread-2155.html)



Getting Import error in Python program - atinesh922 - Feb-23-2017

On running main.py. I'm getting the following error

Quote:ModuleNotFoundError: No module named 'dir2'

Python files

main.py

import dir1
__init__.py

import dir2
are organized as follows


src
 |
 +-- main.py
 |
 +-- dir1
       |
       +-- __init__.py
       |
       +-- dir2
Can anybody tell why the code in __init__.py cannot recognize directory dir2


RE: Getting Import error in Python program - buran - Feb-23-2017

If dir2 is a directory it mas have __init__.py in it. The file may be empty.


RE: Getting Import error in Python program - atinesh922 - Feb-23-2017

(Feb-23-2017, 04:44 AM)buran Wrote: If dir2 is a directory it mas have __init__.py in it. The file may be empty.

I tried putting empty __init__.py file, getting same error.


RE: Getting Import error in Python program - wavic - Feb-23-2017

Hello!
The dir2.py must be in the same directory as main.py. Or you have to add the path to it in the PYTHONPATH environment variable.


RE: Getting Import error in Python program - Ofnuts - Feb-23-2017

import dir1.dir2.


RE: Getting Import error in Python program - Larz60+ - Feb-23-2017

Quote:The dir2.py must be in the same directory as main.py.

It doesn't have to be in the same directory, if there is a properly formatted __init__.py file in the package root.
If interested, watch this https://www.youtube.com/watch?v=0oTh1CXRaQ0