Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Running Python scripts: VS Code vs. Terminal

Learn how to run a Python script from within VS Code as well as a terminal outside of VS Code.

In this video, you will learn how to run a Python script from within VS Code as well as a terminal outside of VS Code.

As we saw in the video, when you define a string between three quotation marks at the top of your script, the Python interpreter identifies this as a ‘doc’ string. This string will automatically be stored with the name ___doc___.

Docstrings are useful when used to describe what the script is doing. By having one in your script, when a peer is looking at your code, or when you are looking at it 6 months after creating it, it is clear what the script is aiming to achieve.

Running Python script within the VS Code terminal

Follow along

The file used in this exercise is python_argv_ex1.py and is available for download from the Downloads section below. Once you have downloaded the file, open the file in VS Code, and follow along with the video.

Mapping command shortcuts

The presenter made use of a command shortcut to run a line of code. To create your own shortcuts open the command palette.

  • Mac: cmd+shift+p
  • Windows: ctrl+shift+p

Select ‘keyboard shortcuts’, and press the pencil button on the right of the command to edit to shortcut preference.

The presenter used the shortcut cmd R to run his script within the terminal in VS Code.

Running Python script from a terminal outside VS Code.

When running your Python script from a terminal outside of VS Code, here are the steps summarized from the video:

  • Move from the User directory to the directory where the scripts are stored.
  • Use the command “python” followed by the name of the script, then run the script.

sys.argv

To use the sys.argv command, it is necessary for you to import the sys package in Python. This can be done easily by using the code line: import sys as seen in the video. argv is simply a list of the command line arguments that we passed to the Python interpreter. print(sys.argv) will print the commands that produced the output in the terminal once your script has been run.

Let’s take a closer look at sys.argv in the the next step.

This article is from the free online

Intermediate Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now