Python import from parent directory

前端 未结 5 1216
渐次进展
渐次进展 2020-12-08 05:02

I have the following:

         ModuleFolder
              |
              |-->. ModuleFile.py .
              |
              \'-->. TestsFolder .
             


        
5条回答
  •  粉色の甜心
    2020-12-08 05:48

    You can also make symlink of the module path which you want to import and then use that symlink to import. Create a symlink in the python dist-packages.

    To create a symlink:

    ln -s "/path/to/ModuleFolder" "/path/to/python/dist/packages/module_symlink_name"

    To import module in the script:

    from module_symlink_name import ModuleFile
    

    No need to export python path or modifying sys path.

提交回复
热议问题