问题:PyCharm错误:尝试导入自己的模块时(“ Python模块”)为“无模块”

我已经编写了一个模块(位于文件my_mod.py夹中的文件文件my_module)。目前,我正在处理文件cool_script.py夹中的文件cur_proj。我已经使用File-open在PyCharm中打开了该文件夹(我假设是一个PyCharm项目)。

在ProjectView(CMD-7)中,我可以看到我的项目cur_proj(红色),并且在“外部库”下可以看到my_module。在cool_script.py中,我可以写

from my_module import my_mod as mm

PyCharm甚至建议my_mod。到目前为止,一切都很好。

但是,当我尝试运行cool_script.py时,PyCharm告诉我 “没有名为my_module的模块”

我觉得这很奇怪,因为

A)在终端(OS 10.10.2)中,在python中,我可以导入模块没有问题-.bashrc中的PYTHONPATH中有相应的条目

B)在PyCharm中-设置-项目cur_proj-项目解释器-python解释器旁边的CogWheel-更多-显示所选解释器图标的路径,PYTHONPATH的路径确实出现(我认为应该如此)

因此,为什么在尝试运行cool_script.py时出现错误?- 我想念什么?

笔记:

附录2015年2月25日

当我进入PyCharm以运行-编辑配置时,对于当前项目,有两个选中的选项带有复选标记:“ 将内容根添加到PYTHONPATH ”和“ 将源根添加到PYTHONPATH ”。当我都取消选中时,我可以加载我的模块。

现在就可以了- 但是为什么呢?

出现了其他问题:

  • 什么是“内容根”和什么是“源根”?为什么在PYTHONPATH中添加一些内容却使其以某种方式中断?
  • 我是否应该一直取消选中这两个选项(在默认设置中也要取消选中这两个选项,而不是仅针对项目特定的配置(“运行/调试配置”对话框的左侧面板)?

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).

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

and PyCharm even makes suggestion for my_mod. So far so good.

However, when I try to run cool_script.py, PyCharm tells me “No module named my_module”

This seems strange to me, because

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) 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)

Hence, why do I get the error when I try to run cool_script.py? — What am I missing?

Notes:

Addendum 2015-Feb-25

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?

