.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.

What opens 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.

Windows

  • VS Code
  • PyCharm
  • IDLE
  • any text editor

macOS

  • VS Code
  • PyCharm
  • any text editor

Linux

  • VS Code
  • vim
  • any text editor

Open a .py file step by step

  1. Confirm the file really is a .py. Drop it into the WhatFileType identifier, which reads its signature in your browser without uploading anything.
  2. On Windows, open it with VS Code or PyCharm.
  3. On Mac, use VS Code or PyCharm.
  4. On Linux, try VS Code or vim.
  5. Still stuck? Convert the .py to pyc, which opens more widely.

Why can't I open my .py file?

Almost always because no installed app understands Python. Install one of the apps above, or convert the file to a format your existing software already reads. If double-clicking opens the wrong program instead of nothing at all, the file association is set to the wrong app rather than the file being broken.

The other possibility is that the file is not really a .py at all. Drop it into the file identifier to check it by its bytes instead of its name. Nothing is uploaded.

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.

Python has no fixed magic number, so there is no signature table on this page. Its bytes alone do not prove the format: identification relies on the extension, the structure of the content and the program that created it. The magic-numbers reference lists the formats that do carry one.

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. How sources are ranked and what is deliberately left out is set out in the methodology.