Python is a very widely used scripting language. Python comes with many supported IDE (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.
Following are few prerequisites before you proceed any further:
1) Knowledge of Python programming.
2) Knowledge of 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.
1) The first step is to install the python on your system. Simply download the suitable version of python from here. Double click the installer and follow the on screen steps. Python will be the installed.
2) After 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 installation as shown below i.e.
3) Open visual studio 2015 and click “New Project“.
4) From “Template” menu click on “Python“.
5) In the right menu of the “New Project” window choose “From Existing Python code” template.
6) Name the project “PythonIPGeoLocation” and click ok as shown below i.e.
7) 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 i.e.
8) Now, on screen below, simply select the file which contains python code main method which you will ultimately be setting as default file to run and click next i.e.
9) On the next screen choose the location where you want to save your python project and click finish as shown below i.e.
10) You will see that your python project has been created in visual studio 2015 as shown below i.e.
11) Now, let’s set python environment in visual studio, but, before that you need to set installed python path to environment variable if you have not choose the option of “Add Python 3.x to PATH” during installation of the python. For that matter, right click on “My Computer->Properties“. “System” window, click “Advanced system settings” and then click “Environment Variables” as shown below
12) Now add “D:\Python3.6\Scripts;D:\Python3.6;” paths into “PATH” variable under “User Variable for your username” as shown below i.e.
13) Now, let’s set python environment in visual studio, Right click your python project in visual studio then click”Python Tools->Python Environments” as shown below.
14) “Python Environment” window will open next to “Solution Explorer” window, click “+ Custom” link as shown below i.e.
15) Now, provide following settings for python version that you have installed and click Apply link. I have installed Python 3.6, so, my settings are as follow i.e.
In above window, 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.
16) Now, our python environment is set. Try to execute the project, you will get error because some packages that the code uses are not yet installed into our python environment as shown below i.e.
In above image visual studio will highlight that “BeautifulSoup” package is missing. So, we need to install that missing package.
17) In order to install missing package Open “Python Environment” window and choose “pip” from drop down menu as shown below i.e.
18) Since, I am using python 3.6 so, I install “BeautifulSoup4” which is compatible with python 3.6 as shown below i .e.
19) After installation of any package click on “Refresh” and wait till the refresh complete as shown below i.e.
20) In “BeautifulSoup” package for python 3.6 is renamed to “bs4“. So, rename “BeautifulSoup” in import to “bs4” as shown below i.e.
21) Now, execute the project and you will see below result i.e.
The above window 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 browser, you will see same result as shown in above output window of python code execution i.e.
So, that about it!!
In this tutorial, you will learn how to import the existing python code into visual studio IDE and also learn how to execute the code, you will also learn to add the require paths into system environment variable and learn to install python tools for visual studio.