Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module import error
#1
When I'm in the directory 'sub_pckg2' and run python mod3.py in the terminal, I get a modulenotfounderror but this error does not occur when I run the program from my IDE editor which in this case is Pycharm.

#pkg/sub_pckg2/mod3.py
from pkg.sub_pkg1.mod1 import foo

foo()

#pkg/sub_pckg1/mod1.py
def foo():
    print('[mod1] foo()')
If I change the code as follows, adding the two lines in bold the error goes away and I do not understand why.
#pkg/sub_pckg2/mod3.py
import sys
[b]base_dir = "/Users/me/PycharmProjects/inference_engine2/inference2/proofs/"
sys.path.append(base_dir + "/other/study/temp/")[/b]
from pkg.sub_pkg1.mod1 import foo

foo()

#pkg/sub_pckg1/mod1.py
def foo():
    print('[mod1] foo()')
Also, suppose I had 10 files in the folder sub_pckg2 and they all used mod1.foo I would then have to put
base_dir = "/Users/me/PycharmProjects/inference_engine2/inference2/proofs/"
sys.path.append(base_dir + "/other/study/temp/")
At the top of each file. Further, suppose I deleted mod1.py, I would then have to go back to all 10 files and make the proper changes. My way around this is to add a file:

#add_path.py
import sys
base_dir = "/Users/me/PycharmProjects/inference_engine2/inference2/proofs/"
sys.path.append(base_dir + "/other/study/temp/")
And then write simply at the top of each file:

import add_path

But is there a better way of doing things?

I also don't understand why I get an import error when I do the following:

##sub_pckg2.mod3.py
from . import mod4

qux()


##sub_pckg3.mod4.py
def qux():
    print ("hey")

testing

Actually, I see in the Python Cookbook that it says:

Quote:relative imports only work for modules that are located inside a proper package
But I don't know exactly what a proper package is unless it's one where each subfolder has the file __init__.py in it.
Reply
#2
you need __init__.py files in each directory so that python knows where to find things.
Here's a video on how these files should be created: https://www.youtube.com/watch?v=0oTh1CXRaQ0
Reply
#3
I've got the __init__.py files added and it is still not working. Also, a 3 hour video on module imports that's a bit much. [Image: doxZ6e]
Reply
#4
Okay, I'll take a shot. The problem is you are not using a package like a package.
You are trying to run a program within your package as main that imports other parts of the package.

Think about that. That isn't what packages are for. The way packages work is you write them so that other programs that aren't within that package can import and use them.

So let's take this structure:
This is a package. the submodules can import each other for internal use but aren't generally intended to be run as main.

So let's put this package somewhere in the path so we can import it from the main program we write. For our purpose here it will be inside the folder of our main program, but it will work as long as it is in the path that python checks for imports:
Note that the my_program folder itself does not have an __init__.py and is not itself a package.

Here are the files:
Now, if we run main.py everything should work.
Output:
mod2, bar() mod1, foo()
This is because a program always checks its own folder as an import location and our package is in the same folder (as I said as long as it is in your path it would still work). If however you try to run sub_mod2.py directly as main, you will get this error:
Error:
Traceback (most recent call last): File "sub_mod2.py", line 1, in <module> from pkg.sub_pkg1 import sub_mod1 ModuleNotFoundError: No module named 'pkg'
This s because it can't find the package (unless it is already in your path). When you run in your IDE it is adding some things to your path automatically for you, but you shouldn't assume this behavior. This is why you are needing to explicitly add the path yourself.

I hope this has helped. It isn't a particularly simple subject.
Reply
#5
Thanks that did it. It took me a very long time to find someone who could explain this to me. Even the standard experts that have been highly reliable in the past were stumped. And I agree with you about this being a hard subject, I had to read 4 or 5 separate tutorials on this subject and spend about 6 hours on it. This is one area where Van Rossum and company need to do some work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  import module with syntax error Skaperen 7 370 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  Import Error Christian 3 671 Apr-17-2021, 11:27 PM
Last Post: bowlofred
  'urllib3' Module not found when import 'requests' spanz 5 1,515 Jan-06-2021, 05:57 PM
Last Post: snippsat
  Folium import error kashif_flavio 2 681 Dec-07-2020, 08:58 AM
Last Post: bowlofred
  Import error Beingcode 0 777 Nov-20-2020, 02:57 AM
Last Post: Beingcode
Smile Import error with local file colt 1 547 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  switch from version 3.5.3 to 3.8.6 get import error python001 9 947 Oct-31-2020, 03:02 PM
Last Post: python001
  Problem with Flask Bcrypt import module marcello86 2 1,063 Aug-31-2020, 08:10 PM
Last Post: marcello86
  import yaml error for python3 maiya 4 3,607 Jul-15-2020, 06:07 PM
Last Post: Gribouillis
  Unable to import my own module ThePhantom 3 886 May-01-2020, 01:30 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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