views:

113

answers:

6

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?

+1  A: 

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

Daniel DiPaolo
What's a good editor?
Shannon
IDLE has a pretty good editor for a beginner. Go to *File* -> *New Window* (*Ctrl+N*) and a window where you can write your program will pop up.
Cristian Ciupitu
This might help you find a good python editor: http://stackoverflow.com/questions/81584/what-ide-to-use-for-python
RevolXadda
try iPython http://ipython.scipy.org/dist/0.10/ipython-0.10.win32-setup.exe
Tumbleweed
A: 

Interactive mode is a great way to test code, and try out new ideas without writing code to a file.

To program into a python file, all you will need to do (in its simplest form) is create a file, say - test.py. Right click, and chose edit with idle.

Tim
Aha! Wonderful!
Shannon
A: 

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

Tim McNamara
+1  A: 

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
ghostdog74
A: 

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.

EddieV223
+1  A: 

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
Mark Lutton
-1: `execfile`. A bad thing to introduce to someone who can't figure out how to use the command line.
S.Lott
Why use the Windows command line when you can use Python?
Mark Lutton