Python tools visual studio tutorial




















It's simple to run hello. Just click the Run Python File in Terminal play button in the top-right side of the editor. The button opens a terminal panel in which your Python interpreter is automatically activated, then runs python3 hello.

Right-click anywhere in the editor window and select Run Python File in Terminal which saves the file automatically :. This command is convenient for testing just a part of a file.

In the REPL, you can then enter and run lines of code one at a time. First, set a breakpoint on line 2 of hello. Alternately, just click in the editor's left gutter, next to the line numbers. When you set a breakpoint, a red circle appears in the gutter. Next, to initialize the debugger, press F5. Since this is your first time debugging this file, a configuration menu will open from the Command Palette allowing you to select the type of debug configuration you would like for the opened file.

These different configurations are fully explained in Debugging configurations; for now, just select Python File , which is the configuration that runs the current file shown in the editor using the currently selected Python interpreter.

The debugger will stop at the first line of the file breakpoint. The current line is indicated with a yellow arrow in the left margin. If you examine the Local variables window at this point, you will see now defined msg variable appears in the Local pane. The Status Bar also changes color orange in many themes to indicate that you're in debug mode.

The Python Debug Console also appears automatically in the lower right panel to show the commands being run, along with the program output. To continue running the program, select the continue command on the debug toolbar F5.

The debugger runs the program to the end. Tip Debugging information can also be seen by hovering over code, such as variables. In the case of msg , hovering over the variable will display the string Hello world in a box above the variable.

You can also work with variables in the Debug Console If you don't see it, select Debug Console in the lower right area of VS Code, or select it from the Select the blue Continue button on the toolbar again or press F5 to run the program to completion.

For full details, see Debugging configurations, which includes notes on how to use a specific Python interpreter for debugging. Tip: Use Logpoints instead of print statements : Developers often litter source code with print statements to quickly inspect variables without necessarily stepping through each line of code in a debugger. In VS Code, you can instead use Logpoints. A Logpoint is like a breakpoint except that it logs a message to the console and doesn't stop the program.

For more information, see Logpoints in the main VS Code debugging article. Let's now run an example that's a little more interesting.

In Python, packages are how you obtain any number of useful code libraries, typically from PyPI. For this example, you use the matplotlib and numpy packages to create a graphical plot as is commonly done with data science. Note that matplotlib cannot show graphs when running in the Windows Subsystem for Linux as it lacks the necessary UI support.

Return to the Explorer view the top-most icon on the left side, which shows files , create a new file called standardplot. Tip : If you enter the above code by hand, you may find that auto-completions change the names after the as keywords when you press Enter at the end of a line. To avoid this, type a space, then Enter. Next, try running the file in the debugger using the 'Python: Current file' configuration as described in the last section.

Unless you're using an Anaconda distribution or have previously installed the matplotlib package, you should see the message, 'ModuleNotFoundError: No module named 'matplotlib'.

Such a message indicates that the required package isn't available in your system. This command opens a command prompt for your selected interpreter. A best practice among Python developers is to avoid installing packages into a global interpreter environment. You instead use a project-specific virtual environment that contains a copy of a global interpreter. Once you activate that environment, any packages you then install are isolated from other environments.

Such isolation reduces many complications that can arise from conflicting package versions. To create a virtual environment and install the required packages, enter the following commands as appropriate for your operating system:. Note : When you create a new virtual environment, you should be prompted by VS Code to set it as the default for your workspace folder.

Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. Any additional feedback? Note Python support is presently available only on Visual Studio for Windows.

Tip The Community edition is for individual developers, classroom learning, academic research, and open source development. Submit and view feedback for This product This page. On disk, the project is a. Under your project are source files, in this case only a single.

Also under the project is the Python Environments node 4. Expand the node to show the available Python interpreters. Expand an interpreter node to see the libraries installed in that environment 5. Right-click any node or item in Solution Explorer to show a context menu of applicable commands. For example, Rename lets you change the name of a node or item, including the project and the solution. Write and run code. Skip to main content. This browser is no longer supported.

Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. Any additional feedback? Visual Studio with the Python workload installed. Select the PythonConsoleApp template, and select Next. The new project opens in Visual Studio. The Visual Studio Solution Explorer window shows the project structure 1.

The default code file opens in the editor 2. The Properties window shows more information for the item selected in Solution Explorer , including its exact location on disk 3.

Submit and view feedback for This product This page. View all page feedback.



0コメント

  • 1000 / 1000