简体   繁体   English

PyCharm自动导入无法正确导入

[英]PyCharm auto-import Fails To Import Properly

I have an app structured like so: 我有一个结构如下的应用程序:

api/
|_app/
    |_ __init__.py
    |_conf/
    |_resources/
        |_ __init__.py
        |_controller.py
    |_dao/
        |_ __init__.py
        |_thing_dao.py
    |_etc...

I want to use the function get_thing_by_id in thing_dao.py inside of controller.py . 我想使用的功能get_thing_by_idthing_dao.pycontroller.py In PyCharm I start to type get_thing_by_id and it prompts me to auto-import the function. 在PyCharm中,我开始键入get_thing_by_id ,它提示我自动导入该函数。 The problem is it simply does from thing_dao import get_thing_by_id but when I attempt to run the app (Flask) I get ImportError: No module named 'thing_dao' . 问题是,它只是from thing_dao import get_thing_by_id但是当我尝试运行应用程序(Flask)时,我得到ImportError: No module named 'thing_dao' What I end up having to do is a relative import or start at the module level from app.dao.thing_dao import get_thing_by_id . 我最终要做的是相对导入或from app.dao.thing_dao import get_thing_by_id的模块级别开始。

I suspect this issue is related to my project structure and less of a PyCharm bug. 我怀疑此问题与我的项目结构有关,而与PyCharm错误无关。 Is there a way I could structure my project to better hint to PyCharm how to do imports? 有没有办法构造我的项目,以更好地提示PyCharm如何进行导入? I've looked through the configuration option for auto-imports and PyCharm and they're quite slim so I suspect that I'm doing something wrong. 我已经查看了自动导入和PyCharm的配置选项,它们的体积很小,因此我怀疑自己做错了什么。

I discovered I had my directories marked as a "Source Root". 我发现我的目录标记为“源根”。 Unmarking them made the imports work properly. 取消标记它们可以使导入正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM