将软件包添加到Windows的Python“嵌入式”安装中

fee*_*wet 9 python embed

文档中

嵌入式发行版是一个包含最小Python环境的ZIP文件。

听起来不错!Python的64位Windows嵌入式v3.6.5仅13MB。作为编译的替代方法,我想将一些python脚本以及在未安装Python的Win10机器上运行它们所需的最低要求一起压缩。

现在,我几乎总是需要导入其他软件包来提供功能。但是,如果我想与该嵌入式版本的Python一起发送python脚本,就无法确定该怎么做。例如,如果我的脚本使用numpy,如何在“嵌入”中包含该库?即,这样我就可以在任何Win10机器上解压缩单个部署文件并立即执行我的脚本吗?

(通常pip install numpy会创建一个超过50MB的Lib子目录!但是对于“嵌入式”部署,我不需要任何调试支持,也不需要大量文件中包含的任何其他内容。)

小智 17

还有一个类似的技巧。

  1. 嵌入式版本安装pip:

    卷曲-sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py

    python get-pip.py

  2. 通过添加编辑 pythonXX._pth

    Lib\站点包

  3. 使用pip安装其他包

  • 您可能希望对“类似技巧”的引用更加准确。您指的是另一个答案吗?然后你可以注意到“类似于xyz的答案的技巧” (3认同)

F.X*_*.X. 10

至少对于最新的 Python 版本(在 3.8 和 3.11 上测试),这似乎工作正常:

  • 官方网站下载您需要的Windows嵌入包,并解压。

    PS> Invoke-WebRequest -Uri https://www.python.org/ftp/python/3.11.1/python-3.11.1-embed-amd64.zip -OutFile python-3.11.1-embed-amd64.zip
    PS> Expand-Archive .\python-3.11.1-embed-amd64.zip
    PS> cd .\python-3.11.1-embed-amd64
    
    Run Code Online (Sandbox Code Playgroud)
  • 打开python3xx._pth与您的版本对应的文件(例如python311._pthPython 3.11),并确保以下导入行未注释。这会自动将pip 使用的站点目录添加到 Python 路径中:

    PS> Invoke-WebRequest -Uri https://www.python.org/ftp/python/3.11.1/python-3.11.1-embed-amd64.zip -OutFile python-3.11.1-embed-amd64.zip
    PS> Expand-Archive .\python-3.11.1-embed-amd64.zip
    PS> cd .\python-3.11.1-embed-amd64
    
    Run Code Online (Sandbox Code Playgroud)

    在 Powershell 中,可以通过运行以下命令来自动执行此操作:

    PS> Add-Content -Path .\python311._pth -Value 'import site'
    
    Run Code Online (Sandbox Code Playgroud)
  • 下载官方 pip bootstrap 脚本,例如使用 Powershell:

    PS> Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -OutFile get-pip.py
    
    Run Code Online (Sandbox Code Playgroud)
  • 运行下载的脚本(确保您在当前目录中使用正确的 Python 可执行文件.\):

    PS> .\python.exe get-pip.py
    Collecting pip
      Using cached pip-22.3.1-py3-none-any.whl (2.1 MB)
    Collecting setuptools
      Downloading setuptools-66.0.0-py3-none-any.whl (1.3 MB)
         ---------------------------------------- 1.3/1.3 MB 16.0 MB/s eta 0:00:00
    Collecting wheel
      Using cached wheel-0.38.4-py3-none-any.whl (36 kB)
    Installing collected packages: wheel, setuptools, pip
      WARNING: The script wheel.exe is installed in 'C:\Users\xxxx\Downloads\python-3.11.1-embed-amd64\Scripts' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
      WARNING: The scripts pip.exe, pip3.11.exe and pip3.exe are installed in 'C:\Users\xxxx\Downloads\python-3.11.1-embed-amd64\Scripts' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed pip-22.3.1 setuptools-66.0.0 wheel-0.38.4
    
    Run Code Online (Sandbox Code Playgroud)
  • 从目录运行 pip .\Scripts

    PS> .\Scripts\pip.exe list
    Package    Version
    ---------- -------
    pip        22.3.1
    setuptools 66.0.0
    wheel      0.38.4
    
    PS> .\Scripts\pip.exe install numpy
    Collecting numpy
      Downloading numpy-1.24.1-cp311-cp311-win_amd64.whl (14.8 MB)
         ---------------------------------------- 14.8/14.8 MB 11.9 MB/s eta 0:00:00
    Installing collected packages: numpy
      WARNING: The script f2py.exe is installed in 'C:\Users\xxxx\Downloads\python-3.11.1-embed-amd64\Scripts' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed numpy-1.24.1
    
    PS> .\python.exe -c 'import numpy; print(numpy.__version__)'
    1.24.1
    
    Run Code Online (Sandbox Code Playgroud)


小智 5

有一种方法可以扩展Python嵌入式安装。我设法创建了Flask-ready包,可以在目标计算机上解压缩并运行代码。关键是要安装EXACT 相同的 Python版本(标准完全成熟的蟒蛇)为目标的嵌入式小蟒蛇。不仅是x86版本,而且x86也必须匹配。

然后在普通python上从pip安装模块,转到NormalPython \ Lib \ site-packages,然后将安装后出现的所有新文件复制到EmbeddedPython \ Lib, 最后将Lib添加到Embedded python文件夹中的pythonXX._pth

对您的应用程序进行全面测试非常重要,以防您错过某些软件包。同样,这对于将.exe也添加到Scripts文件夹的程序包均无效。您仍然可以将exe复制到Script文件夹,然后将其添加到可以解决问题的路径。