惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
腾讯CDC
博客园_首页
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
SecWiki News
SecWiki News
美团技术团队
P
Privacy International News Feed
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
D
DataBreaches.Net
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Schneier on Security
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
Forbes - Security
Forbes - Security
D
Docker
T
Tenable Blog
S
Secure Thoughts
雷峰网
雷峰网
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
使用PyOxidizer构建独立的Python应用程序
种瓜 · 2019-06-27 · via 夜行人

文章目录

前言

目录对python程序进行打包方式主要有5种: py2exe、py2app,pyinstaller,cx_Freeze,nuitka。本文介绍一种新工具: PyOxidizer。

本文是使用使用PyOxidizer构建独立的Python应用程序的一个笔记。

基本按照官方教程的引导。

PyOxidizer是indygreg开源了一个项目,用于构建独立的Python应用程序。文档在这儿

有趣的是, PyOxidizer本身基于Rust, 个中原因indygreg这篇文章里做了阐述。

安装

为了使用PyOxidizer,首先需要按照Rust环境。

安装Rust

Rust版本要求: 1.33+.

可以按照rust官方官方的引导说明来安装。

我在MacOS环境里工作,我准备使用rustup,方便将来管理Rust版本,安装过程非常简单: brew install rustup

如果你也使用rustup,安装好之后接下来运行:rustup-init

安装完成之后检查版本:

1
2
➜  ~ rustc --version
rustc 1.35.0 (3c235d560 2019-05-20)

安装PyOxidizer

cargo install pyoxidizer

安装完成后,尝试运行它:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
➜  ~ pyoxidizer
PyOxidizer 0.1.2
Gregory Szorc <gregory.szorc@gmail.com>
Build and distribute Python applications

USAGE:
    pyoxidizer [SUBCOMMAND]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    add                             Add PyOxidizer to an existing Rust project. (EXPERIMENTAL)
    analyze                         Analyze a built binary
    build                           Build a PyOxidizer enabled project
    build-artifacts                 Process a PyOxidizer config file and build derived artifacts
    help                            Prints this message or the help of the given subcommand(s)
    init                            Create a new Rust project embedding Python.
    python-distribution-extract     Extract a Python distribution archive to a directory
    python-distribution-licenses    Show licenses for a given Python distribution
    run                             Build and run a PyOxidizer application
    run-build-script                Run functionality that a build script would perform

使用

创建项目

pyoxidizer init pyapp

该命令将创建一个支持嵌入Python的Rust项目。运行完命令,将打印相关信息及下一步该做什么的提示。

根据提示信息,依次运行:

1
2
3
cd pyapp
pyoxidizer build # 第一次运行会下载对应平台的Python解释器
pyoxidizer run

如果一切顺利,pyoxidizer run将启动了一个Rust可执行文件,它启动了一个交互式Python调试器!尝试输入一些Python代码:

1
2
>>> print("hello, world")
hello, world

自定义Python和打包行为

自定义

项目根目录里有一个自动生成的pyoxidizer.toml文件,该文件决定默认运行时行为。

找到[[embedded_python_run]]部分, 这部分决定Python解释器启动时要执行的操作,调整为:

1
2
3
[[embedded_python_run]]
mode = "eval"
code = "import uuid; print(uuid.uuid4())"

现在我们告诉解释器启动时运行:eval(import uuid; print(uuid.uuid4())

pyoxidizer run输出的结果为: 4ef94bc0-4cbe-4404-9269-0690fec68094

打包第三方库

接下来,让我们试着打包现有的Python应用程序!

我们试着打包第三方库:pyflakes

编辑配置文件:pyoxidizer.toml,使得:

1
2
3
[[packaging_rule]]
type = "pip-install-simple"
package = "pyflakes==2.1.1"

以及

1
2
3
[[embedded_python_run]]
mode = "eval"
code = "from pyflakes.api import main; main()"

这将告诉PyOxidizer你要安装pyflakes的2.1.1版本。在构建时,会运行pip install pyflakes==2.1.1,并将它们添加到生成的二进制文件中。我们试试看:pyoxidizer run -- --help

新的pyoxidizer.toml文件应该类似于:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Multiple [[python_distribution]] sections elided for brevity.

[[build]]
application_name = "pyflakes"

[[embedded_python_config]]
raw_allocator = "system"

[[packaging_rule]]
type = "stdlib-extensions-policy"
policy = "all"

[[packaging_rule]]
type = "stdlib"
include_source = false

[[packaging_rule]]
type = "pip-install-simple"
package = "pyflakes==2.1.1"

[[embedded_python_run]]
mode = "eval"
code = "from pyflakes.api import main; main()"

成功为pyflakes制作了一个可执行文件!

参考