Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 module import error
When I'm in the directory 'sub_pckg2' and run python 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.

from pkg.sub_pkg1.mod1 import foo


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.
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


def foo():
    print('[mod1] foo()')
Also, suppose I had 10 files in the folder sub_pckg2 and they all used 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, 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:
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:
from . import mod4

def qux():
    print ("hey")


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 in it.
you need files in each directory so that python knows where to find things.
Here's a video on how these files should be created:
I've got the files added and it is still not working. Also, a 3 hour video on module imports that's a bit much. İmage
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 and is not itself a package.

Here are the files:
Now, if we run everything should work.
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 directly as main, you will get this error:
Traceback (most recent call last): File "", 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.

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)