Import file from parent directory?

后端 未结 6 1972
悲哀的现实
悲哀的现实 2020-12-08 18:25

I have the following directory structure:

application
    tests
        main.py
    main.py

application/main.py contains some functions.

6条回答
  •  Happy的楠姐
    2020-12-08 18:38

    First of all you need to make your directories into packages, by adding __init__.py files:

    application
        tests
            __init__.py
            main.py
        __init__.py
        main.py
    

    Then you should make sure that the directory above application is on sys.path. There are many ways to do that, like making the application infto a package and installing it, or just executing things in the right folder etc.

    Then your imports will work.

提交回复
热议问题