Python Forum

Full Version: Getting Import error in Python program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
If dir2 is a directory it mas have __init__.py in it. The file may be empty.
(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.
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.
import dir1.dir2.
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