Section B.4. Running Python Programs
Python code can be typed at a >>> interactive prompt, run from a C program, or placed in text files and run. There is a variety of ways to run code in files:
- Running from a command line
-
Python files can always be run by typing a command of the form python file.py in your system shell or console box, as long as the Python interpreter program is on your systems search path. On Windows, you can type this command in an MS-DOS console box; on Linux, use an xterm.
- Running by clicking
-
Depending on your platform, you can usually start Python program files by double-clicking on their icons in a file explorer user interface. On Windows, for instance, .py Python files are automatically registered such that they can be run by being clicked (as are .pyc and .pyw files).
- Running by importing and reloading
-
Files can also be run by importing them, either interactively or from within another module file. To rerun a module files code again without exiting Python, be sure to run a call like reload(module).
- Running files in IDLE
-
For many, running a console window and one or more separate text editor windows constitutes an adequate Python development environment. For others, IDLE -- the Python Integrated Development Environment (but really named for Monty Pythons Eric Idle) -- is a development environment GUI for Python. It can also be used to run existing program files or develop new systems from scratch. IDLE is written in Python/Tkinter, and thus is portable across Windows, X Windows (Unix), and Macintosh. It ships (for free) as a standard tool with the Python interpreter. On Windows, IDLE is installed automatically with Python; see Section B.1.1 under Section B.1 earlier in this appendix.
IDLE lets you edit, debug, and run Python programs. It does syntax coloring for edited Python code, sports an object browser the lets you step through your systems objects in parallel with its source code, and offers a point-and-click debugger interface for Python. See IDLEs help text and page at http://www.python.org for more details. Or simply play with it on your machine; most of its interfaces are intuitive and easy to learn. The only thing IDLE seems to lack today is a point-and-click GUI builder (but Tkinters simplicity tends to make such builders less important in Python work).
- Running files in Pythonwin
-
Pythonwin is another freely available, open source IDE for Python, but is targeted at Windows platforms only. It makes use of the MFC integration made available to Python programmers in the win32all Windows-specific Python extensions package described in Chapter 15. In fact, Pythonwin is something of an example application of these Windows tools. Pythonwin supports source code editing and launching much like IDLE does (and there has been some cross-pollination between these systems). It doesn sport all the features or portability of IDLE, but offers tools all its own for Windows developers. Fetch and install the win32all Windows extensions package to experiment with Pythonwin. You can also find this package on this books CD (see http://examples.oreilly.com/python2).
- Running Python from other IDEs
-
If you are accustomed to more sophisticated development environments, see the Visual Python products from Active State (http://www.activestate.com), and the PythonWorks products from PythonWare (http://www.pythonware.com). Both are emerging as I write this, and promise to provide advanced integrated development tool suites for Python programmers. For instance, ActiveStates plans include support for developing Python programs under both Microsofts Visual Studio and the Mozilla application environment, as well as a Python port to the C#/.NET environment. PythonWares products support visual interface development and a suite of development tools.
Other platforms have additional ways to launch Python programs (e.g., dropping files on Mac icons). Here are a few more hints for Unix and Windows users:
- Unix and Linux users
-
You can also make Python module files directly executable by adding the special #!/usr/bin/python type line at the top of the file and giving the file executable permissions with a chmod command. If you do, you can run Python files simply by typing their names (e.g., file.py ), as though they were compiled executables. See Chapter 2, for more details.
- Windows users
-
If you see a flash when you click on a Python program in the file explorer, its probably the console box that Python pops up to represent the programs standard input/output streams. If you want to keep the programs output up so that you can see it, add a call raw_input( ) to the bottom of your program; this call pauses until you press the Enter key. If you write your own GUI and don want to see the console pop-up at all, name your source files with a .pyw extension instead of .py.
- Windows NT and 2000
-
You can also launch a Python script file from a command-line prompt simply by typing the name of the scripts file (e.g., file.py ). These platforms correctly run the script with the Python interpreter without requiring the special #! first line needed to run files directly in Unix. To run Python command lines on Windows 9x platforms, youll need to add the word "python" before the filename and make sure the python.exe executable is on your PATH setting (as described earlier). On all Windows platforms, you may also click on Python filenames in a Windows explorer to start them.
Категории