Visual Studio And Python - Importing Existing Project

Introduction 

 
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 the 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 the 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. 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 Studio. 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 the "Template" menu, click "Python".
 
In the right menu of the "New Project" window, choose the "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 set 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 chosen the option of "Add Python 3.x to PATH" during the 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 the "PATH" variable under "User Variable for your username", as shown below.
 
 
Now, let's set the 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 the "Solution Explorer" window, click "+ Custom" link, as shown below.
 
 
 
Now, provide the settings given below for the 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 the "Auto Detect" option to fill the "Language Version & Architecture" attributes. Also, don't forget to provide the "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 the "BeautifulSoup" package is missing. Thus, we need to install that missing package.
 
In order to install the missing package, open the "Python Environment" Window and choose "pip" from the 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 the "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 Geolocation. If you open the Web Service link directly in the browser, 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.