如何通知 PyCharm 自定义模块的位置? [英] How do I inform PyCharm of the location of custom modules?
问题描述
我有一个使用以下结构设置的应用程序.
I have an application at work that is set up with the following structure.
/project_root
/applications
/app1
__init__.py
/app2
__init__.py
...
/appN
/pkg
/database
__init__.py
/toolbox
__init__.py
...
__init__.py
__init__.py
main_framework.py
我正在研究使用 PyCharm(社区版),而不是 Eclipse 的 PyDev 工具.在 app1
的 __init__.py
中,我有一个如下所示的导入语句:
I'm investigating using PyCharm (community edition), instead of Eclipse's PyDev tool. In __init__.py
in app1
I have an import statement that looks like this:
import pkg.database
PyCharm 使用 no module named pkg
消息抱怨导入.
PyCharm complains about the import with a no module named pkg
message.
(点击查看大图)
不过,由于框架的构建方式,系统运行得很好.每个 appN
都知道 pkg
目录.我如何通知 PyCharm 我在 pkg
目录中有自定义构建的模块,以便这些警告停止?
Due to how the framework is built, though, the system runs just fine. Each of the appN
s know about the pkg
directory. How do I inform PyCharm that I have custom built modules in the pkg
directory so that these warnings stop?
我正在测试 PyCharm 4.0.3 社区版.
I am testing PyCharm 4.0.3 Community Edition.
我尝试了几件事,感谢下面的评论,但没有帮助:
A couple things I've tried, thanks for comments below, that haven't helped:
- 在项目结构"对话框中,project_root 被列为对话框右侧内容根目录"下的唯一目录.我已经添加了
pkg
目录,以便它也被列出.这没有效果. - 在问题的初始版本中,我在显示结构时错过了
pkg
中的__init__.py
.该文件确实存在.
- In the Project Structure dialog, project_root was listed as the only directory under "Content Root" on the right hand side of the dialog. I've added the
pkg
directory so that it is listed as well. This had no effect. - In the initial version of the question, I missed the
__init__.py
inpkg
when displaying the structure. This file does exist.
推荐答案
解决方案是一个两步过程:
The solution to this was a two step process:
- 添加
pkg
目录作为源根目录.通过选择File
->Settings
->Project
->(选择项目)->Project Structure
然后选择pkg
目录并通过单击Sources
按钮添加.单击确定. - 然后选择
File
->Invalidate Caches/Restart
->Invalidate and Restart
- Add the
pkg
directory as a source root. Do this by selectingFile
->Settings
->Project
-> (select the project) ->Project Structure
then select thepkg
directory and add by clicking theSources
button. Click Ok. - Then select
File
->Invalidate Caches / Restart
->Invalidate and Restart
等待 PyCharm 重新启动并重建它的缓存/扫描索引.现在 pkg
目录被检测为源根目录,我的错误消失了.
Wait for PyCharm to restart and rebuild it's cache / scan the indexes. Now the pkg
directory is detected as a source root and my errors are gone.
这篇关于如何通知 PyCharm 自定义模块的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!