Python Pycharm 收到错误“找不到‘__main__’模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52030496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:00:59  来源:igfitidea点击:

Pycharm gets error "can't find '__main__' module"

pythonpycharm

提问by Mladen Milosavljevic

Whenever I try to run script via Virtualenv in pycharm, i get this error:

每当我尝试在 pycharm 中通过 Virtualenv 运行脚本时,我都会收到此错误:

C:\Users\Costello\PycharmProjects\test\venv\Scripts\python.exe C:/Users/Costello/PycharmProjects/test
C:\Users\Costello\PycharmProjects\test\venv\Scripts\python.exe: can't find '__main__' module in 'C:/Users/Costello/PycharmProjects/test'

Everything works fine via idle, or vs code. I figure it must be something with a way that I have setup my pycharm but have no idea what.

通过空闲或 vs 代码一切正常。我想这一定是我设置了我的 pycharm 但不知道是什么。

edit: this happens whatever I run, even simple print function.

编辑:无论我运行什么都会发生这种情况,即使是简单的打印功能。

edit: even with choosing regular python intepreteur, same happens just in pycharm

编辑:即使选择常规的 python intepreteur,在 pycharm 中也会发生同样的情况

C:\Users\Costello\AppData\Local\Programs\Python\Python37\python.exe: can't find '__main__' module in 'C:/Users/Costello/PycharmProjects/test'

回答by Mladen Milosavljevic

Figured out whats wrong. In the configuration window in pycharm, I had to select proper script path:

弄清楚出了什么问题。在 pycharm 的配置窗口中,我必须选择正确的脚本路径:

enter image description here

在此处输入图片说明

回答by Hori Revens

In your Pycharm:

在你的 Pycharm 中:

  1. Select Run - Edit Configurations
  2. In Configuration tabs, select Module namein option Choose target to runand type your python file's name
  3. Click Applyand OKbutton
  1. 选择 Run - Edit Configurations
  2. 在 中Configuration tabsModule name在选项中选择Choose target to run并输入您的python 文件的名称
  3. 单击ApplyOK按钮

Or the simple way is when you run your code for first time (on a new file) just type keyboard Alt+Shift+F10to run and save the configuration. On the second time (after configuration is saved) just type Alt+F10to run your code.

或者,简单的方法是当您第一次运行代码时(在新文件上),只需键入键盘Alt+Shift+F10即可运行并保存配置。第二次(保存配置后)只需键入Alt+F10即可运行您的代码。

回答by Nitesh Patil

Go to "Edit Configuration" and only speciy your filename like filename.py

转到“编辑配置”并仅指定您的文件名,例如 filename.py

Existing path -----> C:\Users\np4\PycharmProjects\TEST\venv

现有路径-----> C:\Users\np4\PycharmProjects\TEST\venv

Try with this -----> C:\Users\np4\PycharmProjects\TEST\venv\MultiSites.py

试试这个 -----> C:\Users\np4\PycharmProjects\TEST\venv\MultiSites.py

回答by Mohsen Hrt

I encountered this problem when my cmdwas forced to "End Task" by "Task Manager". My problem solved when I restart my IDE.

当我cmd被“任务管理器”强制“结束任务”时,我遇到了这个问题。当我重新启动 IDE 时,我的问题解决了。

回答by Togrul

"Open 'Edit Run/Debug configurations' dialog" (on the top, next to "Run") "Edit Configurations" "script path:" --> select correct script path.

“打开‘编辑运行/调试配置’对话框”(在顶部,“运行”旁边)“编辑配置”“脚本路径:”-->选择正确的脚本路径。

回答by Pradip74

In Pycharm (Ubuntu):

在 Pycharm (Ubuntu) 中:

  1. Create New Project
  2. Give Project Name
  3. Right Click on bin folder
  4. Create New Python File
  5. Write Your Code
  6. Upper Right Side: Add Configuration
  7. Left side: right click on "+" sign
  8. Input full file name
  9. Home/Downloads/myfile.py as "Script path"
  10. python 2.x/3.x as Python Interpreter
  11. Click on apply / ok
  1. 创建新项目
  2. 给出项目名称
  3. 右键单击 bin 文件夹
  4. 创建新的 Python 文件
  5. 编写代码
  6. 右上角:添加配置
  7. 左侧:右键单击“+”号
  8. 输入完整文件名
  9. 主页/下载/myfile.py 作为“脚本路径”
  10. python 2.x/3.x 作为 Python 解释器
  11. 点击申请/确定

回答by Aditya Gupta

I fixed it by removing values from Interpreter optionsfield in run/debug configurations. While trying to add an interpreter, I had added the python path to the mentioned field.

我通过从运行/调试配置中的解释器选项字段中删除值来修复它。在尝试添加解释器时,我已将 python 路径添加到提到的字段中。

After clearing out the field all worked well.

清理完场地后一切顺利。

You can find run/debug configurationsettings in the drop-down left of run icon on top-right of pycharm window.

您可以run/debug configuration在 pycharm 窗口右上角的运行图标的左侧下拉菜单中找到设置。