Python Forum
Getting Import error in Python program
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Import error in Python program
#1
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
Reply
#2
If dir2 is a directory it mas have __init__.py in it. The file may be empty.
Reply
#3
(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.
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
import dir1.dir2.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dynamic Module Import Error DaddyMAN 3 1,642 Jun-20-2025, 12:07 AM
Last Post: Pedroski55
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 10 26,950 May-26-2025, 01:55 PM
Last Post: deanhystad
  import module error tantony 5 6,330 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Cryptic Error with import statement Led_Zeppelin 2 4,501 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin
  Install any library via pip get an error cannot import name 'SCHEME_KEYS' from 'pip. Anldra12 2 14,342 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  getting an import statement to work in my program barryjo 1 2,830 Dec-06-2021, 04:28 PM
Last Post: snippsat
  import module with syntax error Skaperen 7 11,553 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  Import Error Christian 3 5,089 Apr-17-2021, 11:27 PM
Last Post: bowlofred
  Folium import error kashif_flavio 2 6,320 Dec-07-2020, 08:58 AM
Last Post: bowlofred
  Import error Beingcode 0 3,448 Nov-20-2020, 02:57 AM
Last Post: Beingcode

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020