Python files (.py)#

Video Tutorial

A .py file is a text file that contains Python code. It’s the standard file format used for writing and executing Python programs. When you write Python code in a .py file, you can run the file to execute the code it contains, and it can be opened and edited in any text editor or integrated development environment (IDE) like VS Code or IDLE.

In this tutorial you will learn how to do all of these using Visual Studio Code:

Opening a Folder#

Opening a folder in VS Code allows you to manage all your project files in one place.

  1. Press the File button in the top left corner of VS Code.

  2. Select Open Folder… from the dropdown menu.

    Save File in VS Code
  3. Browse to the folder you want to open and select it.

  1. Press the File button in the top left corner of your screen.

  2. Select Open Folder… from the dropdown menu.

    Open Folder in VS Code
  3. Browse to the folder you want to open and select it.

Creating a Python-file#

You can create a Python file using VS code. This is useful if you are writing code that you will need again.

Press the File button in the top left corner of VS Code. Select New File ‣ Python File from the menu that drops down.

IDLE Shell

Press the File button in the top left corner of your screen. Select New File ‣ Python File from the menu that drops down. You may need to hover your mouse around the top of the screen for the menu bar to appear.

IDLE Shell

Saving a File#

Saving a file in VS Code ensures that your work is not lost and can be accessed later.

  1. Press the File button in the top left corner of VS Code.

  2. Select Save or Save As… from the dropdown menu.

    Save File in VS Code
  3. Please choose a location and name for your file, then save it.

Note, you can save a file by pressing Ctrl+S.

  1. Press the File button in the top left corner of your screen.

  2. Select Save or Save As… from the dropdown menu.

  3. Please choose a location and name for your file, then save it.

    Save File in VS Code

Note, you can save a file by pressing Command+S.

Tip

You can enable auto save by pressing the File button in the top left corner of VS Code and then selecting Auto Save.

Opening a File#

Opening a file in VS Code allows you to edit and run your code directly within the editor.

  1. Press the File button in the top left corner of VS Code.

  2. Select Open… from the dropdown menu.

    Save File in VS Code
  3. Browse to the file you want to open and select it.

  1. Press the “File button in the top left corner of your screen.

  2. Select “Open… from the dropdown menu.

    Open File in VS Code
  3. Browse to the file you want to open and select it.

Tip

You can open a file by dragging and dropping it directly into the VS Code window.

Selecting the right interpreter#

Once you have created a Python file, select your interpreter. In short, the interpreter is the version of Python that you use to execute your code.

  1. Press Ctrl+Shift+P

  2. Type Python: Select Interpreter and press Enter once this shows up under the options

    IDLE Shell
  3. Choose the option similar to Python 3.11.X ('base'). Your PC may have a different version of Python.

    IDLE Shell

Tip

In VS Code, pressing Ctrl+Shift+P brings down a search bar where you can search for help with anything.

  1. Press Ctrl+Shift+P

  2. type Python: Select Interpreter and press Enter once this shows up under the options

    IDLE Shell
  3. Choose the option similar to Python 3.11.5 ('base'). You may have a different version of Python on your own PC.

    IDLE Shell

Tip

In VS Code, pressing Ctrl+Shift+P brings down a search bar where you can search for help with anything.

Run a Python File#

Once you’ve written your Python code, you can easily run it in VS Code.

Running Python files/scripts requires one to have set the interpreter, see here.

When a Python file is open in VS Code, a button will appear in the top-right of VS Code. When pressing that button, the currently opened file will be executed.

An example is found towards the end of this video.