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
Pycharm gets error "can't find '__main__' module"
提问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
回答by Hori Revens
In your Pycharm:
在你的 Pycharm 中:
- Select
Run - Edit Configurations
- In
Configuration tabs
, selectModule name
in optionChoose target to run
and type your python file's name - Click
Apply
andOK
button
- 选择
Run - Edit Configurations
- 在 中
Configuration tabs
,Module name
在选项中选择Choose target to run
并输入您的python 文件的名称 - 单击
Apply
和OK
按钮
Or the simple way is when you run your code for first time (on a new file) just type keyboard Alt+Shift+F10
to run and save the configuration. On the second time (after configuration is saved) just type Alt+F10
to 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 cmd
was 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) 中:
- Create New Project
- Give Project Name
- Right Click on bin folder
- Create New Python File
- Write Your Code
- Upper Right Side: Add Configuration
- Left side: right click on "+" sign
- Input full file name
- Home/Downloads/myfile.py as "Script path"
- python 2.x/3.x as Python Interpreter
- Click on apply / ok
- 创建新项目
- 给出项目名称
- 右键单击 bin 文件夹
- 创建新的 Python 文件
- 编写代码
- 右上角:添加配置
- 左侧:右键单击“+”号
- 输入完整文件名
- 主页/下载/myfile.py 作为“脚本路径”
- python 2.x/3.x 作为 Python 解释器
- 点击申请/确定
回答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 configuration
settings in the drop-down left of run icon on top-right of pycharm window.
您可以run/debug configuration
在 pycharm 窗口右上角的运行图标的左侧下拉菜单中找到设置。