PyCharm错误:尝试导入自己的模块时("Python模块")为“无模块" [英] PyCharm error: 'No Module' when trying to import own module (python script)

查看:260
本文介绍了PyCharm错误:尝试导入自己的模块时("Python模块")为“无模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个模块(文件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?

注意:

  • 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 in my_module and in cur_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