Python In Visual Studio 2017



-->

This video shows you the basics of creating a python project in VS2017. I show you how to add existing code and files to a new project and also how to set th. Python Tools for Visual Studio is a completely free extension, developed and supported by Microsoft with contributions from the community. Visit our Github page to see or participate in PTVS development. Visual Studio Community 2019. Free, fully-featured IDE for students, open-source and individual. Visual Studio provides a UI to manage packages in your Python environments. View environments. Select the View Other Windows Python Environments menu command. The Python Environments window opens as a peer to Solution Explorer and shows the different environments available to you. The list shows both environments that you installed using the Visual Studio installer and those you installed.

Visual

Python is a popular programming language that is reliable, flexible, easy to learn, free to use on all operating systems, and supported by both a strong developer community and many free libraries. The language supports all manners of development, including web applications, web services, desktop apps, scripting, and scientific computing and is used by many universities, scientists, casual developers, and professional developers alike.

Visual Studio provides first-class language support for Python. This tutorial guides you through the following steps:

2017

Prerequisites

  • Visual Studio 2017 with the Python workload installed. For instructions, see Work with Python in Visual Studio - Step 0.
  • Visual Studio 2019 with the Python workload installed. For instructions, see Work with Python in Visual Studio - Step 0.

You can also use an earlier version of Visual Studio with the Python Tools for Visual Studio installed. See Install Python support in Visual Studio.

Step 1: Create a new Python project

A project is how Visual Studio manages all the files that come together to produce a single application, including source code, resources, configurations, and so on. A project formalizes and maintains the relationship between all the project's files as well as external resources that are shared between multiple projects. As such, projects allow your application to effortlessly expand and grow much easier than simply managing a project's relationships in ad hoc folders, scripts, text files, and even your own mind.

In this tutorial you begin with a simple project containing a single, empty code file.

  1. In Visual Studio, select File > New > Project (Ctrl+Shift+N), which brings up the New Project dialog. Here you browse templates across different languages, then select one for your project and specify where Visual Studio places files.

  2. To view Python templates, select Installed > Python on the left, or search for 'Python'. Using search is a great way to find a template when you can't remember its location in the languages tree.

    Notice how Python support in Visual Studio includes a number of project templates, including web applications using the Bottle, Flask, and Django frameworks. For the purposes of this walkthrough, however, let's start with an empty project.

  3. Select the Python Application template, specify a name for the project, and select OK.

  4. After a few moments, Visual Studio shows the project structure in the Solution Explorer window (1). The default code file is open in the editor (2). The Properties window (3) also appears to show additional information for any item selected in Solution Explorer, including its exact location on disk.

  5. Take a few moments to familiarize yourself with Solution Explorer, which is where you browse files and folders in your project.

    (1) Highlighted in bold is your project, using the name you gave in the New Project dialog. On disk, this project is represented by a .pyproj file in your project folder.

    (2) At the top level is a solution, which by default has the same name as your project. A solution, represented by a .sln file on disk, is a container for one or more related projects. For example, if you write a C++ extension for your Python application, that C++ project could reside within the same solution. The solution might also contain a project for a web service, along with projects for dedicated test programs.

    (3) Under your project you see source files, in this case only a single .py file. Selecting a file displays its properties in the Properties window. Double-clicking a file opens it in whatever way is appropriate for that file.

    (4) Also under the project is the Python Environments node. When expanded, you see the Python interpreters that are available to you. Expand an interpreter node to see the libraries that are installed into that environment (5).

    Right-click any node or item in Solution Explorer to access a menu of applicable commands. For example, the Rename command allows you to change the name of any node or item, including the project and the solution.

Python In Visual Studio 2017

Next step

Go deeper

  • Python projects in Visual Studio.
  • Python for Beginners (python.org)
-->

Previous step: Run code in the debugger

The Python developer community has produced thousands of useful packages that you can incorporate into your own projects. Visual Studio provides a UI to manage packages in your Python environments.

View environments

How To Use Python In Visual Studio 2017

  1. Select the View > Other Windows > Python Environments menu command. The Python Environments window opens as a peer to Solution Explorer and shows the different environments available to you. The list shows both environments that you installed using the Visual Studio installer and those you installed separately. That includes global, virtual, and conda environments. The environment in bold is the default environment that's used for new projects. For additional information about working with environments, see How to create and manage Python environments in Visual Studio environments.

    Note

    You can also open the Python Environments window by selecting the Solution Explorer window and using the Ctrl+K, Ctrl+` keyboard shortcut. If the shortcut doesn't work and you can't find the Python Environments window in the menu, it's possible you haven't installed the Python workload. See How to install Python support in Visual Studio for guidance about how to install Python.

  2. The environment's Overview tab provides quick access to an Interactive window for that environment along with the environment's installation folder and interpreters. For example, select Open interactive window and an Interactive window for that specific environment appears in Visual Studio.

  3. Now, create a new project with File > New > Project, selecting the Python Application template. In the code file that appears, paste the following code, which creates a cosine wave like the previous tutorial steps, only this time plotted graphically. Alternatively, you can use the project you previously created and replace the code.

  4. With a Python project open, you can also open the Python Environments window from Solution Explorer by right-clicking Python Environments and selecting View All Python Environments

  5. Looking at the editor window, you'll notice that if you hover over the numpy and matplotlib import statements that they are not resolved. That's because the packages have not been installed to the default global environment.

Install packages using the Python Environments window

  1. From the Python Environments window, select the default environment for new Python projects and choose the Packages tab. You will then see a list of packages that are currently installed in the environment.

  2. Install matplotlib by entering its name into the search field and then selecting the Run command: pip install matplotlib option. This will install matplotlib, as well as any packages it depends on (in this case that includes numpy).

  3. Consent to elevation if prompted to do so.

  4. After the package is installed, it appears in the Python Environments window. The X to the right of the package uninstalls it.

    Note

    A small progress bar might appear underneath the environment to indicate that Visual Studio is building its IntelliSense database for the newly-installed package. The IntelliSense tab also shows more detailed information. Be aware that until that database is complete, IntelliSense features like auto-completion and syntax checking won't be active in the editor for that package.

    Visual Studio 2017 version 15.6 and later uses a different and faster method for working with IntelliSense, and displays a message to that effect on the IntelliSense tab.

Run the program

  1. Now that matplotlib is installed, run the program with (F5) or without the debugger (Ctrl+F5) to see the output:

Next step

Go deeper