ImportError: No module named - Python

后端 未结 6 1642
闹比i
闹比i 2020-11-28 04:52

I have a python application with the following directory structure:

src
 |
 +---- main
 |
 +---- util
 |
 +---- gen_py
         |
         +---- lib
<         


        
6条回答
  •  青春惊慌失措
    2020-11-28 05:26

    Your modification of sys.path assumes the current working directory is always in main/. This is not the case. Instead, just add the parent directory to sys.path:

    import sys
    import os.path
    
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    import gen_py.lib
    

    Don't forget to include a file __init__.py in gen_py and lib - otherwise, they won't be recognized as Python modules.

提交回复
热议问题