
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Understanding Python File Extensions
What are Python File Extensions?
Python file extensions are the suffixes that are added to the end of file names, which indicate the type of content inside the file and how it relates to the Python programming environment. These extensions help the interpreter, operating system, and developers understand how to process or execute the file.
The .py, .pyc, .pyo, and .pyd files are the most commonly used Python file extensions and have their significance when it comes to executing python programs. Here are the Python file extensions and their descriptions -
- .py: The input source code that you've written.
- .pyc: The compiled bytecode. If we import a module, then Python will build a *.pyc file that contains the bytecode to make importing it again later easier and faster.
- .pyo: A *.pyc file that was created while optimizations (-O) were on. This extension is now deprecated.
- .pyd: A Python dynamic module file on Windows, which is similar to a .dll used for C/C++ extensions.
- .pyw: A Python GUI script file on Windows that runs without opening a command prompt window.
Let's see each of the Python File Extensions in detail for our better understanding -
.py files
The extension ".py" is the standard file extension for Python source code files. These files contain Python code that can be executed by a Python interpreter. Python source code is written in files with a .py extension. For example, a file named my_script.py contains Python code that can be executed by a Python interpreter.
Example
Here is an example that shows how to execute the Python file with .py extension -
# hello.py # my_script.py def greet(name): print("Hello, " + name + "!") greet("Alice")
Here is the output of the above Python code -
Hello, Alice!
The above code can be executed by using the command python filename.py in the command prompt.
.pyc files
.pyc is the file extension for compiled Python code files. When a .py file is executed, the Python interpreter compiles the code to bytecode and saves it in a .pyc file to improve performance on subsequent executions.
When we run this script then the interpreter will create a compiled bytecode version of the code and save it to a file named my_script.pyc.
In a few cases, we may not be able to see the compiled Python files. In such cases, we can generate the compiled Python code files forcefully by running the below code in the command prompt, then the file will be generated in the same directory -
python -m compileall filename.py
Example
Below is an example, in which we will create a compiled Python file for the script sample.py in the same directory -
python -m compileall sample.py
Here is the output of executing the above command in the command prompt -
Compiling 'sample.py'...
The compiled Python code files with .pyc extension will be generated in the directory __pycache__ where the Python file is located.
.pyo files
The .pyo is another file extension for compiled Python code files. The only difference between .pyc and .pyo files is that .pyo files are compiled with optimizations enabled. If you run this script with the -O flag interpreter will create an optimized compiled bytecode version of the code and save it to a file named filename.pyo.
Example
Following is an example of creating compiled Python code files with .pyo extensions -
# hello.py # my_script.py def greet(name): print("Hello, " + name + "!") greet("Alice")
Below is the output of the above code -
Hello, Alice!
Running this code will generate a my_script.pyc file, but running the code with the -O flag (python -O my_script.py) will generate a my_script.pyo file instead.
.pyd files
.pyd is a file extension used on Windows for binary files that contain compiled Python code. These files are similar to .pyc files but are designed to be used as dynamic link libraries (DLLs) that can be loaded by other programs.
If we have a Python module that contains code written in C or C++, then the compiled code is saved to a shared library file with a .pyd file extension. It must be noted that .pyd files are specific to the Windows platform. On other platforms, such as macOS or Linux, the equivalent file extension is .so(shared object) or .dylib (dynamic library).
Example
Here is an example of creating the compiled Python code with the extension .pyd for the executed Python file -
# mymodule.py # my_module.py def add(a, b): return a + b print(add(3,4)) # my_module.pyd # code implements the 'add' function and is compiled to a shared library
Here is the output of the above Python code -
7
Compiling the above code with cython --embed mymodule.py will generate a mymodule.c file which can then be compiled into a mymodule.pyd file using a C compiler.