beyond top level package error in relative import

后端 未结 13 2501
醉梦人生
醉梦人生 2020-11-22 12:50

It seems there are already quite some questions here about relative import in python 3, but after going through many of them I still didn\'t find the answer for my issue. s

13条回答
  •  礼貌的吻别
    2020-11-22 13:12

    Not sure in python 2.x but in python 3.6, assuming you are trying to run the whole suite, you just have to use -t

    -t, --top-level-directory directory Top level directory of project (defaults to start directory)

    So, on a structure like

    project_root
      |
      |----- my_module
      |          \
      |           \_____ my_class.py
      |
      \ tests
          \___ test_my_func.py
    
    

    One could for example use:

    python3 unittest discover -s /full_path/project_root/tests -t /full_path/project_root/

    And still import the my_module.my_class without major dramas.

提交回复
热议问题