PyCharm 错误:尝试导入自己的模块时出现“无模块"(python 脚本) [英] PyCharm error: 'No Module' when trying to import own module (python script)
问题描述
我编写了一个模块(位于my_module
文件夹中的文件my_mod.py
文件).目前,我正在处理位于 cur_proj
文件夹中的文件 cool_script.py
.我已经使用 File -- open 在 PyCharm 中打开了文件夹(我假设,因此,它是一个 PyCharm 项目).
I have written a module (a file my_mod.py
file residing in the folder my_module
).
Currently, I am working in the file cool_script.py
that resides in the folder cur_proj
. I have opened the folder in PyCharm using File -- open (and I assume, hence, it is a PyCharm project).
在 ProjectView (CMD-7) 中,我可以看到我的项目 cur_proj
(红色),在外部库"下我可以看到 my_module
.在cool_script.py中,我可以写
In ProjectView (CMD-7), I can see my project cur_proj
(in red) and under "External Libraries" I do see my_module
. In cool_script.py, I can write
from my_module import my_mod as mm
而且 PyCharm 甚至为 my_mod 提出了建议.到目前为止一切顺利.
and PyCharm even makes suggestion for my_mod. So far so good.
但是,当我尝试运行 cool_script.py 时,PyCharm 告诉我没有名为 my_module 的模块"
这对我来说似乎很奇怪,因为
This seems strange to me, because
A) 在终端 (OS 10.10.2) 中,在 python 中,我可以导入模块没问题——.bashrc 中的 PYTHONPATH 中有相应的条目
A) in the terminal (OS 10.10.2), in python, I can import the module no problem -- there is a corresponding entry in the PYTHONPATH in .bashrc
B) 在 PyCharm -- 设置 -- 项目 cur_proj -- 项目解释器 -- python 解释器旁边的 CogWheel -- 更多 -- 显示所选解释器图标的路径,来自 PYTHONPATH 的路径确实出现(我认为它们应该)
B) in PyCharm -- Settings -- Project cur_proj -- Project Interpreter -- CogWheel next to python interpreter -- more -- show paths for selected interpreter icon, the paths from PYTHONPATH do appear (as I think they should)
因此,当我尝试运行cool_script.py 时为什么会出现错误?-- 我错过了什么?
Hence, why do I get the error when I try to run cool_script.py? -- What am I missing?
注意事项:
- 我不是在cool_script.py 的顶部声明一个不同的/特殊的python 版本
- 我确保
my_module
的路径是对的 - 我放入
__init__.py
文件(空文件) 在my_module
和cur_proj
中 - 我没有使用
virtualenv
- I am not declaring a different / special python version at the top of cool_script.py
- I made sure that the path to
my_module
is correct - I put
__init__.py
files (empty files) both inmy_module
and incur_proj
- I am not using
virtualenv
当我进入 PyCharm 运行 -- 编辑配置时,对于我当前的项目,有两个带有复选标记的选项被选中:将内容根目录添加到 PYTHONPATH"和将源根目录添加到 PYTHONPATH".当我都取消选中时,我可以加载我的模块.
When I go in PyCharm to Run -- Edit Configurations, for my current project, there are two options that are selected with a check mark: "Add content roots to PYTHONPATH" and "Add source roots to PYTHONPATH". When I have both unchecked, I can load my module.
所以它现在起作用了——但为什么?
So it works now -- but why?
出现了更多问题:
- 什么是内容根",什么是源根"?为什么在 PYTHONPATH 中添加一些东西会使它以某种方式中断?
- 我是否应该一直取消选中这两个选项(在默认设置中也是如此,不仅是项目特定的配置(运行/调试配置"对话框的左侧面板)?
推荐答案
如果你自己的模块在同一路径,你需要将路径标记为Sources Root
.在项目资源管理器中,右键单击要导入的目录.然后选择Mark Directory As
并选择Sources Root
.
If your own module is in the same path, you need mark the path as Sources Root
. In the project explorer, right-click on the directory that you want import. Then select Mark Directory As
and select Sources Root
.
这篇关于PyCharm 错误:尝试导入自己的模块时出现“无模块"(python 脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!