|
 |
Visual Python Tutorial
|
 |
Introduction
Visual Python is ActiveState's full-featured Python plug-in for Visual Studio .NET. Developers who
are familiar with Visual Studio can use the same powerful tool set and intuitive environment to
write Python programs. This tutorial is an introduction to Visual Python. Using the sample solution
included with Visual Python, follow the tutorial by performing the instructions in the Try it Out
sections.
TopOpen the Visual Python Tutorial
The tutorial uses the sample solution "Dramatis Personae" included with Visual Python.
In the editing section of the tutorial, you will alter the program to display Visual Python's editing
features. Instructions for reverting the changes are included in the relevant tutorial sections.
- From the Windows Start menu, select
Start|Programs|ActiveState Visual Python|Samples|Dramatis Personae. This will load
Visual Studio, and open the sample project in Visual Studio's Solution Explorer.
(If you have previously opened the sample project, the files may open automatically
in the editor, as Visual Studio stores the state of each solution in memory.)
- If necessary, in the Solution Explorer window, double click python_sample.py
to open the file in the Visual Studio editor pane. If the Solution Explorer is not
visible, select View|Solution Explorer.
- Throughout the tutorial, sections of the program are referred to by the line number.
If necessary, enable the display of line numbers in Visual Studio by selecting
Tools|Options|Text Editor and checking the Line numbers box in the
Display section of the dialog.
By default, Visual Python's editing features are enabled when the program is installed.
However, if you have previously disabled any of the features, you will be instructed to re-enable them
in the relevant section of the tutorial.
TopSolutions and Projects
In Visual Studio, the "solution" is the top level of project organization.
Solutions contain one or more projects; projects contain one or more "items"
(files, folders, data connections, etc.)
Things to Note
- solutions can contain multiple projects
- projects can contain multiple items
- use the Solution Explorer (View|Solution Explorer) to modify
the properties and contents of solutions and projects
- in the Solution Explorer, right-click the solution, project or file
name for a context menu
- when a solution has multiple projects, the project name that is
displayed in bold text is the "Startup Project"
Try it Out
if necessary, open the Dramatis Personae solution
View the Dramatis Personae solution in the Solution Explorer (View|Solution Explorer).
- the "dramatis personae" project is contained in the "dramatis personae" solution
- the "python_sample.py" program and the "dream_act1.xml" data file are contained in
the "dramatis personae" project
|
 |
TopRun the Program
In this step, you will simply run the program to see what it does. Things to Note
- programs can be run within Visual Studio's Output window, or within a shell window
- to configure your run preference, check Run in shell window in the
Project's
Properties
Try it Out
Select Debug|Start Without Debugging, or press Ctrl+F5.
- Visual Studio's Run Window displays the output from the program
|
 |
TopEdit the Program
|
In this section:
|
Related User Guide Topics:
|
Task List
Visual Studio's Task List maintains a dynamic list of syntax errors in your Python program.
(See Syntax Checking for
more information.) To view a complete Task List, select View|Show Tasks|All.
Things to Note
- If the Task List window is not visible, select View|Other Windows|Task List to display it
- syntax errors are automatically added to the Task List
- when syntax errors are repaired, the items are removed from the Task List
- manual entries can be also added to the Task List
- comments prefixed with the text TODO: are added to the Task List
Try it Out
Line 32: add the text #TODO: this is a comment
- the comment will be displayed in the Task List
Clear the contents of line 32.
Line 31: remove the bracket following name
- a syntax error is added to the Task List
Re-enter the bracket following name.
|
 |
TopSyntax Checking
Visual Python's syntax checking monitors the validity of your code as you type. To enable
syntax checking, select Tools|Options|Text Editor|Python|Visual Python Specific, and ensure
that the Check syntax while editing box is checked.
Things to Note
- syntax errors are underlined with red wavy lines
- explanatory pop-up tips are displayed when you hover the mouse pointer over the syntax error
- syntax errors are added to Visual Studio's Task List (View|Show Tasks|All)
Try it Out
Line 8: delete a letter from the statement import
- line 8 is underlined to indicate a syntax error
- an explanatory message is displayed when you hover the cursor over the error
- an entry is added to the Task List
Undo the change to Line 8.
|
 |
