程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

use. PyC to encrypt Python code

編輯:Python

Conventional python Packaging tools ( for example wheel), Yes, it will python The code enters a compressed package ( for example .tar.gz), And changed his name to .whl. Unpack during installation , Then the source code will be placed in site-package below , So for some needs python Code confidentiality is not applicable ..
When the source code needs to be kept secret , It can be considered to pass .pyc Instead of .py To provide services .
The following simple steps are explained :

  1. Generate .pyc
    Use py_compile To generate pyc file . If import Generated pyc File failed , Then change the file name to the module name . Then delete all py file
    Reference material :https://zhuanlan.zhihu.com/p/344040942
  2. Create a directory at the same level of the project root directory setup.py, The content includes
from setuptools import setup,find_packages
import sys
import compileall
# Compile all the .py files under my_proj/m1.
# The pyc files get created in the same directory alongside the py file
compileall.compile_dir('dir')
# Not including the m1 directory in the packages, but including pyc patterns in
# package_data
setup(
name='my-pyapp',
version='0.0.1',
packages=find_packages(),
package_data={

'': ['dir/*.pyc'],
},
)

Through here package_data To introduce .pyc
Reference material :https://gist.github.com/raghavan97/9e1e6fadc838978666fd47a08c90ba95
3. perform

pip install wheel
python setup.py bdist_wheel

To create wheel package .

The current problems :.pyc After import , Can achieve import, however import The method of the completed object is inaccessible , The error information is as follows :

AttributeError: module 'xxx' has no attribute x'

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved