简体   繁体   English

初学者关于在 IDLE shell 中查找 Python 脚本的问题

[英]Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.我是 Python 的初学者,之前没有任何编程经验,所以请耐心等待。

I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine.我安装了 Python,开始在 Shell window 中玩它(输入变量,玩数学运算),一切都很好。 I open a New Window and started writing a simple script.我打开一个新的 Window 并开始编写一个简单的脚本。 Something like this:是这样的:

print (1+1)打印 (1+1)

I press Run Module, and I am asked to name and save the script first.我按运行模块,系统要求我先命名并保存脚本。 I do so by calling it firstscript.py, and save it to my desktop.为此,我将其命名为 firstscript.py,并将其保存到我的桌面。

Now I press Run Module, and in the shell window 2 is printed on the screen.现在我按下 Run Module,屏幕上打印出 shell window 2。 Everything is fine.一切都好。 I close Python, and open it up again.我关闭 Python,然后再次打开它。 Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.现在,在 shell window 中,我输入 firstscript.py 并收到红色消息 NameError: name 'firstscript' is not defined。

I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.只有当我在桌面上打开脚本文件并从那里按运行模块时,我才能运行我的程序,但是如果我尝试通过键入它的名称直接在 IDLE Shell 中启动它,我会收到错误消息。

What did I do wrong?我做错什么了? Thank you.谢谢你。

Good to see that you are starting with python.很高兴看到您从 python 开始。

Firstly, you can run the file directly using 'Run Module' only when you have the file open.首先,只有当文件打开时,您才能使用“运行模块”直接运行文件。 Once you save the file and quit, you are out of the file editor and back to the terminal.保存文件并退出后,您将离开文件编辑器并返回终端。 Simply typing in firstscripty.py will not work as it does not recognize the command.简单地输入 firstscripty.py 是行不通的,因为它无法识别该命令。 To run the file from the terminal, use the below code:要从终端运行文件,请使用以下代码:

python [locationOfFile\]firstscript.py

You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files你可以看看这个详细的解释: https://realpython.com/python-idle/#how-to-work-with-python-files

The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop这里的问题是 Shell 不知道你的firstscript.py在桌面上

The simplest way i suggest using cmd with:我建议使用cmd的最简单方法:

python C:\Users\{your user}\Desktop\firstscript.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM