0

Sample project structure

The below code in mod_b.py

from a.sub_a import mod_a

mod_a.sample()

Trying to Run Python file in the Terminal from mod_b, Getting the below error:

(base) C:\Users\%%\Desktop\python-test>C:/Users/%%/AppData/Local/Continuum/anaconda3/python.exe c:/Users/%%/Desktop/python-test/a/sub_b/mod_b.py
Traceback (most recent call last):
  File "c:/Users/%%/Desktop/python-test/a/sub_b/mod_b.py", line 1, in <module>
    from a.sub_a import mod_a
ModuleNotFoundError: No module named 'a'
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Raj
  • 11
  • 2
  • `(base) C:\Users\%%\Desktop\python-test>C:/Users/%%/AppData/Local/Continuum/anaconda3/python.exe c:/Users/%%/Desktop/python-test/a/sub_b/mod_b.py Traceback (most recent call last): File "c:/Users/%%/Desktop/python-test/a/sub_b/mod_b.py", line 1, in from .a.sub_a import mod_a ModuleNotFoundError: No module named '__main__.a'; '__main__' is not a package` – Raj Dec 19 '19 at 14:55
  • I think answer of your question is here: https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – JenilDave Dec 19 '19 at 15:03

1 Answers1

0

When executing a python file, that file's directory is added to the PYTHONPATH... and that's it.

Since "absolute" imports try to find their target from directories on the pythonpath, it can't find anything here.

You want to either:

  • move the executable scripts to your "path root" (possibly extracting the scripts from the modules)
  • configure PYTHONPATH explicitly
  • update sys.path by hand from within your script
Masklinn
  • 34,759
  • 3
  • 38
  • 57