不想打包,又不想泄露核心代码怎么办
最简单的方法,被动的生成.pyc文件:
直接把启动项目后,项目的核心代码生成的__pycache__文件夹里对应的 .pyc 预编译文件复制出来,
然后删掉核心代码xxx.py
再把xxx.cpython-39.pyc文件改一下名字改成xxx.pyc
这样项目就能正常跑了!!!
注意.pyc文件的内容跟python的版本相关,不同版本的解释器,编译同样的.py文件生成的.pyc文件是不一样的
还可以使用模块来主动的将.py文件 预编译成.pyc文件
终端输入命令 python -m py_compile test.py
或者运行脚本文件
import py_compile
py_compile.compile('test.py')
这样会在test.py文件所在的目录下,生成一个__pycache__文件夹,
其中存有一个test.cpython-39.pyc文件,
这个pyc文件就是test.py预编译之后生成的文件。
一般来说,我们的工程都是在一个目录下的,一般不会说仅仅编译一个py文件而已,
而是需要把整个文件夹下的py文件都编译为pyc文件,python又为了我们提供了另一个模块:compileall
使用方法如下:
import compileall
compileall.compile_dir(r'C:\Users\abc\Desktop\release_project')
也可以使用终端:
python -m compileall C:\Users\abc\Desktop\release_project
这样就会在C:\Users\abc\Desktop\release_project目录下生成一个新的目录__pycache__,
这个目录的下面会存放所有的和python源代码对应的pyc文件
脚本文件实现将项目里面所有的.py文件预编译成.pyc
其他不是.py的文件直接复制
这样生成的这个dist文件夹里面,
就有该项目能运行的所有文件了
import os
import py_compile
import sys
import shutil
def compile_python_files(src_dir, dest_dir, except_file):
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for root, dirs, files in os.walk(src_dir):
for file in files:
if file.endswith('.py') and file not in except_file:
py_file_path = os.path.join(root, file)
pyc_file_path = py_file_path[:-3] + '.pyc'
pyc_file_path = os.path.join(dest_dir, os.path.relpath(pyc_file_path, src_dir))
dirname = os.path.dirname(pyc_file_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
py_compile.compile(py_file_path, cfile=pyc_file_path, doraise=True)
else:
src_file_path = os.path.join(root, file)
dest_file_path = os.path.join(dest_dir, os.path.relpath(src_file_path, src_dir))
if not os.path.exists(os.path.dirname(dest_file_path)):
os.makedirs(os.path.dirname(dest_file_path))
shutil.copy(src_file_path, dest_file_path)
print("Compilation complete.")
file_dir='C:/Users/10596/Desktop/server-suzhougaotienewcity_test/serve_flask_dev.py'
def compile_file(file_dir):
py_compile.compile(file_dir)
print("Compilation complete.")
if __name__ == "__main__":
src_dir = 'C:/Users/10596/Desktop/server-suzhougaotienewcity_test'
dest_dir = './dist'
except_file = ['settings.py', '__init__.py']
compile_python_files(src_dir, dest_dir, except_file)
用cython模块直接对python代码加密成.pyd文件
比如 shenjieyun.pyx
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
from distutils.core import setup, Extension
from Cython.Build import cythonize
ext = Extension(name='shenjieyun',
sources=['shenjieyun.pyx'])
setup(ext_modules=cythonize(ext))
python setup.py build_ext --inplace
脚本文件自动将对应目录下的所有.py文件转化成.pyd文件,
并保存到对应的目录下,还保留原目录的文件结构
from Cython.Build import cythonize
from distutils.extension import Extension
from distutils.core import setup
import os
import shutil
def compile_python_files(input_directory, output_directory, except_file):
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for root, dirs, files in os.walk(input_directory):
for file in files:
if file.endswith('.py') and file not in except_file:
module_name = os.path.splitext(file)[0]
py_file_path = os.path.join(root, file)
pyd_file_path = py_file_path[:-3] + '.pyd'
fin_pyd_file_path = os.path.join(output_directory, os.path.relpath(pyd_file_path, input_directory))
dirname = os.path.dirname(fin_pyd_file_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
ext = Extension(module_name, [py_file_path])
setup(
ext_modules=cythonize(ext),
script_args=["build_ext", "--build-lib", dirname]
)
else:
old_copy_file_path = os.path.join(root, file)
new_copy_file_path = os.path.join(output_directory, os.path.relpath(old_copy_file_path, input_directory))
if not os.path.exists(os.path.dirname(new_copy_file_path)):
os.makedirs(os.path.dirname(new_copy_file_path))
shutil.copy(old_copy_file_path, new_copy_file_path)
print("Compilation complete.")
if __name__ == "__main__":
input_directory = "C:/Users/10596/Desktop/server-5granuepositioning/shenjieyun"
output_directory = "C:/Users/10596/Desktop/server-5granuepositioning/shenjieyun_dist"
except_file = ['settings.py', '__init__.py']
compile_python_files(input_directory, output_directory, except_file)
print("所有 .py 文件转换为 .pyd 文件完成")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY