Creating a Python project in Visual Studio

Although you can use the Python interpreter console to try out Python statements, the console doesn’t allow us to save what we’ve written, so it can get quite tedious having to type in commands repeatedly. Given the Visual Studio environment, it’s much easier to create a Python project which allows us to store Python code in one or more files, and run the program with a click of a button.

To create a Python project, start Visual Studio and select ‘Create a new project’ from the menu on the right.

Visual Studio menu

Now select Python from the drop-down menu at the top, then Python Application.

Selecting a new Python project.

You will now get the ‘Configure your new project’ window. Enter a name for the project (we’ve chosen ‘PythonPerson’ here), and select where on your hard drive you want to store the project. In my case, I reserve the C drive for the Windows operating system and some programs, but due to limited space on the C drive, I store most other things on the larger D drive. Feel free to put your code wherever you like – Visual Studio will keep track of the locations.

Naming a new Python project.

Now click Create, and your new project opens in Visual Studio’s main window. The main window on the upper left is an editor for your initial Python source code file, which is given the Project name (PythonPerson) with a .py extension, so the filename is Code that you enter into this file forms the entry point of the program.

Before we enter any code, look at the Solution Explorer on the upper right.

Solution explorer.

Solution explorer is the central road map for your project. It contains links to all the files in the project, together with the contents of these files. We’ll use the Solution Explorer to add a class to our Python project. If you’ve done any object oriented programming in a language such as Java, C# or C++, you’ll be familiar with classes; if not, don’t worry, we’ll get to classes a bit later.

To add a class to our project, right-click on the PythonPerson line (the line just below “Solution ‘PythonPerson’ (1 of 1 project)) and then right-click again on Add. Select ‘New Item…’ from the final right-most menu.

Adding a new item to a project.

You can also use the keyboard shortcut Ctrl+Shift+A to do the same thing.

In the dialog box that appears, select ‘Python class’ from the list, and then enter the name of this class in the text box at the bottom. We’ll call it Person. You can enter the name as the full file name or just as Person, in which case, Visual Studio will add the .py extension automatically.

Back in the main window of Visual Studio, the editor has now opened for editing. You’ll see the lines

class Person(object):
    """description of class"""

The first line declares the Person class, and the second line is a comment giving a description of what the class represents.

We can add some attributes to the class, as shown:

class Person(object):
    """Attributes of a person"""
    firstName = 'Glenn'
    lastName = 'Rowe'
    age = 39

I’ve edited the description, and added 3 attributes. Note that, unlike most other common languages, Python does not require you to specify the data types of the fields in the class. In fact, Python usesĀ dynamic typing, meaning that the data type of a variable is determined by its current contents, and can change over the course of a program. More on this later.

To use this class, we return to the file in Visual Studio and enter the following code:

from Person import Person
me = Person();
print(me.firstName, me.lastName)

The first line tells the Python interpreter to look in the file (the ‘from Person’ bit) and from that file, import the definition of the class ‘Person’. The Person class is now available for use in subsequent code in the file.

Line 2 creates an object called ‘me’ which is an instance of the Person class, and line 3 prints out the firstName and lastName fields from that object.

I would advise you to type in the above code manually in Visual Studio rather than just copying and pasting it from the web page. The reason for this is that you will see Visual Studio’s intellisense feature in action. Intellisense detects possible completions for your code as you type, and can save you a lot of time and errors.

Having entered the code above in the two files and, you can run the result by clicking on the Start button (with the little green triangle) in Visual Studio’s toolbar at the top of the window. An output window will appear, containing the result:

Glenn Rowe
Press any key to continue . . .

At this point, feel free to experiment by adding fields to the Person class, or adding other statements to the file, just to see what they do. You’ll no doubt encounter some errors, but as you’re not dealing with any code that deletes files or modifies databases, you can’t really do any damage.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.