Muokkaa

Jaa


Tutorial: Write and run Python code in Visual Studio

This article presents Step 2 in the tutorial series Work with Python in Visual Studio.

The Visual Studio interactive development environment (IDE) provides various windows that support different development tasks. Step 1 in the tutorial describes how to manage project files in the Solution Explorer window. In Step 2, you use the editor window to work with the contents of files, like source code. The editor is contextually aware of the type of file you're editing. The editor also recognizes the programming language (based on the file extension), and offers features appropriate to that language such as syntax coloring and auto-completion by using IntelliSense.

In Step 2 of the tutorial, you learn how to:

  • Write Python code in the editor
  • Run code (without debugging)
  • Use Intellisense features for writing code

Prerequisites

Write code in Visual Studio

When you create a new Python project from the Python Application template, Visual Studio creates an empty Python file (.py) and opens the file in the editor. Visual Studio uses the project name that you specify at creation as the name for the file. The default project name is "Python Application1" and the default file name is PythonApplication1.py. In this section, you add code to this empty Python file.

Follow these steps to start writing Python code:

  1. Open your empty Python file in the Visual Studio editor.

  2. In the editor, start to enter the Python function name print.

    As you enter the function name, Visual Studio IntelliSense displays auto-completion options for your code. The default completion option is highlighted:

    Screenshot that shows IntelliSense auto-completion options in the editor in Visual Studio. The default completion option is highlighted.

    To apply the default completion to your code, select the Tab keyboard shortcut.

    Completions are most helpful when you need to add longer statements or identifiers in your code. IntelliSense shows different completion options based on the statement you're using. For example, when you use parenthesis () with a function name, Intellisense recognizes the code as a function call.

  3. In the editor, start to enter code to call the print() function.

    When you enter the open parenthesis (, Intellisense shows full usage information for the function. The current argument is in boldface. In this example, the argument is values: object:

    Screenshot that shows IntelliSense auto-completion options for the print function in the editor in Visual Studio.

  4. In the editor, complete the code statement so it matches the following example:

    print("Hello, Visual Studio")
    

    Notice how Visual Studio applies different syntax coloration for the print function versus the function argument "Hello, Visual Studio". Visual Studio also uses coloring and formatting techniques to help you read your code. For example, Visual Studio uses underlining to draw your attention to issues with your code.

  5. Temporarily delete the closing quote mark " for the string argument.

    Notice how Visual Studio shows a red underline for code that contains syntax errors.

    Screenshot that shows IntelliSense syntax coloring and error highlighting in the editor in Visual Studio.

  6. Replace the closing quote mark " in your code.

Configure preferred formatting and behavior

Your development environment preferences are a personal matter. Visual Studio gives you complete control over the IDE appearance and interactive behavior. In addition to showing/hiding windows and setting up your preferred window layout, you can choose colors and other formatting options for menus, window content, and the editor.

Follow these steps to explore configuration options for your environment:

  1. Select Tools > Options on the toolbar.

  2. Explore the configuration settings under the Environment and Text Editor tabs.

    Each tab lists settings by area, such as General, Fonts and Colors, Advanced, and so on.

  3. Explore options specific to working with Python in Visual Studio:

    • Tools > Options > Python
    • Tools > Options > Text Editor > Python
  4. To see options that apply to all supported programming languages, select Tools > Options > Text Editor > All Languages.

Run code in Visual Studio

After you have some code in place, you're ready to try running your program. You can choose to run the application with or without debugging.

  1. To run the code without debugging, select Debug > Start without Debugging on the toolbar, or use the Ctrl+F5 keyboard shortcut.

    Visual Studio warns you if you still have errors in your code.

  2. When you run the program, a console window opens to show the results. This window is similar to what you see when you run a Python interpreter with the Python file (PythonApplication1.py) from the command line.

    Screenshot that shows the console window with output for the running program in Visual Studio.

  3. To close the console window, select any key. You return to the Visual Studio editor.

Explore Intellisense completions

In addition to completions for statements and functions, IntelliSense provides completions for Python import and from statements. These completions help you easily discover what modules are available in your environment and the members of those modules.

Follow these steps to explore more Intellisense completion features:

  1. In the editor, delete the print statement from your code.

  2. Start to enter the import sys statement. When you start to enter sys, Visual Studio shows a list of possible modules for the import function:

    Screenshot that shows how IntelliSense displays the available modules for an import statement in the editor in Visual Studio.

    You can scroll through the list to see the full set of available modules.

  3. To complete the statement, enter sys or select the sys completion option in the Intellisense dialog.

  4. Move to the next line in your code file, and start to enter the from math statement. Visual Studio shows the list of possible modules for the function:

    Screenshot that shows how IntelliSense displays the available modules for the 'from' statement in the editor in Visual Studio.

  5. Complete the math library name or select the math completion option from the Intellisense dialog.

  6. Continue the statement by adding a space followed by the import function name. Visual Studio shows the list of possible module members for this use of the import function:

    Screenshot that shows how IntelliSense displays the available module members for the `import` function in the editor in Visual Studio

  7. Finish the statement by using the Intellisense completion options to add the cos and radians members.

    Here's what your completed code should look like:

    import sys
    from math import cos, radians
    

    Tip

    Completions work with substrings as you type, matching parts of words, letters at the beginning of words, and even skipped characters.

  8. Add a little more code to your Python file and print the cosine values for 360 degrees:

    for i in range(360):
        print(cos(radians(i)))
    
  9. Run your program again. You can try the Ctrl+F5 keyboard shortcut.

    Close the program output window when you're done.

Next step