In this article, we will see how to set up Python in Visual Studio 2017 (I am using the free Community Edition) and later, we will create a sample web app with the help of Bottle framework (Bottle template is available in Visual Studio 2017).

Due to Python’s popularity, Microsoft has now started Python language support in many of their products. They already started Python support in Visual Studio 2013 and VS 2015 and now integrated Python with SQL Server 2017 too. Python is a general-purpose language and lots of third-party are libraries available there with Python. That makes it very easy to use. If we use Python with Visual Studio, we can leverage the advantages of many Visual Studio features (like a debugger).

Step 1 - Install Python in already installed Visual Studio 2017/New installation with PythonYou can download the Visual Studio Community Edition online installer from this URL and run it. If you have already installed Visual Studio, it will check for the existing installation.

After some time, it will show the existing version of Visual Studio. You can click "More" drop-down button and click "Modify".

Now, select the "Python development" option.

On the right side, it shows the selected features. By default, it selects three features, as shown below.

You can add more features to it if needed.

Please click the "Install" button and it will take some time to install all the new features.Step 2: Details about the Python features available with Visual Studio 2017There are two types of Python applications currently available in Visual Studio Machine Learning Category.

Three types of Machine learning projects are available with the cookiecutter template.

Web Category

Under Web category, there are 13 types of project templates available.

Step 3: Create a Bottle Framework Project

We are going to create a simple Polls project with Bottle framework. Bottle is a simple, fast, powerful and lightweight web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library. It supports Routing, Templates, Utilities, and HTTP services. You can get more details about the Bottle framework from here.

In this Polling Application, the users can give their vote to any topic they choose and later, they can view the total polls on that topic.

Choose the Python Bottle project template and give a name to that.

Our polls project needs some external packages so we must install these packages. As Microsoft recommends, we can install these packages in a virtual Python environment.

We must save our project now, before proceeding to the further steps.

We can give the environment a name. By default, the environment's name is “env” but if needed, you can change it. I have given it as “python-env”.

Click the "Create" button now. It will take some time to create the environment and other dependencies.Please note that there is currently one issue in Visual Studio Community edition - it will not add the new files to the solution the first time after project creation. It is listed as a missing file as shown below.

If you click any file in the Solution Explorer, it shows the below error message.

For the time being as a workaround, we can close the solution and reopen it.

Bottle framework is an MVC kind framework that contains Models, Views, and Controllers. In our project, we can see various Python files under the Models folder and Views folder. We have some controller files also. Routes.py is the main controller file and it will control the routing part of our application. If you look at the requirements.txt file, you can see there are three references it contains.

In our polling application, we will use three kinds of data storage. By default, it is a memory storage. Every time we restart the application, it loses its previous run data. The second option is MongoDB. For MongoDB, we can use a local MongoDB database and pymongois used for connecting Python with MongoDB. The third option is the Azure Table Storage and the azure-storage library is used for connecting Python with the Azure Table.

Step 4: Installing the required libraries from requirment.txt

Right-click the Python environment and click "Install" from the requirments.txt option.

It will get the required details from library repositories and install to our Python environment.

If you check the project location, you can see all the library files saved in the environment folder python-env > Lib > site-packages.

Before running the application, I have just edited the sample.json file in Models folder to modify the topics in our polling.

Please note that we are running the application in “memory” mode. So, on every run, it resets the previous polling data.Step 5 - Running the Python Bottle ApplicationPlease run the application. Visual Studio will automatically create a web server (like IIS local server) and it listens to every action from the browser and the log.

Now, our application runs in the browser.

Please note that currently, there is no polling selected. Click "Create Sample Polls" to create a new poll.

After every polling, it shows the status of the total result.

We can run this application using MongoDB database as well. For that, we must change the settings in settings.py file.If you are new to MongoDB, please follow my previous article on C# Corner A Simple TO-DO Python Flask Application With MongoDB where I have explained all the basic steps to start with Python and MongoDB.Please edit the settings.py file with the following changes. You must change repository name from “memory” to “mongodb” and then change MONGODB_HOST to “localhost:27017” (We are using Local MongoDB database so the host is localhost and default port is 27017)

Now, run your application again and choose any poll topic and vote.

If you have MongoDB Compass Community or any other MongoDB data explorer tools available, you can see a new "polls" database and "polls" collection created now.

In this article, we saw the steps for installing Python in Visual Studio 2017 and we also created a Python application with Bottle framework. We used the default Polls application template in Visual Studio. We executed the application with “in-memory” mode and later we changed the configuration to “MongoDB” mode.