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