Frozen your python script with PyInstaller - Linux Instance

本文介绍如何在Linux环境下使用PyInstaller将Python脚本打包为独立可执行文件。包括安装依赖、PyInstaller安装及使用说明,并提及了利用Cython进一步隐藏源代码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Frozen your python script with PyInstaller - Linux Instance

In this article, we will describe an example of frozen your python script with PyInstaller in Linux env.

------------------------

1. Pre-Requiremets, Linux

  • PyInstaller requires the ldd terminal application to discoverthe shared libraries required by each program or shared library.It is typically found in the distribution-packageglibc or libc-bin.
  • It also requires the objdump terminal application to extractinformation from object files.This is typically found in the distribution-packagebinutils.
  • Install python-pip:  $ sudo apt-get install python-pip

2. Install pyInstaller

       $ sudo pip install pyinstaller

3. How to use

       Write your python code and save it as "myscript.py" at the path: ~/ws_pyi/

      $ cd ~/ws_pyi

      $ pyinstaller --clean --key <KEY> -F myscript.py

      This will create an excutable file myscript at the path: ~/ws_pyi/dist/

      For more options and details: https://pythonhosted.org/PyInstaller/#what-pyinstaller-does-and-how-it-does-it

4. Another app you can use to frozen your .py code: Cython

     As the pyInstaller's manual says:

     Hiding the Source Code:

  • The bundled app does not include any source code.However, PyInstaller bundles compiled Python scripts (.pyc files).These could in principle be decompiled to reveal the logic ofyour code.
  • If you want to hide your source code more thoroughly, one possible optionis to compile some of your modules with Cython.Using Cython you can convert Python modules into C and compilethe C to machine language.PyInstaller can follow import statements that refer toCython C object modules and bundle them.
  • Additionally, Python bytecode can be obfuscated with AES256 by specifyingan encryption key on PyInstaller's command line. Please note that it is stillvery easy to extract the key and get back the original bytecode, but itshould prevent most forms of "casual" tampering.

-------------------------

For Details, Please Refer pyInstaller's manual: https://pythonhosted.org/PyInstaller/

