Ffile2fix
Sign in Get started

What is a .py file?

A .py file is a Python source file: plain text that the Python interpreter runs as a script or imports as a module. You meet them in automation scripts, data science projects, Django and Flask apps, and tools you download from GitHub.

Key facts

  • Plain text, UTF-8 by default since Python 3; indentation is part of the syntax.
  • MIME type is usually text/x-python (also seen as text/x-script.python).
  • Python compiles imported modules to .pyc bytecode in a __pycache__ folder; the .py file stays the source of truth.
  • Usually a few KB; projects list their third-party dependencies in requirements.txt or pyproject.toml.
  • Can start with a shebang line such as #!/usr/bin/env python3 so it can run directly on Unix-like systems.

How to open a .py file

Windows

Edit in VS Code (with the Python extension), PyCharm or IDLE. Install Python from python.org or the Microsoft Store and run py script.py in Command Prompt or PowerShell.

macOS

Edit in VS Code, PyCharm or BBEdit. Install Python from python.org or with brew install python, then run python3 script.py in Terminal.

Linux

Most distributions ship Python 3. Edit with nano, vim or VS Code and run python3 script.py; add chmod +x script.py to run it as ./script.py when it has a shebang.

Common problems and fixes

IndentationError or TabError
The file mixes tabs and spaces or has inconsistent indentation. Configure your editor to insert 4 spaces and convert existing tabs to spaces.
ModuleNotFoundError: No module named ...
The package is not installed for the interpreter you are using. Create a virtual environment (python3 -m venv .venv), activate it and run pip install -r requirements.txt.
SyntaxError: Non-UTF-8 code or UnicodeDecodeError
The file was saved in a legacy encoding such as Windows-1252. Re-save it as UTF-8, or read data files with an explicit encoding= argument.
Double-clicking the file flashes a window and closes
The script finished or crashed immediately. Run it from a terminal so you can read the output and any traceback.
'python' is not recognized / command not found
Python is not on your PATH. On Windows use the py launcher or re-run the installer with Add python.exe to PATH; on macOS and Linux call python3.

Often converted to or from: Jupyter notebook (.ipynb), Standalone executable (PyInstaller), PHP or Node.js (manual port), Plain text

Frequently asked questions

How do I run a .py file?

Open a terminal in the file's folder and run python3 script.py (or py script.py on Windows). Install any dependencies listed in requirements.txt first, ideally inside a virtual environment.

What is the difference between .py and .pyc?

.py is the human-readable source; .pyc is bytecode Python generates automatically to load modules faster. You can delete __pycache__ folders safely and Python will rebuild them.

Is it safe to run a .py file from the internet?

Only if you trust the source, because a script runs with your user's permissions and can read files or install software. Read it in an editor before running it.

See all file types → · Browse error fixes →