Bottom Page

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.
Quote
#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
Quote
#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. İmage
Quote
#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.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to import entire module ? tonycstech 4 269 Dec-01-2019, 01:53 AM
Last Post: quyenca
  import error (unknown location) pseudo 1 225 Nov-14-2019, 11:47 PM
Last Post: pseudo
  When I import a Module it wont run PyNovice 17 577 Oct-26-2019, 11:14 AM
Last Post: PyNovice
  2 versions of module installed, how to import the earlier one sveto4ka 8 377 Oct-22-2019, 02:51 PM
Last Post: snippsat
  importin function vs. import module hikerguy62 2 250 Jul-28-2019, 03:13 PM
Last Post: ndc85430
  Can't import the CSV module loren41 1 421 May-31-2019, 10:05 PM
Last Post: Yoriz
  Nanpy import error Apretext 2 444 Apr-29-2019, 04:00 PM
Last Post: Apretext
  import module error german77 4 659 Feb-28-2019, 02:29 PM
Last Post: german77
  Python 3 Module Import Help Stealthychu 25 1,922 Oct-28-2018, 04:16 PM
Last Post: Larz60+
  Running pytest gives " 'cannot import name 'session' " error jasonblais 2 742 Oct-10-2018, 05:02 PM
Last Post: jasonblais

Forum Jump:


Users browsing this thread: 1 Guest(s)