Running Python scripts from Visual Studio Code

I am starting to run trainings in Python, and we opted for Visual StudioCode to be the default Python IDE as it is free, multi-platform and provides a pretty good coding experience. Any participant will be free to use his favorite IDE, and if any participant encounters an issue on his local machine, we’ll invite him to use Cloud9. This sounds like a plan!

Now, let’s discuss some simple steps to run Visual Studio Code on your laptop.

This guide assumes you have a Python runtime on your machine (whether 2.7 or 3.5), and VS Code version 1.9 or above.

The easiest way to start with VS Code is to have a set of scripts at hand that you can start from. If you’re looking for a few samples, grab those proposed at the end of this blog. Then, go to your python scripts directory and type:

$ code .

Once VS Code is started, you’ll see your .py scripts in the file explorer on the left.

First, you want to add the Python extension from Don Jayamanne, to your VS Code environment. Hit Ctrl-Shift-X, and look for the extension, then install it.

Then, open one of your scripts, and hit the Open Terminal menu to instantly run your script.

To start a debug session, add a breakpoint in your script, and pick the debug icon on the left. As you don’t have any python debug configuration yet, click the wheel, and a default debugging configuration will be created, and the corresponding launch.json file will open.

Now, click the green right arrow, and start debugging your script…

The instructions below will help you with some python code samples, or troubleshoot your python installation.

How to check your Python environment version

Run the following commands, and check you are running the right level of versions for python and pip.

Depending on your version of Python, you should get one of the outputs below (the revision numbers — 3rd digit — may differ though).

$ python -V
Python 2.7.11
$ pip -V
pip 8.1.2 from c:\python27\lib\site-packages (python 2.7)

or if you’re running Python 3.5:

$ python -V
Python 3.5.1
$ pip -V
pip 9.0.1 from c:\python35\lib\site-packages (python 3.5)

If warned, proceed to a pip upgrade with the instructions on the command line. I was personally invited to upgrade to pip 9 at that step.

You may need to run both versions 2.7 and 3.5 of python on your machine. This is my case as I use the gcloud tools which requires python 2.7, and our Cisco Spark learning labs at DevNet require version 3.5.

You can then force Visual Studio to use a specific python runtime by reaching to your VS Code settings. Then, add a “python.path” property to your user or workspace settings. Choose user settings if you want this preference to be shared among all your workspaces.

How can I check if a module is installed ?

It only takes a shell snippet. If the module is not installed, you get an error on line 1. If it is installed, line 2 returns 0.

$ python -c "import flask"
$ echo $?

and to check the version of the module, type pip show as in:

$ pip show flask

Some python samples to run with VS Code

If you have no python directory filled with python script samples, clone the DevNet samples scripts for Cloud Collab, and change to the directory as described below.

In that directory, you’ll find a script, as well as a simple script built with the flask library.

Open VS Code from this directory by simply typing “code .”

$ git clone
$ cd devnet-express-cloud-collab-code-samples/
$ cd itp/collab-tools-visual-studio-code-ide-itp/
$ ls
$ code .