What Is a .PY File? (.py, .pyw, .pyc, .ipynb)

A .py file is a Python script: a plain text file of Python source code that the Python interpreter runs. It is used for automation, data analysis, web back ends, machine learning and teaching. You can open and read any .py file in a text editor, but running one requires Python to be installed.

Extension
.py, .pyw, .pyc, .ipynb
Full name
Python Source Code
Type
Code and web file
Developed by
Python Software Foundation (Python created by Guido van Rossum)

About PY files

The .py file extension marks Python source code. Running python3 script.py (or py script.py on Windows) executes it from top to bottom. Related extensions include .pyw for Windows scripts that run without a console window, .pyc for compiled bytecode that Python creates automatically in __pycache__ folders, and .ipynb for Jupyter notebooks, which are JSON files mixing code, output and notes.

A Python file can also be a module that other code imports. That is why the file name matters: import uses it directly, so names must be valid identifiers, typically lowercase with underscores. Projects declare dependencies in requirements.txt or pyproject.toml; a script that fails with ModuleNotFoundError usually needs those packages installed with pip.

A .py file is a program and runs with your user's permissions, so a script from an untrusted source can delete files, steal data or install malware. Read it before running, especially if it was sent unexpectedly or downloaded from a forum. Opening it in an editor is safe; running it is the step that matters.

What PY files are used for

  • Automation scripts for files, spreadsheets and web tasks
  • Data analysis and machine learning code
  • Web application back ends (Django, Flask, FastAPI)
  • Programming courses and coding exercises
  • Plugins and add-ons for apps like Blender and QGIS

How to open a .py file

Windows

To read or edit, right-click > Open with > Notepad or open it in VS Code. To run it, install Python from python.org or the Microsoft Store, then run py script.py in Terminal.

Mac

Open it in TextEdit or VS Code to read. To run, install Python from python.org (or the Xcode Command Line Tools) and run python3 script.py in Terminal.

Linux

Most distributions include Python 3; run python3 script.py, and edit with any text editor.

Online / any browser

Google Colab and other online notebooks run Python in the browser; paste the code or upload the file.

How to convert a .py file

To share a script with people who do not have Python, package it as an executable with PyInstaller, which creates a .exe on Windows or an app bundle on a Mac.

To turn a Jupyter notebook into a .py file, use File > Save and Export Notebook As > Executable Script in JupyterLab, or jupyter nbconvert --to script.

Renaming a .txt to .py works only if the text is valid Python; the extension simply tells editors and the launcher what it is.

Changing the extension in the file name does not convert the file. See how to change a file extension for when renaming is safe.

Naming and renaming PY files

  • Use lowercase letters and underscores (clean_invoices.py); hyphens and spaces make a file impossible to import as a module.
  • Never name a script after a standard library or installed module, such as random.py, email.py or pandas.py, because it will shadow the real module and cause confusing import errors.
  • Name entry points clearly (main.py, cli.py) and keep test files prefixed with test_ so pytest discovers them automatically.

Renaming lots of .py files? See how to rename python files with ai, use the free online bulk renamer for patterns and numbering, or try File Renamer AI to name each file from what is inside it.

Related file types

Frequently Asked Questions

How do I run a .py file?

Install Python, open a terminal in the file’s folder and run python3 filename.py (or py filename.py on Windows). Many editors, including VS Code, also have a Run button.

What app opens .py files?

Any text editor can open them to read: Notepad, TextEdit or VS Code. Python itself is needed to run them.

Is it safe to run a .py file?

Only if you trust the source or have read the code. A Python script can do anything your user account can, including deleting or uploading files.

What is a .pyc file?

It is compiled Python bytecode that Python creates automatically to load modules faster. You can safely delete __pycache__ folders; they are regenerated.

More code and web file extensions · all file extensions