.py

What is a .py file?

Python Source File · Code and data · Guido van Rossum / Python Software Foundation · 1991

A .py file is Python source code: plain text instructions run by the Python interpreter. Python is one of the most popular languages, used for web backends, data science, automation and machine learning.

Category
Code and data
Type
Plain text
Is a .py file safe to open? Caution. A .py file is a program: running it executes whatever it contains. Read code from unknown sources before running it.

What is a .py file used for?

Python source is readable text organised by indentation rather than braces. Running python file.py executes it top to bottom. The first run may create a __pycache__ folder of compiled .pyc files to speed up later imports.

Because it is plain text, a .py file opens in any editor, but an editor with Python support such as VS Code or PyCharm adds highlighting, linting and a debugger.

How to open a .py file

The .py format opens in the following apps, grouped by operating system. If one app does not work, try another from the same list. For a step-by-step walkthrough, see how to open a .py file.

Windows

  • VS Code
  • PyCharm
  • IDLE
  • any text editor

macOS

  • VS Code
  • PyCharm
  • any text editor

Linux

  • VS Code
  • vim
  • any text editor

How to convert a .py file

You can convert a .py file to pyc, exe (via PyInstaller) using a conversion tool or the export menu of an app that opens it.

.py file signature (magic bytes)

Programs recognise a .py file by the bytes at the start of the file, not by its name. These are the signatures for Python.

Hex signatureOffsetNote
(no fixed signature)0plain text; no fixed magic bytes

See the full magic-numbers reference for every format.

Frequently asked questions

What is a .py file?

A .py file is Python source code: plain text instructions run by the Python interpreter. Python is one of the most popular languages, used for web backends, data science, automation and machine learning.

How do I run a .py file?

From a terminal, run python file.py (or python3 file.py). Double-clicking may just open it in an editor, depending on your setup.

How do I open a .py file to read it?

Any text editor works. VS Code or PyCharm add syntax highlighting and let you run and debug the code.

What is a .pyc file?

A .pyc is the compiled bytecode Python generates from your .py to load faster next time. You edit the .py, not the .pyc.

Related formats

Sources

Details on this page were checked against authoritative references: