Skip to main content
 首页 » 编程设计

python之字节编译后删除python源文件

2024年10月24日10zhwl

我可以在字节编译后删除python源文件(*.py)吗?我知道这不提供安全性,并且 .pyc/o 文件可以很容易地进行逆向工程;我只想在一次发布中发布更少的文件。

# Clean the python cache 
find ./mypythonstuffs -type f -name "*.py[co]" -delete 
find ./mypythonstuffs -type d -name "__pycache__" -delete 
 
# Remove test directories 
find ./mypythonstuffs -name "test" -type d -exec rm -r "{}" \; 
 
# Byte-compile, ignoring .git subdirectories 
python3 -O -m compileall -f -x r'[/\\][.]git' ./mypythonstuffs 
 
# Remove now-unnecessary??? python source files 
find ./mypythonstuffs -type f -name "*.py" -delete 

请您参考如下方法:

请不要这样做。

首先,python 字节码是一个未记录的实现细节。不能保证将字节码分发给其他人会真正起作用。

其次,它使调试变得困难。你知道回溯是如何显示源代码行的吗?这是从原始源文件中获取的。即使您根本不关心 FOSS,您仍然应该关心从用户那里获得有用的错误报告。

第三,实在没有必要。如果您想分发更少的文件,只需分发 python 源文件。字节码是一个实现细节。您一开始就不应该 checkin 或分发它。