Relative imports in Python 3

前端 未结 16 1070
误落风尘
误落风尘 2020-11-21 06:42

I want to import a function from another file in the same directory.

Sometimes it works for me with from .mymodule import myfunction but sometimes I get

16条回答
  •  逝去的感伤
    2020-11-21 07:05

    I needed to run python3 from the main project directory to make it work.

    For example, if the project has the following structure:

    project_demo/
    ├── main.py
    ├── some_package/
    │   ├── __init__.py
    │   └── project_configs.py
    └── test/
        └── test_project_configs.py
    

    Solution

    I would run python3 inside folder project_demo/ and then perform a

    from some_package import project_configs
    

提交回复
热议问题