Total Pageviews

Sunday 6 October 2019

Python Development In Visual Studio 2017

Python at a glance

  • Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991
  • Python is a server side programming language.
  • Python is targeted for almost all platforms
  • Python is a modular language like nodejs
  • Python is a indent sensitive language

Development environment setup

  1. Go to https://www.python.org/downloads/ and download latest (or earlier) version of Python for Windows OS
  2. Install Python using run as administrator in your local machine as follows


    Step 1: Select Customize installation with Install launcher for all & Add Python x.x to PATH selected.
    Note: Install Now option will install python for current user only.


    Step 2: Check the all options particularly pip & for all user option and click Next


    Step 3: Check the all options particularly the red marked options. Remember the install location. Click Install


    Installing… It may take few minutes.


    Step 4: Installation is completed

Checking Environment is OK or not

  • Open command prompt
  • To know python version execute python --version
  • To know pip (python packages installer) version execute pip --version or execute pip list
  • If you can see the result as screenshot below then it's Okay.
  • Otherwise there may be some problem during installation and Check system environment variable PATH value discussed in later section

System Environment Variables for Python

If python is correctly setup then system environment variable PATH will contain two directory path such as
  1. python.exe container directory. In my case it is C:\Program Files (x86)\Python37-32\
  2. pip.exe container directory. In my case it is C:\Program Files (x86)\Python37-32\Scripts\
If somehow these aren’t set automatically then you have to set those manually and check again using python --version and pip --version command

Fig: System environment variable PATH values

Basic Python

Python contains almost all basic features like other programming languages such as loop (for, while), if else etc. But syntax is different.
Python’s exceptional feature is scope definition.
Here scope is maintained by indent (number of tabs in left side)

Scope


Python in Visual Studio 2017

.NET developer can easily work on python in visual studio 2017 or earlier.
If you can setup python environment correctly (as shown in previous slides), You can see the python environments in visual studio from menu View→Other Windows→Python Environments and Python Interactive Window as screenshot below

Create python project in VS 2017

Open Visual Studio and Go to File menu then New -> Project then as screenshot below


Python code snippet

You may not need to remember all python code syntax. Visual studio will help you to insert code snippet into your place.
Or you type python keywords and press TAB, you will get full statement structure

Simple python program

  • This program imports environ dictionary defined in os package.
  • Gets all keys and prints the values and finally prints the count.
  • None is same as null in other language
from os import environ
 
print("My Environment Information")
keys = environ.keys()
count = 0
for key in keys:
    if environ[key] != None:
            print(key + " = "+ environ[key])
            count += 1
print("There are ", count, " information")

Debugging & Watching is same as .NET program

Related Links

https://www.python.org/
https://www.python.org/downloads/
https://docs.python.org/3/contents.html

Conclusion

I think this session may help for Python beginners as well as .NET guys.