Python in Visual Studio – Part 2

In this post we’re going to look at some features of Python Tools for Visual Studio (PTVS) that weren’t covered in Python in Visual Studio.

Generating a project’s dependencies

If you are working on a project then it is likely at some point that it will have dependencies on other packages downloaded from the Python Package Index (PyPI). If you want other people to use your project then they will also need to install those dependencies.

Fortunately the pip package installer (and therefore PTVS) can create a list of those dependencies and save it to a file, which by convention is named requirements.txt.

To create a requirements file in PTVS…

  • Right click on the Python environment in Solution Explorer.
  • Select Generate requirements.txt.

A new file requirements.txt will be added to your project. If you open it, you’ll see that it contains a list of package names and versions, as shown in this example.

astroid==1.4.5
colorama==0.3.7
httpie==0.9.3
lazy-object-proxy==1.2.2
Pygments==2.1.3
pylint==1.5.5
requests==2.10.0
six==1.10.0
tornado==4.3
wrapt==1.10.8

Here, the packages are pinned to specific versions with ‘==’, but this doesn’t have to be the case. In this example, tornado is pinned to version 4.3, but what if a new version came out, say, 4.3.1, or if we knew that any version from 4.3 to 4.5 would work just fine.

To cover that scenario we can write this:

tornado>=4.3,<=4.5

For more information on the syntax of requirements files take a look at this documentation in the pip user guide.

Installing a project’s dependencies

Similarly, a project’s dependencies can be installed from requirements.txt. You would typically do this if you’d checked a project out of version control, or downloaded it from somewhere like GitHub, because you almost certainly wouldn’t expect to find all of a project’s dependencies checked into version control.

To install a project’s dependencies…

  • Right click on the Python environment in Solution Explorer.
  • Select Install from requirements.txt.

Python Tools for Visual Studio will download and install all of the dependencies listed in requirements.txt, along with any of their dependencies.

Running and debugging individual programs

To run or debug an individual Python program in your project…

  • Find the file under the project in Solution Explorer.
  • Right click on it and select Start without Debugging or Start with Debugging.

Setting the startup file

When running a Python project in Visual Studio, it needs to know which file in the project should be executed.

To set a project’s startup file…

  • Find the file under the project in Solution Explorer.
  • Right click on it and select Set as startup file.

This is the file that will be run when you press F5 / Ctrl F5 to debug / run your project.

Executing in Python interactive

In Python in Visual Studio, we saw how to run the Python interactive shell. Sometimes it is useful to run a program or module, then, once it has finished, use the interactive shell to explore its state. PTVS allows us to do this by running the project in the interactive shell.

To run the project in the Python interactive shell..

  • Go to Debug > Execute Project in Python Interactive.

execute_interactive

In the example above, I ran a simple project in the Python interactive shell. Once it finished, its functions, classes and modules remained active, so I was able to list them with dir() then run the say_hello() function again.

The Class View

Visual Studio has a Class View that lets us navigate quickly around the classes in the project. The class view displays a tree containing the projects in the solution, the files in each project, and the classes and functions in each file. In the example below, the class NamedTupleReader is selected in the top pane, and all of its methods and their parameters are shown in the bottom pane. Double-clicking on one of these methods will bring up that method in the editor.

class_view

Setting conditions in breakpoints

In Python in Visual Studio, I touched on the fact that it was possible to set breakpoints for debugging Python programs, but I didn’t go into further detail, such as the fact that PTVS supports conditional breakpoints.

To set a conditional breakpoint…

  • Left click in the margin to set a breakpoint
  • Right click on the breakpoint and select Conditions…
  • Use the dropdown to set the condition type, eg, Conditional Expression and enter the condition that will cause the breakpoint to trigger.
  • Continue to add conditions as required, then click Close once done.

In the following example, a conditional breakpoint is set to trigger only when the first letter of name is “D”, ie, when name[0] == ‘D’.

breakpoint_condition

Setting actions in breakpoints

Similarly, Python Tools for Visual Studio supports adding actions to breakpoints that can log a message to the output window. Actions don’t have to stop execution (which makes really makes them tracepoints rather than breakpoints). I have to say that I find them extremely useful when debugging when I want to see the value of a variable without necessarily having to stop the program or insert print() functions.

To add an action to a breakpoint…

  • Left click in the margin to set a breakpoint
  • Right click on the breakpoint and select Actions…
  • Enter a message in the edit box, using braces, ie,  and } to interpolate variables.

One thing I’ve noticed that’s different about breakpoint actions when debugging Python is that they don’t seem to support $FUNCTION to display the function name, unlike C# which does. I don’t know if this is a bug – but it always seems to display <unknown> when I do it.

In the following example, an action breakpoint is set to log the value of name is {name} to the output window whenever execution crosses that line.

breakpoint_action

When you need the command line

We’ve seen that you can do much of your Python programming and debugging from the comfort of Visual Studio. But there are scenarios where you might want to drop down to the command line, for example, to use Python command line tools. For example, if you were testing a web API then you might want to install the httpie package into your virtual environment, as this lets you make http requests from the command line. It’s similar to cURL, but simpler to use.

If you were to just open a command prompt directly and type python then you would get the globally installed version of Python, and httpie would most likely not be on the path. How, then, do you get access to a project’s virtual environment from the command line?

To open a command prompt in a project’s virtual environment…

  • Right click on the Python environment in Solution Explorer.
  • Select Open Command Prompt Here…

This will open a command prompt in the base directory of the environment. The command window’s title will reflect the name of the environment.

If you were to type python or use any command line tools such as pip or httpie in this command prompt then you would be using the versions from the virtual environment, not from the globally installed Python. In the example below, the output from pip freeze shows the packages that are installed in the environment.

command_prompt

Finally

I wrote these posts to give Visual Studio users an overview of how to perform everyday Python tasks from within Visual Studio. I think PTVS turns Visual Studio into an excellent Python IDE, and I hope that its support for Python will encourage you to give Python a try.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s