Further questions emerged:

  • What are “content roots” and what are “source roots”? And why does adding something to the PYTHONPATH make it somehow break?
  • should I uncheck both of those options all the time (so also in the defaults, not only the project specific configurations (left panel of the Run/Debug Configurations dialog)?

回答 0

如果您自己的模块在同一路径中,则需要将该路径标记为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.

I hope this helps.


回答 1

所以如果你去

->设置->项目:My_project->项目结构,

仅提供源代码所在的目录,并将其标记为“源”(您可以在同一窗口中看到它)。带有源代码的目录应变为蓝色。现在,您可以导入位于同一目录中的模块。

So if you go to

-> Setting -> Project:My_project -> Project Structure,

Just the directory in which the source code is available and mark it as “Sources” (You can see it on the same window). The directory with source code should turn blue. Now u can import in modules residing in same directory.


回答 2

PyCharm社区/专业版2018.2.1

我现在遇到了这个问题,并且能够以类似于@Beatriz Fonseca和@Julie指出的方式解决它。

如果转到File-> Settings-> Project: YourProjectName-> Project Structure,则将具有当前正在处理的项目的目录布局。必须浏览目录并将其标记Source为所有源文件的目录,或作为Resource严格导入文件的文件夹。

您还需要确保将__init__.py文件放置在资源目录中,或者实际上是要导入的任何位置,并且可以很好地工作。

我希望这个答案对某人有帮助,并希望JetBrains可以解决此烦人的错误。

PyCharm Community/Professional 2018.2.1

I was having this problem just now and I was able to solve it in sort of a similar way that @Beatriz Fonseca and @Julie pointed out.

If you go to File -> Settings -> Project: YourProjectName -> Project Structure, you’ll have a directory layout of the project you’re currently working in. You’ll have to go through your directories and label them as being either the Source directory for all your Source files, or as a Resource folder for files that are strictly for importing.

You’ll also want to make sure that you place __init__.py files within your resource directories, or really anywhere that you want to import from, and it’ll work perfectly fine.

I hope this answer helps someone, and hopefully JetBrains will fix this annoying bug.


回答 3

我尝试的是获取文件所在的位置。

例如 E:\git_projects\My_project\__init__.py is my location.

我转到文件->设置->项目:My_project->项目结构,然后将内容根添加到要提到的地方 E:\git_projects\My_project

它为我工作。

What I tried is to source the location where my files are.

e.g. E:\git_projects\My_project\__init__.py is my location.

I went to File -> Setting -> Project:My_project -> Project Structure and added the content root to about mention place E:\git_projects\My_project

it worked for me.


回答 4

my_module是不是模块的文件夹,您不能导入文件夹,请尝试移至my_mod.py与相同的文件夹cool_script.py,然后执行import my_mod as mm。这是因为python仅在当前目录和中sys.path查找,因此my_mod.py除非在同一目录中,否则不会找到

或者,您可以在此处找到答案,告诉您如何从其他目录导入。

关于您的其他问题,我不知道我不使用PyCharm。

my_module is a folder not a module and you can’t import a folder, try moving my_mod.py to the same folder as the cool_script.py and then doimport my_mod as mm. This is because python only looks in the current directory and sys.path, and so wont find my_mod.py unless it’s in the same directory

Or you can look here for an answer telling you how to import from other directories.

As to your other questions, I do not know as I do not use PyCharm.


回答 5

我也收到“将源根添加到PYTHONPATH”错误。我的问题是我有两个具有相同名称的文件夹,例如project/subproject1/thing/src和,project/subproject2/thing/src并且两个文件夹都标记为源根。当我将其中一个"thing"文件夹重命名为"thing1"(任何唯一的名称)时,它起作用了。

也许PyCharm自动添加选定的源根目录,它不会使用完整路径,因此会混淆具有相同名称的文件夹。

I was getting the error with “Add source roots to PYTHONPATH” as well. My problem was that I had two folders with the same name, like project/subproject1/thing/src and project/subproject2/thing/src and I had both of them marked as source root. When I renamed one of the "thing" folders to "thing1" (any unique name), it worked.

Maybe if PyCharm automatically adds selected source roots, it doesn’t use the full path and hence mixes up folders with the same name.


回答 6

当Python解释器找不到您的代码时,可能会导致这种情况。您必须向Python明确提及才能在此位置找到您的代码。

为此:

  • 转到您的python控制台
  • 添加sys.path.extend(['your module location'])到Python控制台。

在您的情况下:

  • 转到您的python控制台,
  • 首先,编写以下代码:

    import sys
    sys.path.extend([my module URI location])
  • 编写此语句后,可以运行以下命令:

    from mymodule import functions

This can be caused when Python interpreter can’t find your code. You have to mention explicitly to Python to find your code in this location.

To do so:

  • Go to your python console
  • Add sys.path.extend(['your module location']) to Python console.

In your case:

  • Go to your python console,
  • On the start, write the following code:

    import sys
    sys.path.extend([my module URI location])
    
  • Once you have written this statement you can run following command:

    from mymodule import functions
    

回答 7

必须完成的关键混乱步骤是为您要执行的源文件重新创建运行配置,以便IDE选择新路径。

对我而言,真正有效的方法是转到“运行/编辑配置…”,为您要在左侧运行的文件选择配置,取消选中“将源根添加到PYTHONPATH”框,保存,然后返回并选中该框并保存。然后它会工作。

The key confusing step that must be done is to recreate the run configuration for the source file that you’re trying to execute, so that the IDE picks up the new paths.

The way that actually worked for me was to go to Run/Edit Configurations…, select the configuration for the file that you’re trying to run on the left side, uncheck the “Add source roots to PYTHONPATH” box, save, and then go back and check the box and save. THEN it would work.


回答 8

内容根是保存项目代码的文件夹,而源根也被定义为相同的文件夹。我唯一了解的区别是,源根目录中的代码是在内容根目录中的代码之前构建的。

取消选中它们不会影响运行时,直到您没有在程序包中创建单独的模块(这些模块已手动连接到Django)为止。这意味着,如果您的任何文件不包含“ from django import …”或未通过django调用任何功能,则取消选中这两个选项将导致故障。

更新-仅在使用Virtual Environmanet时出现问题,并且仅在通过提供的终端控制项目时出现。因为终端仍然可以通过默认系统pyhtonpath而不是虚拟环境工作。而python django控制面板可以正常工作。

Content roots are folders holding your project code while source roots are defined as same too. The only difference i came to understand was that the code in source roots is built before the code in the content root.

Unchecking them wouldn’t affect the runtime till the point you’re not making separate modules in your package which are manually connected to Django. That means if any of your files do not hold the ‘from django import…’ or any of the function isn’t called via django, unchecking these 2 options will result in a malfunction.

Update – the problem only arises when using Virtual Environmanet, and only when controlling the project via the provided terminal. Cause the terminal still works via the default system pyhtonpath and not the virtual env. while the python django control panel works fine.


回答 9

解决此问题而不必将目录标记为源根的解决方案是编辑运行配置,然后在执行中选择选项“重定向输入自”,然后选择要运行的脚本。之所以可行,是因为随后将其视为脚本是在此目录中以交互方式运行。但是,Python仍会使用错误“没有名为x的模块”来标记模块名称:

从以下位置重定向输入:

解释器执行import语句时,它将在从以下来源汇编的目录列表中搜索x.py:

  1. 运行输入脚本的目录或当前目录(如果解释器正在交互运行)
  2. PYTHONPATH环境变量(如果已设置)中包含的目录列表。
  3. 在安装Python时配置的与安装有关的目录列表,在我的情况下为Ubuntu上的usr / lib / python3.6。

The solution for this problem without having to Mark Directory as Source Root is to Edit Run Configurations and in Execution select the option “Redirect input from” and choose script you want to run. This works because it is then treated as if the script was run interactively in this directory. However Python will still mark the module name with an error “no module named x”:

Redirect input from:

When the interpreter executes the import statement, it searches for x.py in a list of directories assembled from the following sources:

  1. The directory from which the input script was run or the current directory if the interpreter is being run interactively
  2. The list of directories contained in the PYTHONPATH environment variable, if it is set.
  3. An installation-dependent list of directories configured at the time Python is installed, in my case usr/lib/python3.6 on Ubuntu.

回答 10

Pycharm 2017.1.1

  1. 点击View->ToolBarView->Tool Buttons
  2. 在左窗格Project上将可见,右键单击它,然后按Autoscroll to source ,然后运行您的代码。

这对我有用。

Pycharm 2017.1.1

  1. Click on View->ToolBar & View->Tool Buttons
  2. On the left pane Project would be visible, right click on it and press Autoscroll to source and then run your code.

This worked for me.


回答 11

ln -s someProject

如果您有someDirectory / someProjectDir和两个文件,则file1.py和file2.py,以及file1.py尝试使用此行导入

从someProjectDir导入file2

即使您已将someProjectDir指定为源目录,并且即使它在首选项中也将项目,项目结构菜单显示为内容根目录,它也无法使用。它将起作用的唯一方法是通过链接如上所示的项目(unix命令,在Mac中工作,不确定Windows的使用或语法)。由于软链接是由Pycharm在依赖项目中创建的,因此似乎存在某种机制,Pycharm可以在从版本控制签出或作为上下文根添加时自动执行此操作。因此,尽管复制了相同的内容,但奇怪的是目录的复制令人烦恼,而必要性却令人困惑。同样在自动创建的依赖项中,它在版本控制下也不显示为新目录。.idea文件的比较可能会发现更多。

ln -s . someProject

If you have someDirectory/someProjectDir and two files, file1.py and file2.py, and file1.py tries to import with this line

from someProjectDir import file2

It won’t work, even if you have designated the someProjectDir as a source directory, and even if it shows in preferences, project, project structure menu as a content root. The only way it will work is by linking the project as show above (unix command, works in mac, not sure of use or syntax for Windows). There seems some mechanism where Pycharm does this automatically either in checkout from version control or adding as context root, since the soft link was created by Pycharm in a dependent project. Hence, just copying the same, although the weird replication of directory is annoying and necessity is perplexing. Also in the dependency where auto created, it doesn’t show as new directory under version control. Perhaps comparison of .idea files will reveal more.


回答 12

对我有用的答案确实是OP在他的2015年更新中提到的内容:取消选中Python运行配置中的以下两个框

  • “将内容根添加到PYTHONPATH”
  • “将源根添加到PYTHONPATH”

我已经将run config设置为使用正确的venv,因此PyCharm不需要做其他工作即可向路径添加内容。相反,它导致了错误。

The answer that worked for me was indeed what OP mentions in his 2015 update: uncheck these two boxes in your Python run config:

  • “Add content roots to PYTHONPATH”
  • “Add source roots to PYTHONPATH”

I already had the run config set to use the proper venv, so PyCharm doing additional work to add things to the path was not necessary. Instead it was causing errors.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。