Shannon
Shannon

Reputation: 39

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. 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). Can coding not be done within that program?

Upvotes: 3

Views: 10517

Answers (7)

Rajan Vashisht
Rajan Vashisht

Reputation: 25

To get out of python interactive mode, press the below keyboard combination

CTRL + Z

Upvotes: 0

darxtrix
darxtrix

Reputation: 2040

use new window tool in the file icon,in the python idle itself to write a program

Upvotes: 0

Mark Lutton
Mark Lutton

Reputation: 7007

I like to use a different directory for each project. Suppose I decide to use W:/mytest as my directory. 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.

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. I save this (empty) file as "test1.py".

I type this into my test1.py file and save it again:

""" 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". I can do this in the Python Shell:

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

Upvotes: 1

EddieV223
EddieV223

Reputation: 5303

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.

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342333

You write Python code line by line (as you would on Python interactive mode) in a text editor such as vim, emacs etc... Then you run these line by line code using the Python interpreter by giving it the name of your script.

$ python myscript.py

Upvotes: 1

Tim McNamara
Tim McNamara

Reputation: 18375

To start coding in a file, just open a new file and start typing.

Upvotes: -2

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

Yes, coding should be done in a different program. The interactive shell is very useful but it's not an editor.

Upvotes: 2

Related Questions