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.
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.
Saving a File#
Saving a file in VS Code ensures that your work is not lost and can be accessed later.
Tip
You can enable auto save by pressing the
button in the top left corner of VS Code and then selecting .Opening a File#
Opening a file in VS Code allows you to edit and run your code directly within the editor.
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.
Press Ctrl+Shift+P
Type Python: Select Interpreter and press Enter once this shows up under the options
Choose the option similar to
Python 3.11.X ('base')
. Your PC may have a different version of Python.
Tip
In VS Code, pressing Ctrl+Shift+P brings down a search bar where you can search for help with anything.
Press Ctrl+Shift+P
type Python: Select Interpreter and press Enter once this shows up under the options
Choose the option similar to
Python 3.11.5 ('base')
. You may have a different version of Python on your own PC.
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.