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
  Unable to import my own module ThePhantom 3 292 May-01-2020, 01:30 AM
Last Post: menator01
  Which app do I import to fix this error? ErnestTBass 6 316 Apr-26-2020, 08:43 PM
Last Post: snippsat
  Error while humansize module import. johnkgson 3 463 Apr-26-2020, 08:37 AM
Last Post: johnkgson
  from module import function krishanu21 4 232 Apr-22-2020, 10:58 AM
Last Post: buran
  Importing module from a package results in import error goghvv 2 278 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Import Module and Method ( dynamic ) ? JohnnyCoffee 0 187 Mar-23-2020, 04:23 PM
Last Post: JohnnyCoffee
  how to import a module which is installed to personal folder using pip install --pre? geekgeek 2 287 Mar-09-2020, 02:38 PM
Last Post: geekgeek
  module run on import? mcmxl22 3 289 Mar-02-2020, 04:37 PM
Last Post: mcmxl22
  Cannot import list from another module? t4keheart 1 227 Feb-17-2020, 10:25 PM
Last Post: t4keheart
  Python 2.7 Import error. Directory and filename conflict petcoo00 2 265 Feb-02-2020, 08:46 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)