TopSyntax Coloring
Syntax coloring displays different language elements in different colors. To configure custom
colors, select Tools|Options|Environment|Fonts and Colors.
Things to Note
- When different languages share the same elements, the same color
configuration is used for multiple languages. For example, the
color assigned to the "Comment" language element will be applied to
comments in programs of all languages supported by Visual Studio.
Try it Out
- Select Tools|Options|Environment|Fonts and Colors.
- Ensure that Text Editor is displayed in the "Show settings for" field.
- In the "Display Items" list, select Comment.
- Change the color in the "Item foreground" field to Green, and the "Item background"
to Dark gray. Click OK.
|
 |
TopMethod and Module Tips
When you enter a space after the "import" or "from" command, Visual Python
displays a list of folders and modules found in the system's PYTHONPATH environment
variable, and in the directory where Visual Python was invoked. When you enter a period
after a module name, a list of the classes and functions contained in the module is
displayed. When you enter a period after a class name, a list of the methods within
the class is displayed.
Things to Note
- to position the tip list, continue to enter the letters of the desired item
- to select an item, highlight it in the list box and press Enter or Tab
- to clear the tip list, press Esc
- to re-display the tip list, press Ctrl+J
Try it Out
Line 9: enter import, followed by a space
- a list of folders and modules is displayed
Line 9: enter xm and press Enter
- the xml folder will be inserted
Line 9: enter a period after xml
- folders under the xml folder are displayed; select dom
Line 9: enter a period after dom
- methods within the dom module are displayed
Clear the contents of Line 9.
|

 |
TopCall Tips
Call tips display the arguments, default values and a brief description for
methods and functions. Call tips are displayed when you enter an open
parenthesis ("(") after a module name followed by a function name, a class name
followed by a method name, or a variable name followed by a method name.
Things to Note
- each argument is displayed in bold text as you complete the list of arguments
- to clear the tip, continue typing or press Esc
- to redisplay the tip, enter Ctrl+Shift+Space
Try it Out
Line 9: enter import string
Line 10: enter print string.join
- enter an open parenthesis - the string.join call tip is displayed
Line 10: enter string.split
- enter an open parenthesis - the string.split call tip is displayed
Clear the contents of lines 9 and 10.
|
 |
TopDynamic Help
Visual Studio's Dynamic Help pane displays links to context-sensitive Python language reference.
To display the Dynamic Help pane, select Help|Dynamic Help.
Things to Note
- links to help topics are displayed based on the current cursor position
- to view and search the entire contents of the Python language reference,
select Help|Contents|ActiveState Visual Python|Python Language Reference
Try it Out
Line 17: click on the print keyword
- the print keyword is displayed in the Help list
|
 |
TopCode Folding
Code folding is used to collapse and expand sections of your Python program. To display
code folding markers, select Tools|Options|Text Editor|Python|Visual Python Specific, and ensure
that the Show code folding box is checked.
Things to Note
- plus and minus symbols to the left of a line of code indicate sections that can be
collapsed or expanded
- hover the mouse pointer over the box to the right of a folded line to display
the folded code in a pop-up box
- collapsed blocks will be automatically expanded during debugging
Try it Out
Line 27: click on the minus symbol
- lines 27 to 31 are collapsed
- a plus symbol to the left of line 27 indicates a collapsed block
- hover the mouse pointer over the three ellipses to the right of line 27 to display the hidden code
|
 |
TopSmart Indenting
Smart indenting automatically indents lines in the editor after a colon. To enable Smart
Indenting, select Tools|Options|Text Editor|Python|Tabs, and ensure that the
"Smart" button is selected in the Indenting section of the dialog.
Things to Note
- after a colon, the indentation is incremented based on the value specified in the
Tabs dialog of Visual Studio's Options
Visual Python also performs automatic indenting when a line break is entered after an open
parenthesis. Visual Python sets the indentation level to the column following the parenthesis
on the previous line. The close parenthesis will return the indentation to its previous level.
Things to Note
- indentation is incremented and decremented in the same manner as smart indenting,
described above
- the indentation behavior will work for nested parentheses
Try it Out
Line 29: at the end of the line, press Enter.
- the cursor position is indented by the amount specified for Tab values
Line 25: after the open parenthesis, press Enter.
- the cursor position is indented to the column after the open parenthesis
Undo the changes made to lines 25 and 29.
|

 |
