Python Forum

Full Version: module import error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
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]
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.
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.