Sep-16-2018, 11:11 PM
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.
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:
testing
Actually, I see in the Python Cookbook that it says:
#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 packageBut I don't know exactly what a proper package is unless it's one where each subfolder has the file __init__.py in it.