Python Standalone Deploy Environment

Overview

PyStand

Python 独立部署环境。Python 3.5 以后,Windows 下面都有一个 Embedded Python 的 独立 Python 运行环境,这个 PyStand 就是配合 Embedded Python 使用的。

特性介绍

  • 使用 PyStand + PyQt5 精简版发布 PyQt 程序,打包大小只有 14MB。
  • 使用 PyStand 发布普通 Python 程序,打包大小仅 5MB。

功能说明

  • Windows 下独立 Python 环境的启动器。
  • 自动加载 PyStand.exe 同级目录下面 runtime 子目录内的 Embedded Python。
  • 自动启动 PyStand.exe 同级目录下面的 PyStand.int 程序(Python 代码)。
  • 如果改名,会加载对应名称的 .int 文件,比如改为 MyDemo.exe 就会加载 MyDemo.int
  • 窗口程序,无 Console,但是如果在 cmd.exe 内运行,可以看到 print 的内容。
  • 会自动添加 PyStand.exe 同级目录下的 site-packages 目录,库可以放到里面。

使用方式

  • 用 CMake 生成 PyStand.exe (或者到 Release 里下个现成的)。
  • 下载 Python Embedded 版本,放到 PyStand.exe 所在目录的 runtime 子目录内。
  • 注意 Python Embedded 如果是 32 位,PyStand 配置 CMake 时也需要指明 -A Win32
  • 在 PyStand.exe 所在目录创建 Python 源代码 PyStand.int。
  • 双击 PyStand.exe 就会运行 PyStand.int 里的代码。

常见问题

安装依赖

用一个同 Embedded Python 相同版本的 Python 做一个 venv,然后 pip 独立安装好模块后 将 site-packages 内对应的包复制到 PyStand.exe 的 site-packages 下直接使用。

查看错误

如果在 cmd.exe 内部运行 PyStand.exe 可以看到标准输出和标准错误。

MessageBox

PyStand 添加了一个 os.MessageBox(msg, title) 的接口,可以用来简单显示个对话框。

更换图标

可以替换 appicon.ico 文件并重新编译 PyStand.exe ,或者使用 Resource Hacker 直接 替换 Release 内下载的 PyStand.exe 文件的程序图标。

脚本组织

可以在 PyStand.exe 同级目录新建一个 script 文件夹,将脚本放进去,PyStand.int 里面 就是添加一下 sys.path 然后 import 即可。

发布打包时将 script 文件夹用 zip 压缩成 script.egg 文件,PyStand.int 里检测到该 文件存在就加入到 sys.path,然后再 import。

使用例子

这个回答里我说了详细的用法以及 PyInstaller 的优缺点:

更多的用法可以见 Release 下面的例子。

