PyCharm错误:尝试导入自己的模块时("Python模块")为“无模块" [英] PyCharm error: 'No Module' when trying to import own module (python script)
问题描述
我已经编写了一个模块(文件my_mod.py
文件位于文件夹my_module
中).
当前,我正在文件cool_script.py
中,该文件位于文件夹cur_proj
中.我已经使用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的模块"
However, when I try to run cool_script.py, PyCharm tells me "No module named 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)-设置-项目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.
现在就可以使用-但是为什么?
出现了更多问题:
- 什么是内容根"和什么是源根"?为什么在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屋!