TopCommenting Blocks of Code
When you need to comment a selection of code, rather than an individual line,
you can use Visual Studio's Comment Selection function.
Things to Note
- select the code block first
- to comment the selected block, click Edit|Advanced|Comment Selection, or use
the keyboard shortcuts Ctrl+K, Ctrl+C
- to uncomment a commented block click Edit|Advanced|Uncomment Selection, or use
the keyboard shortcuts Ctrl+K, Ctrl+U
Try it Out
Line 27: click and drag from the beginning of line 27 to the end of line 31. Select
Edit|Advanced|Comment Selection.
- each line of the block will be preceded by a # symbol
Select Edit|Advanced|Uncomment Selection to remove the commenting.
|
 |
Opening a Module
If your program includes an external Python module, you can open the module in the
editor pane using the Open Document option on the right-mouse context menu. To open
a module, position the cursor over the module name and right-click.
Try it Out
Line 6: click anywhere in the text "import xml.dom.minidom".
- the "minidom.py" module will be opened in the editor pane
|
 |
TopDebug the Program
|
In this section:
|
Related User Guide Topics:
|
In this section, you will analyze the program as it runs - that is, you'll debug it. The debug
process can be used to identify and fix errors in programs; it is also useful for tracing the
structure and functionality of existing programs.
Setting Breakpoints
Breakpoints can be manipulated in the breakpoint margin of the editor pane, or in the
Breakpoints Window (Debug|Windows|Breakpoints (Ctrl+Alt+B)).
- click on the desired line in the breakpoint margin to set a breakpoint
- click on an existing breakpoint to remove it
- alternatively, use the Breakpoints Window to add and remove breakpoints
Try it Out
Line 23: click on the grey margin to the left of the editor pane
- a red circle on the breakpoint margin indicates the breakpoint
- the breakpoint is added to the Breakpoints Window
|
 |
TopViewing and Changing Variables
Use the Watch window (Debug|Windows|Watch, or Ctrl+Alt+W number)
to view and change variable values, and to add new variables. To add a variable to the Watch
Window, copy and paste the variable from the editor pane or from the Locals window, or enter
it manually.
Try it Out
Select Debug|Start or press F5 to run the debugger.
The program will run until it encounters the breakpoint on line 23 (set in the previous step).
Display a Watch window by selecting Debug|Windows|Watch. Copy the
variable element.childNodes[0].nodeValue on line 23 from the
editor pane to the Watch window and press Enter.
- the variable value is displayed in the Watch Window
|
 |
TopUsing the Call Stack
The Call Stack (Debug|Windows|Call Stack or Ctrl+Alt+C) displays the name
and language of each function that is currently on the stack. The variables displayed in the
Locals window are particular to the stack; that is, you can see variables associated with
each function by selecting the desired stack.
Try it Out
If necessary, select Debug|Stop Debugging (Shift+F5) to stop the debugger.
Set a breakpoint on line 11, then press F5 to run the debugger. When the debugger stops
on line 11, press F11 to step into the next line.
Display the Call Stack window by selecting Debug|Windows|Call Stack.
- the current stack is indicated by a yellow arrow
- double-click the python_sample.py stack; the editor pane will display the position
where the stack changed, and the Locals window will display variables associated with
that stack
|
 |
Top
Copyright © 2000 - 2005
ActiveState, a division of Sophos Plc.
Portions Copyright © 1987-2003, Microsoft Corporation. All rights reserved. ActiveState and Visual Python are registered trademarks of ActiveState
Corporation.
IntelliSense® is a Registered Trademark of Microsoft.
Other companies and products mentioned in this document are the property of those companies.
|