python38.zip . ../project/Lib/site-packages ../project # Uncomment to run site.main() automatically #import site ,嵌入式python配置好pth文件后运行报错.\python\python.exe .\project\manage.py runserver 0.0.0.0:8000 Watching for file changes with StatReloader Performing system checks... Exception in thread django-main-thread: Traceback (most recent call last): File "threading.py", line 932, in _bootstrap_inner File "threading.py", line 870, in run File "F:\微信信息管理\python\../project/Lib/site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "F:\微信信息管理\python\../project/Lib/site-packages\django\core\management\commands\runserver.py", line 133, in inner_run self.check(display_num_errors=True) File "F:\微信信息管理\python\../project/Lib/site-packages\django\core\management\base.py", line 485, in check all_issues = checks.run_checks( File "F:\微信信息管理\python\../project/Lib/site-packages\django\core\checks\registry.py", line 88, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "F:\微信信息管理\python\../project/Lib/site-packages\django\core\checks\urls.py", line 14, in check_url_config return check_resolver(resolver) File "F:\微信信息管理\python\../project/Lib/site-packages\django\core\checks\urls.py", line 24, in check_resolver return check_method() File "F:\微信信息管理\python\../project/Lib/site-packages\django\urls\resolvers.py", line 494, in check for pattern in self.url_patterns: File "F:\微信信息管理\python\../project/Lib/site-packages\django\utils\functional.py", line 57, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "F:\微信信息管理\python\../project/Lib/site-packages\django\urls\resolvers.py", line 715, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "F:\微信信息管理\python\../project/Lib/site-packages\django\utils\functional.py", line 57, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "F:\微信信息管理\python\../project/Lib/site-packages\django\urls\resolvers.py", line 708, in urlconf_module return import_module(self.urlconf_name) File "importlib\__init__.py", line 127, in import_module File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "F:\微信信息管理\python\../project\project\urls.py", line 23, in <module> import wx.urls File "F:\微信信息管理\python\../project\wx\urls.py", line 5, in <module> from wx import views File "F:\微信信息管理\python\../project\wx\views.py", line 7, in <module> from wxauto import * File "F:\微信信息管理\python\../project/Lib/site-packages\wxauto\__init__.py", line 1, in <module> from .wxauto import WeChat项目结构: ├─project │ ├─Include │ ├─Lib │ │ └─site-packages │ ├─wx │ ├─login │ ├─project │ ├─Scripts │ ├─share └─python,使用指令python .\project\manage.py runserver 0.0.0.0:8000可以正常运行,说明项目代码没有问题,但是使用嵌入式python却报错,这是怎么回事
07-07
2025-06-19 09:08:06 - INFO - 脚本启动 2025-06-19 09:08:06 - INFO - 输出目录: C:/pyMet/figures 2025-06-19 09:08:06 - INFO - 导入依赖库... 2025-06-19 09:08:07 - DEBUG - Could not import boto3, continuing with reduced functionality. 2025-06-19 09:08:07 - DEBUG - GDAL data found in package: path='C:\\Python313\\Lib\\site-packages\\rasterio\\gdal_data'. 2025-06-19 09:08:07 - DEBUG - PROJ data found in package: path='C:\\Python313\\Lib\\site-packages\\rasterio\\proj_data'. 2025-06-19 09:08:09 - DEBUG - matplotlib data path: C:\Python313\Lib\site-packages\matplotlib\mpl-data 2025-06-19 09:08:09 - DEBUG - CONFIGDIR=C:\Users\Administrator\.matplotlib 2025-06-19 09:08:09 - DEBUG - interactive is False 2025-06-19 09:08:09 - DEBUG - platform is win32 2025-06-19 09:08:09 - DEBUG - CACHEDIR=C:\Users\Administrator\.matplotlib 2025-06-19 09:08:09 - DEBUG - Using fontManager instance from C:\Users\Administrator\.matplotlib\fontlist-v390.json 2025-06-19 09:08:10 - DEBUG - Could not import boto3, continuing with reduced functionality. 2025-06-19 09:08:10 - DEBUG - GDAL data found in package: path='C:\\Python313\\Lib\\site-packages\\fiona\\gdal_data'. 2025-06-19 09:08:10 - DEBUG - PROJ data found in package: path='C:\\Python313\\Lib\\site-packages\\fiona\\proj_data'. 2025-06-19 09:08:10 - DEBUG - GDAL data found in package: path='C:\\Python313\\Lib\\site-packages\\fiona\\gdal_data'. 2025-06-19 09:08:10 - DEBUG - PROJ data found in package: path='C:\\Python313\\Lib\\site-packages\\fiona\\proj_data'. 2025-06-19 09:08:10 - INFO - 找到高程数据文件: D:/测试/GMTED2010_land.tif 2025-06-19 09:08:10 - INFO - 读取高程数据... 2025-06-19 09:08:10 - DEBUG - Entering env context: <rasterio.env.Env object at 0x0000027A35DB06E0> 2025-06-19 09:08:10 - DEBUG - Starting outermost env 2025-06-19 09:08:10 - DEBUG - No GDAL environment exists 2025-06-19 09:08:10 - DEBUG - New GDAL environment <rasterio._env.GDALEnv object at 0x0000027A35C17C70> created 2025-06-19 09:08:10 - DEBUG - Installing FilePath filesystem handler plugin... 2025-06-19 09:08:10 - DEBUG - GDAL data found in package: path='C:\\Python313\\Lib\\site-packages\\rasterio\\gdal_data'. 2025-06-19 09:08:10 - DEBUG - PROJ data found in package: path='C:\\Python313\\Lib\\site-packages\\rasterio\\proj_data'. 2025-06-19 09:08:10 - DEBUG - Started GDALEnv: self=<rasterio._env.GDALEnv object at 0x0000027A35C17C70>. 2025-06-19 09:08:10 - DEBUG - Entered env context: <rasterio.env.Env object at 0x0000027A35DB06E0> 2025-06-19 09:08:10 - DEBUG - Sharing flag: 0 2025-06-19 09:08:10 - DEBUG - Nodata success: 1, Nodata value: 32767.000000 2025-06-19 09:08:10 - DEBUG - Dataset <open DatasetReader name='D:/测试/GMTED2010_land.tif' mode='r'> is started. 2025-06-19 09:08:10 - DEBUG - Exiting env context: <rasterio.env.Env object at 0x0000027A35DB06E0> 2025-06-19 09:08:10 - DEBUG - Cleared existing <rasterio._env.GDALEnv object at 0x0000027A35C17C70> options 2025-06-19 09:08:10 - DEBUG - Stopped GDALEnv <rasterio._env.GDALEnv object at 0x0000027A35C17C70>. 2025-06-19 09:08:10 - DEBUG - Exiting outermost env 2025-06-19 09:08:10 - DEBUG - Exited env context: <rasterio.env.Env object at 0x0000027A35DB06E0> 2025-06-19 09:08:10 - INFO - 获取山西地图数据... 2025-06-19 09:08:10 - INFO - 下载地图数据: https://geo.datav.aliyun.com/areas_v3/bound/140000.json 2025-06-19 09:08:10 - ERROR - 获取地图数据失败: 'NoneType' object has no attribute 'drvsupport'
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值