Powered By:
Android Advice
 

Creating Python App with Free IDE – Visual Studio Shell and PyTools

Introduction

This tutorial is good for those just getting started with Python dev. It is demonstrated using the Microsoft Visual Studio 2010 Shell IDE with the PyTools Plugin installed (See my blog post on how to get this setup here: http://www.stevenmarkford.com/free-python-ide-using-visual-studio-shell/ )

In this tutorial we will create a small Python application within the Visual Studio IDE which will simply read in some input from the user do some stuff and then output a result to the user. This tutorial will also be programmed using good practices i.e. object orientation/classes etc.

Create a Python Visual Studio Solution

A Visual Studio “Solution” is just a name given to the collection of files, folder, dependencies and resources that make up your entire program/program.

1. Open Visual Studio.

2. Click File->New->Project. In the window select the project template called “Python Application” (this is installed with Pytools) (see pic below)
Creating_Python_App_01

3. This will create a solution with a single project in it (a solution contains one or more projects), for most small scale applications one project is sufficient. This also adds a default .py file to the project which simply prints “hello world” to the console. You can view the solution structure/files/folders in the “solution explorer”, see pic below:
Creating_Python_App_02

4. To run/test the application simply press F5. This will launch the Python console/interpreted and run your .py file (by default the project is configured to run the .py file that was initially created in the solution but you can change this by right clicking on the project in the solution window and clicking properties, it referred to as the startup file of the project).
Creating_Python_App_03
All of this saves you from having to have the python console permanently open and form manually having to type the appropriate command to run your .py file.

Additional Visual Studio Setup Recommendations

1. For Python the code is highly dependent on indentation so always show indentation characters. you can toggle this on and off by pressing Ctrl+R,W (thats Ctrl+R and then while still holding down ctrl press W, press W quickly after pressing R)

2. Always show the error message window. To show this click on View->Error List
Creating_Python_App_04

Structuring Your Code – Adding files and folders to the project

It is good practice to split your code up into logical files. You may want to for example put all your code that deals with user input into its own folder/file structure (group methods etc). Lets go through an example of how to do this and how to reference/use this code from your main startup .py file.

1. Right click on the project in the solution explorer and click Add->New Folder. Name the folder anything meaningful (lets call it ‘Helpers’)

2. Right click the ‘Helpers’ folder and click Add->’New Item’ then in the window that pops up select ‘Empty Python File’. Name the file UserInput.py and click ‘Add’. (we will put all our user input related code and variables in here)

3. Notice the file was automatically opened and ready for editing (visual studio supports multiple files to be open at once, tabs). You can close files and re-open them by double clicking them in the solution explorer.

4. It is good practice to have one class per file and call it the same name as the file. So in this case the code for UserInput.py will contain a single class called UserInput. We will add to methods to this class RequestInput1() and RequestInput2() which will prompt the user to enter a value for input 1 and input 2 respectively. These methods will save the input form the user in variables on the class. So the class will look like this:
Creating_Python_App_05
Save changes to this file.

5. Now we want to use this new class from within our main python file (the start-up file).
So open and update the main .py file to look like this:
Creating_Python_App_06
In the example code above:

Line 1: we import the user input helper class we created, this is necessary to make use of it in the current .py file

Line 3: we create an instance of the UserInput class and assign it to a local variable called userInput

Line 5: we make a call to the method in the user input class called RequestInput1 which prompts the user to insert a value this value is then assigned to the variable stored in the user input class called input1

Line 6: same as line 5 but for Input2

Line 8 and 9: we output the two values entered by the user.

If you test run your application now by Pressing F5 you will get an error on line 1 in the code saying “No module named Helpers.UserInput” this is because by default the python interpreter does not search subfolders for python modules. To tell the python interpreter that you want a sub directory searched you need to add an empty python file within each directory you want searched called __init__.py
So right click on the Helpers folder, click add->new item, click “Empty Python File”, change the name to __init__.py and click Add.

Now press F5 to successfully run your application.

Python App Deployment

When you are ready to deploy your application and run it as a stand-alone app on any machine you can either manually copy all the .py files retaining the current directory structure or even simpler:

1. Right click on the project and click properties
Creating_Python_App_03

2. Select the publish tab on the left, enter in a path, and click publish (this copies all the py files to the destination directory)
Creating_Python_App_07

3. To run your app simply navigate to the directory and type in the name of the main .py in command prompt window (obviously the machine you want to run it on has to have Python installed)

Related Resources

1. For useful PyTool tips see: http://pytools.codeplex.com/wikipage?title=Features%20Editor#Intellisense

References

http://stackoverflow.com/questions/1260792/python-import-a-file-from-a-subdirectory
http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
http://stackoverflow.com/questions/3878479/python-pyc-files-main-file-not-compiled
http://www.stevenmarkford.com/python-error-input-name-is-not-defined/

Leave a Comment

Notify via Email Only if someone replies to My Comment