Visual Studio And Python - Importing Existing Project

Python is a very widely used scripting language. Python comes with many supported IDEs (Integrated Development Environment). Microsoft has also introduced Python's development by using Visual Studio in Visual Studio 2015.

Today, I shall demonstrate how to import and execute existing Python project into Visual Studio 2015.

The prerequisites include knowledge about the technologies given below.

  1. Python programming.
  2. Visual Studio.

You can download sample Python source code. The sample code is taken from here. Python 3.x version is being used in this tutorial.

Let's Begin!!

The first step is to install Python on your system. Simply download the suitable version of Python here. Double click the installer and follow the on screen steps. Python will be installed.

As soon as Python is installed on your system, you need to install Python for Visual Sudio. In order to do this, click Visual Studio 2015 installer and select "Python tools for Visual Studio" options during the installation, as shown below. 



Open Visual Studio 2015 and click "New Project".

From "Template" menu, click "Python".

In the right menu of the "New Project" Window, choose "From Existing Python code" template.

Name the project "PythonIPGeoLocation" and click OK, as shown below. 



A new screen will open on "Create New Project from Existing Python Code", choose the folder, where your current Python code resides and click next, as shown below.



Now, on the screen given below, simply select the file, which contains Python code's main method, which you will ultimately be setting as default file to run and click next.



On the next screen, choose the location, where you want to save your Python project and click Finish, as shown below.



You will see that your Python project has been created in Visual Studio 2015, as shown below.



Now, let's set Python environment in Visual Studio, but, before that, you need to set installed Python path to the environment variable, if you have not choosen the option of "Add Python 3.x to PATH" during installation of Python. Right click on "My Computer->Properties". "System" window, click "Advanced system settings" and then click "Environment Variables", as shown below.



Now, add "D:\Python3.6\Scripts;D:\Python3.6;" paths into "PATH" variable under "User Variable for your username", as shown below.



Now, let's set Python environment in Visual Studio. Right click your Python project in Visual Studio, followed by clicking Python Tools->Python Environments", as shown below.


"Python Environment" Window will open next to "Solution Explorer" Window, click "+ Custom" link, as shown below.

 

Now, provide the settings given below for Python version, which you have installed and click Apply link. I have installed Python 3.6, so my settings are shown below.



In the Window given above, after providing the path to Python, you can click "Auto Detect" option to fill the "Language Version & Architecture" attributes. Also, don't forget to provide "PATH" variable.

 


Now, our Python environment is set. Try to execute the project, you will get an error because some packages that the code uses are not yet installed into our Python environment, as shown below.

In the image given above, Visual Studio will highlight that "BeautifulSoup" package is missing. Thus, we need to install that missing package.

In order to install the missing package, open "Python Environment" Window and choose "pip" from drop down menu, as shown below.



Since, I am using Python 3.6 so, I need to install "BeautifulSoup4", which is compatible with Python 3.6, as shown below. 

 


After installation of any package, click on "Refresh" and wait till the refresh completes, as shown below.



In "BeautifulSoup" package for Python 3.6 is renamed to "bs4". Thus, rename "BeautifulSoup" in import to "bs4", as shown below.



Now, execute the project and you will see the result given below.



The Window given above shows the valid result, which is received from "www.geody.com" Web service for IP base Geo location. If you open the Web Service link directly in the Bbrowser, you will see the same result, as shown in the output Window of Python code execution given above.


So, that about it.

Conclusion

In this article, you learned how to import the existing Python code into Visual Studio IDE and also learn how to execute the code. You also learned to add the required paths into the system environment variable and learned to install Python tools for Visual Studio.

Up Next
    Ebook Download
    View all
    Learn
    View all