1

I have the following structure, parts of the code are forks from other people and parts written by me

parentfolder/
       main.py
       utils.py
       fun1.py
       /childfolder/
             __init__.py
             main_child.py
             function.py
             /utils/
                 __init__.py
                 foo.py 

Now in the function.py, I imported the utils module in the parentfolder following Importing modules from parent folder

(I use an interative python console binded with vim so the __file__ solution does not work for me)

in my main_child.py, i imported function, as well fun1 in the parent folder, which import utils in the parent folder when i run main_child.py, it complains

ImportError: cannot import name 'xxx' from 'utils' (parentfolder/childfolder/utils/__init__.py)

this error is caused in importing fun1 since fun1 import utils in the parentfolder, but now it seems that it only search the filderfolder

any solutions?

sunxd
  • 743
  • 1
  • 9
  • 24
  • 1
    That is very confusing. It would be better to show imports in your code. And if you actually have this project structure, you might want to reconsider it: is there a reason for two `utils.py` files in different modules? – Yevhen Kuzmovych Apr 29 '19 at 11:34
  • in the utils folder, it is foo.py, just edited – sunxd Apr 29 '19 at 11:37
  • Take a look at [this](https://stackoverflow.com/questions/16245106/python-import-class-with-same-name-as-directory). TL;DR - better to rename package or module – Yevhen Kuzmovych Apr 29 '19 at 11:44
  • Please show the actual imports in your files. It seems you want a package, not individual modules. You should not modify PYTHONPATH or sys.path in this case. See also https://stackoverflow.com/questions/39932498/dynamically-updating-the-pythonpath-prevents-pyc-update/39932711#39932711 – MisterMiyagi Apr 29 '19 at 11:48

0 Answers0