简体   繁体   English

如何摆脱Python中的交互模式

[英]How to get out of interactive mode in Python

This obviously an extremely novice question, but I've installed Python 2.7 and started reading the manual. 这显然是一个非常新手的问题,但我已经安装了Python 2.7并开始阅读手册。 However I looked and looked, and couldn't understand how to start programming a file rather than writing in interactive mode. 但是我看了看,并且无法理解如何开始编程文件而不是以交互模式编写。 One book that was online suggested quit(), which surprise -- quit the program. 一本在线的书建议退出(),这令人惊讶 - 退出该计划。

Should coding be done in a different program? 编码应该在不同的程序中完成吗? I'm using IDLE (Python GUI). 我正在使用IDLE(Python GUI)。 Can coding not be done within that program? 编码不能在该程序中完成吗?

Yes, coding should be done in a different program. 是的,编码应该在不同的程序中完成。 The interactive shell is very useful but it's not an editor. 交互式shell非常有用,但它不是编辑器。

您可以在文本编辑器(如vim,emacs等)中逐行编写Python代码(就像在Python交互模式下一样)...然后使用Python解释器逐行运行这些代码,并为其指定脚本名称。

$ python myscript.py

I like to use a different directory for each project. 我喜欢为每个项目使用不同的目录。 Suppose I decide to use W:/mytest as my directory. 假设我决定使用W:/ mytest作为我的目录。 First I create the directory. 首先我创建目录。

Then I start Idle. 然后我开始空闲。 I type the following: 我键入以下内容:

import os
os.chdir("W:/mytest")

This makes W:/mytest the current directory for Idle. 这使得W:/ mytest成为Idle的当前目录。

import sys
sys.path.append(".")

This changes the path so that when I "import", it will look in the current directory. 这会改变路径,这样当我“导入”时,它将在当前目录中查找。

Next I do File / New Window to open an editor window, and in that new window I select File / Save As. 接下来我执行文件/新窗口打开编辑器窗口,然后在新窗口中选择文件/另存为。 It starts in the Python home directory so I have to navigate to W:/mytest. 它从Python主目录开始,因此我必须导航到W:/ mytest。 I save this (empty) file as "test1.py". 我将此(空)文件保存为“test1.py”。

I type this into my test1.py file and save it again: 我在test1.py文件中键入它并再次保存:

""" test1.py is my test
"""

print ("This is test1.")

class Test1:
    def __init__(self):
        print ("Constructed")

This is a contrived example that can be run as a script or imported as a module. 这是一个人为的例子,可以作为脚本运行或作为模块导入。

So I have two windows now; 所以我现在有两个窗户; an editor window and the Idle "Python Shell". 编辑器窗口和空闲“Python Shell”。 I can do this in the Python Shell: 我可以在Python Shell中执行此操作:

>>> execfile("test1.py")
This is test1.
>>> import test1
This is test1
>>> tt = test1.Test1()
Constructed

在文件图标中使用新窗口工具,在python空闲本身编写程序

Push new to start making your own script file. 推送新的以开始制作自己的脚本文件。 Then when you are ready to test click run and then you can watch the results in the interactive mode, and even try new things as if you were adding code to the end of your script file, its a very useful app for debugging, testing and trying new things. 然后,当您准备测试单击运行,然后您可以在交互模式下观看结果,甚至尝试新事物,就像您在脚本文件的末尾添加代码一样,它是一个非常有用的应用程序,用于调试,测试和尝试新事物。

Also in the options you can change the way python opens your scripts when you click edit from windows, you can set it so that it opens the interactive shell or just the editor. 同样在选项中,您可以在从Windows单击编辑时更改python打开脚本的方式,您可以将其设置为打开交互式shell或仅编辑器。

要开始在文件中编码,只需打开一个新文件并开始输入。

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

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