Comments
  • Python3.9打包easyocr极其依赖后运行出错。

    Python3.9打包easyocr极其依赖后运行出错。

    林爷您好,请教一个问题,我打包一个依赖easyocr的脚本后运行出错: Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] on win32

    image

    image

    image

    我没使用您默认提供的3.8版本,是因为这个版本pip安装easyocr的时候会报依赖混乱的错误。操作系统是win10。 似乎在这个环境下,所有pyd格式的包都没法引入进来。用您提供的python3.8版本可以正常使用lxml,但是用我下载的3.9.9 embedded版本就不行,也会报类似的引包错误。

    求指点。

    opened by zhangbl-yh 12
  • PyQt5 QtWidgets.QFileDialog.getOpenFileName 卡死或程序闪退

    PyQt5 QtWidgets.QFileDialog.getOpenFileName 卡死或程序闪退

    使用PyQt5, 主窗体调用子窗体时,子窗体上有个按钮,点击会打开选择文件的对话框,QtWidgets.QFileDialog.getOpenFileName在embedable Python环境运行,子窗体弹出正常,但程序在运行到getOpenFileName(self, 'title',path)时会卡死,程序无响应,即使在powershell中运行PyStand.exe也看不到报错信息。而在常规的Python环境中,运行OK,对话框正常弹出,可选择文件。 怀疑是缺少了哪个库,或者哪个库安装异常?之前发现embed Python没有tkinter,后来把文件拷贝进去了。这里请教下pywin32模块如何安装到embedable Python环境,直接从虚拟环境中site-packages拷贝到到embed 的site-packages就可以吗?

    opened by ConyKitty 6
  • 由于 Exception.ToString() 失败,因此无法打印异常字符串。

    由于 Exception.ToString() 失败,因此无法打印异常字符串。

    将一个PyQt 5的项目添加到了script 文件夹下,在PyStand.int 中引入,然后在terminal中启动PyStand.exe. 跟句错误提示,缺什么就从Python环境的site-package 中拷贝对应的库到PyStand 的site-package 。前面几个库放进去就好了,然后遇到了这个错误,请问怎么解决啊,怎么能看到是哪里引发的错误

    PS D:\PyBuild\pystand>.\PyStand.exe 由于 Exception.ToString() 失败,因此无法打印异常字符串。

    Python: 3.6.2 32 bit PyStand: 32 bit

    opened by ConyKitty 4
  • 运行含有中文的源文件存在问题

    运行含有中文的源文件存在问题

    创建一个python文件,内容写‘print('视频预览')’,运行报错: File "", line 26, in UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 11: illegal multibyte sequence

    opened by danielweiv 3
  • 有多进程调用时,会重复启动Qt主窗口直到内存耗尽

    有多进程调用时,会重复启动Qt主窗口直到内存耗尽

    入口代码如下,开头会先启动一个后台进程。 python 直接执行正常,通过PyStand.exe 启动,就会不断启动Qt窗口,直到内存耗尽为止。 PyInstaller也有类似问题,按照PyInstaller的方案,加上了freeze_support(),但是问题依旧。 是否需要在PyStand.exe的主程序里添加此调用?

    if "__main__" == __name__:
        multiprocessing.freeze_support()
        multiprocessing.Process(target=VideoServer.start_server, daemon=True).start()
    
        logger.info("启动程序")
        app = QApplication(sys.argv)
        app.setStyleSheet(BASE_STYLE_SHEET)
        win = AppWin(sys.argv)
        win.show()
        sys.exit(app.exec())
    
    opened by rockswang 2
  • 如何支持多进程调用python

    如何支持多进程调用python

    由于项目需要在程序中需要另起一个进程调用python:

    # PyStand.int
    import sys
    sys.path.append("./script")
    
    from subprocess import run
    # run('python ./script/demo.py')  无法找到python
    run('./runtime/python ./script/demo.py')
    

    但这样会导致无法导入site-packages中的库。仔细想来,这也正是PyStand源码的意义所在。

    那么如果有这种需求,该如何实现呢,烦请不吝赐教,谢谢!!

    opened by coolermzb3 2
  • add an option to build PyStand as a console based application

    add an option to build PyStand as a console based application

    基于如下几点原因,GUI 版本的 PyStand.exe 不太适合封装一个 Python 命令行脚本(比如:https://github.com/myd7349/Ongoing-Study/blob/master/python/extract_columns.py ):

    • 未重定向 stdin,导致无法通过 input 函数接收用户输入;
    • GUI+AttachConsole 副作用:GUI 版的 PyStand.exe 在命令行或 PowerShell 中运行后,都需要用户手动敲入 Enter 才能重新返回到命令行;

    此外,命令行版本的 PyStand.exe 也更方便在 IDE 中调试的时候及时查看错误输出(比如:init_script 中的错误输出)。

    本 PR 中新增一个 CMake option PYSTAND_CONSOLE_BASED,只需要:

    cmake .. -A Win32 -DPYSTAND_CONSOLE_BASED=ON
    

    即可将 PyStand.exe 编译为一个控制台应用程序。

    值得讨论的一些事项:

    • 不知道大佬喜不喜欢 PYSTAND_CONSOLE_BASED 这个名字(抑或定义一个 PYSTAND_CONSOLE,或者 PYSTAND_GUI_BASED,并将 PYSTAND_GUI_BASED 默认为 ON);
    • Console 版本的 PyStand.exe 可以考虑屏蔽 os.MessageBox(本 PR 中未屏蔽);
    • Console 版本的 C++ 实现里可考虑用标准输出替换 MessageBox 来向用户呈现错误提示(本 PR 尚未这样做);
    • Console 版本的可考虑使用 _wmain(int argc, wchar_t *argv[]) 作为主函数原型,并抛弃 GetCommandLineW+CommandLineToArgvW(本 PR 尚未这样做);

    参考:

    opened by myd7349 2
  • use binary reading to avoid encoding issues

    use binary reading to avoid encoding issues

    在本地编码是 GBK 编码的情况下,如果 PYSTAND_SCRIPT 使用 UTF-8 编码并包含非 ASCII 字符,可能导致脚本调用失败。

    示例一:

    #!/usr/bin/env python3
    # coding: utf-8
    
    print('你好,世界!')
    

    该例子显示中文乱码。

    示例二:

    #!/usr/bin/env python3
    # coding: utf-8
    
    print('文章简介')
    

    该例子抛出 UnicodeDecodeError 异常。

    实际上 #2 也提到另外一种做法:可以将这些放在另外一个脚本里,并在 PYSTAND_SCRIPT import 之。

    也可以先 compile 再 exec:

    def exec_full(filepath):
        global_namespace = {
            "__file__": filepath,
            "__name__": "__main__",
        }
        with open(filepath, 'rb') as file:
            exec(compile(file.read(), filepath, 'exec'), global_namespace)
    
    # Execute the file.
    exec_full("/path/to/somefile.py")
    

    另一种解决方案是始终使用 utf-8 打开文件(毕竟,utf-8 编码已被很多人接受)。

    text = open(PYSTAND_SCRIPT, 'r', encoding='utf-8').read()
    

    参考:

    opened by myd7349 2
  • free memory returned by CommandLineToArgvW after use

    free memory returned by CommandLineToArgvW after use

    According to https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw:

    CommandLineToArgvW allocates a block of contiguous memory for pointers to the argument strings, and for the argument strings themselves; the calling application must free the memory used by the argument list when it is no longer needed. To free the memory, use a single call to the LocalFree function.

    opened by myd7349 0
  • 命令行中无法使用input

    命令行中无法使用input

    F:\SMALL\PyStand-py38>Hello from F:\SMALL\PyStand-py38\PyStand.int

    Traceback (most recent call last): File "", line 34, in File "F:\SMALL\PyStand-py38\PyStand.int", line 10, in filename = input("请输入EXCEL文件名(带后缀,EXCEL文件放在本文件同目录)") RuntimeError: input(): lost sys.stdin

    opened by yulk 2
  • 将代码使用egg加密后 如何才能正确的读取egg文件内的文件

    将代码使用egg加密后 如何才能正确的读取egg文件内的文件

    在我的项目中 打包时候出现了错误 下面这行代码报错:

    file = os.path.join("UI","Custom_UI","QToolTip.qss")
    with open(file,'r') as f:
        self.setStyleSheet(f.read())
    

    这里的qss文件是一个需要被读取的文件 用来设置UI样式

    压缩成egg后 运行报错:

    C:\Users\xyj\Desktop\3.0.0-alpha>Traceback (most recent call last):
    Traceback (most recent call last):
      File "<string>", line 34, in <module>
      File "C:\Users\xyj\Desktop\3.0.0-alpha\MOS.int", line 13, in <module>
        Run()
      File "C:\Users\xyj\Desktop\3.0.0-alpha\script\Code\MainWindow.py", line 544, in Run
        ui = RunUi()
      File "C:\Users\xyj\Desktop\3.0.0-alpha\script\Code\MainWindow.py", line 58, in __init__
        self.__init__setToolTipDuration()
      File "C:\Users\xyj\Desktop\3.0.0-alpha\script\Code\MainWindow.py", line 498, in __init__setToolTipDuration
        self._toolTip = ToolTip(parent=self)
      File "C:\Users\xyj\Desktop\3.0.0-alpha\script\UI\Custom_UI\QToolTip.py", line 44, in __init__
        self.__setQss()
      File "C:\Users\xyj\Desktop\3.0.0-alpha\script\UI\Custom_UI\QToolTip.py", line 66, in __setQss
        with open(file,'r') as f:
    FileNotFoundError: [Errno 2] No such file or directory: 'UI\\Custom_UI\\QToolTip.qss'
    

    之后我只好更改了代码 将需要被读取的内容改成一个py文件 并且内建一个函数 调用之后 就返回这个文件的内容 虽然这么可以解决这个问题 但是我并不想这么解决 (因为这个方法很不好 而且以后想快速的改这个qss列表 也很麻烦

    改正前: https://github.com/xianyongjian080402/Minecraft-Optimal-Starter_3/tree/b068e9e212fe5cf668a50d4c83523f7510749c5f

    改正后: https://github.com/xianyongjian080402/Minecraft-Optimal-Starter_3/tree/8629cf3f5d694ca0ba9eb55a28e1c94776f8b735

    opened by xianyongjian080402 2
  • 生成的.egg放在PyStand-py38-pyqt5文件下了,也添加了路径,删除python的script文件,报错如何更改?

    生成的.egg放在PyStand-py38-pyqt5文件下了,也添加了路径,删除python的script文件,报错如何更改?

    源文件拷贝到script文件中没有问题。生成egg文件有问题了。 setup.py脚本

    from setuptools import setup, find_packages
    
    setup(
        name="script",
        version="0.1",
        packages=find_packages()
    )
    
    

    生成的文件名为script-0.1-py3.8.egg,拷贝到PyStand-py38-pyqt5中,显示

    D:\aaa\PyStand-py38-pyqt5>Traceback (most recent call last):
      File "<string>", line 29, in <module>
      File "<string>", line 5, in <module>
    ModuleNotFoundError: No module named 'main'
    
    opened by wangwenqiangGitHub 11
Releases(1.0.6)
Owner
Linwei
Coding since 1991
Linwei
A standalone and lightweight C library

Klib: a Generic Library in C Overview Klib is a standalone and lightweight C library distributed under MIT/X11 license. Most components are independen

Attractive Chaos 3.7k Jan 8, 2023
MojoSetup is a standalone installer for Linux

MojoSetup is a standalone installer for Linux, designed to help third-party developers that need to ship software outside of traditional package management infrastructure.

Ryan C. Gordon 38 Dec 10, 2022
Standalone MinHook wrapper for Golang.

Standalone version of GoMinHook! Credit to https://github.com/NaniteFactory/gominhook and https://github.com/TsudaKageyu/minhook as almost all of the

null 3 Jun 4, 2022
Node running standalone on PC, with interface - self-containing all dependencies

GMD Node Windows Application It is the GMD Node App for Windows packaged in a simple "one-click" installer containing all necessary dependencies. We a

Geoma COOP 3 Sep 25, 2022
ZSV/lib: a fast CSV parsing library and standalone utility

Please note: this code is still alpha / pre-production. Everything here should be considered preliminary. If you like ZSVlib, please give it a star! Z

null 96 Dec 30, 2022
Mystikos is a set of tools for running applications in a hardware trusted execution environment (TEE)

Mystikos is a set of tools for running applications in a hardware trusted execution environment (TEE). The current release supports Intel ® SGX while other TEEs may be supported in future releases. Linux is also a supported target, though only suitable for testing purposes as it provides no additional protection.

null 116 Dec 14, 2022
A programming environment for Lua for the Raspberry Pi Pico microcontroller

picolua A programming environment for Lua for the Raspberry Pi Pico microcontroller. Version 0.3, April 2021 What is this? picolua is a proof-of-conce

Kevin Boone 65 Jan 8, 2023
Tightly coupled GNSS-Visual-Inertial system for locally smooth and globally consistent state estimation in complex environment.

GVINS GVINS: Tightly Coupled GNSS-Visual-Inertial Fusion for Smooth and Consistent State Estimation. paper link Authors: Shaozu CAO, Xiuyuan LU and Sh

HKUST Aerial Robotics Group 587 Dec 30, 2022
A basic system that waters a plant according to the heat index of the environment.

Basic-Plant-Watering-System ?? This is a basic plant watering system created on Arduino Uno using DHT11 Temperature and Humidity sensor. DHT Sensor Li

Aydanur Akça 5 Apr 11, 2021
foxBMS is a free, open and flexible development environment to design battery management systems.

foxBMS is a free, open and flexible development environment to design battery management systems. It is the first modular open source BMS development platform.

The foxBMS Team 100 Jan 10, 2023
A ROS based Open Source Simulation Environment for Robotics Beginners

A ROS based Open Source Simulation Environment for Robotics Beginners

Sulegeyixiu 131 Jan 5, 2023
Cobalt Strike Beacon Object File (BOF) that uses handwritten shellcode to return the process Environment strings without touching any DLL's.

Cobalt Strike "Where Am I?" Beacon Object File Cobalt Strike Beacon Object File (BOF) that uses handwritten shellcode to return the process Environmen

Bobby Cooke 92 Nov 30, 2022
X-CUBE-AZRTOS-F4 (Azure RTOS Software Expansion for STM32Cube) provides a full integration of Microsoft Azure RTOS in the STM32Cube environment for the STM32F4 series of microcontrollers.

X-CUBE-AZRTOS-F4 Azure RTOS Software Expansion for STM32Cube With Azure RTOS complementing the extensive STM32Cube ecosystem providing free developmen

STMicroelectronics 28 Dec 7, 2022
A C++ bare metal environment for Raspberry Pi with USB (32 and 64 bit)

A C++ bare metal environment for Raspberry Pi with USB (32 and 64 bit)

Rene Stange 1.5k Jan 2, 2023
This project aims to bring back a productive working environment on Windows 11

This project aims to bring back a productive working environment on Windows 11

Valentin-Gabriel Radu 10.4k Dec 30, 2022
A proof-of-concept port of uxn to the STM32duino environment

ArdUxno-demo A quick-and-dirty proof-of-concept port of Devine Lu Linvega's amazing Uxn virtual stack machine to an STM32 microcontroller. uxn.c and u

Cass Smith 11 Nov 9, 2022
AI Powered Hacking Environment, A Software For Hackers, Social Engineers, Penetration Testers.

Digital Eagle (Digle) (Note: This Project isn't Finished Yet, it's Under Development, Some Tools will not work) Hell0 W0rld This Project Focus Firstly

Mahmoud Osman (MLT) 4 Oct 22, 2022
Environment Tracking for Smart Farm.

IOT-EnvironmentTracking This is my school project about IOT Fundamentals. Components included: • ESP32 DevKitC • DHT-11 • PIR HC-SR501 • LCD 1602 I2C

Kent Khang 1 Dec 5, 2022
X-CUBE-AZRTOS-F7 (Azure RTOS Software Expansion for STM32Cube) provides a full integration of Microsoft Azure RTOS in the STM32Cube environment for the STM32F7 series of microcontrollers.

X-CUBE-AZRTOS-F7 Azure RTOS Software Expansion for STM32Cube With Azure RTOS complementing the extensive STM32Cube ecosystem providing free developmen

STMicroelectronics 7 Nov 17, 2022