The difference between Python extensions

extension name

When writing Python programs, our common extensions are py, pyc, but there are actually several other extensions. The following is the usage of several extensions.

py

py is the most basic source code extension

pyw

Pyw is another source code extension. The only difference from py is that double-clicking the source code of the pyw extension under windows will call pythonw.exe to execute the source code. This execution method will not have a command line window. It is mainly used when the console information is not required to be seen when GUI programs are released.

pyc

When executing python code, it is often seen that a pyc file with the same name is automatically generated in the same directory. This is the bytecode compiled from the python source code. Generally, the pyc file of the py file referenced in your code is automatically generated when the code is executed. This file can be executed directly, and the source code can not be seen when opened with a text editor.

pyo

pyo is an optimized encoded file similar to pyc.

pyd

pyd is not generated from a python program, but an extension written in other languages that can be called by python, such as a dynamic link library written in C++ that can be called by python

Choice of program release

Take windows platform as an example

Packaged as a normal executable program

If you want to publish the program publicly, the most common way is to package it into an exe program. The advantage of packaging is that users do not need to consider the Python operating environment, and it is also easy to be accessed

Suffer. But the bad thing is that generally packaged programs are relatively large, and because they rely on module packaging, compatibility issues may occur.

The commonly used packaging module is pyinstaller. The commonly used packaging commands are:

pyinstaller -F example.py

In addition, if it is a GUI program that does not require a console window, you can also add the -w parameter:

pyinstaller -w -F example.py

Publish compiled pyc/pyo

If users have a python environment and do not want them to see the source code, they can choose to publish pyc/pyo files.

The directly executed py file will not automatically generate pyc, and it needs to be compiled manually. Single file compilation:

import py_compile
py_compile.compile(r'c:\test.py')

Folder compilation:

import compileall
compileall.compile_dir(dirpath)

Compile into pyo:

python -O -m py_compile file.py

If it is a GUI program that does not require a console window, you can create a new pyw file to call the main pyc program. Only need to import pyc program in pyw.

Release source py

Release source code is generally used by open source projects, there is nothing to explain. Just send the py file directly, as long as the other party has an environment, it can run

Recommended Posts

The difference between Python extensions
The difference between the syntax of java and python
What is the difference between python and pycharm
The difference between python2.x and python3.x
The difference between CentOS 7 and CentOS 6
What is the difference between synchronous and asynchronous Python?
The difference between CentOS and Ubuntu
The difference and connection between Centos and Redhat
What is the difference between a python script and a web page
2.1 The Python Interpreter (python interpreter)
Consolidate the Python foundation (2)
The difference between CentOS and Ubuntu in Linux system
Consolidate the foundation of Python (4)
python guess the word game
Consolidate the foundation of Python(6)
Ubuntu install the latest Python 3.
Python realizes the guessing game
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
Python implements the brick-and-mortar game
First understanding of the difference between centos7 and centos6 (kernel, commands, etc.)
The usage of wheel in python
How Python implements the mail function
How does python change the environment
Use C++ to write Python3 extensions
Python handles the 4 wheels of Chinese
Python3 implements the singleton design pattern
How to save the python program
Python simulation of the landlord deal
What is the use of Python
​What are the numbers in Python?
Python implements the steepest descent method
Python implements the actual banking system
Talking about the modules in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
How to view the python module
Python implements the aircraft war project
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python