[英]Distribute python code with python inside to avoid installation
I have built a bundled Python application that is a composition of an installed (ie " *.exe
", via pyinstaller
) and a code (ie " *.py
") part.我已经构建了一个捆绑的 Python 应用程序,它是一个已安装(即“
*.exe
”,通过pyinstaller
)和一个代码(即“ *.py
”)部分的组合。 This is due to a package that does not support installation, thus I have to call it directly from source.这是由于 package 不支持安装,因此我必须直接从源代码调用它。
I am able to distribute and run the application by including in the bundle my environment in .\venv
and running the code via .\venv\Scripts\python.exe my_script.py
, however it requires Python installed onto the machine, raising an Error otherwise我可以通过将我的环境包含在
.\venv
的捆绑包中并通过.\venv\Scripts\python.exe my_script.py
运行代码来分发和运行应用程序,但是它需要在机器上安装 Python,从而引发错误否则
No Python at 'C:\Python38\python.exe'
Is there a way to include Python38
folder in my package to allow anyone to start the application regardless of Python installed or not (and its version)?有没有办法在我的 package 中包含
Python38
文件夹,以允许任何人启动应用程序,无论是否安装了 Python(及其版本)? I've tried to:我试图:
Python38
folder within package and within .\venv\Scripts\
hoping that .\venv\Scripts\python.exe
was searching there before looking in C:\
-> not working.\venv\Scripts\
中复制Python38
文件夹,希望.\venv\Scripts\python.exe
在查看C:\
-> 之前在那里搜索Python38
in my package and force its copy in C:\
as the very first instruction -> it works, but it seems like cheating, and it may mess up things on recipient's computerPython38
并强制将其复制到C:\
作为第一条指令->它有效,但它似乎是作弊,它可能会在收件人的计算机上搞砸I managed to solve the problem in a somehow easy way:我设法以某种简单的方式解决了这个问题:
Python38
in my package together with my venv
(obviously, they cause the destination folder to grow in size, but it is not a big issue for my purposes)Python38
和我的venv
(显然,它们会导致目标文件夹的大小增加,但对我来说这不是一个大问题).\venv\pyvenv.cfg
changing from absolute path to Python on my computer to a relative path inside the destination folder.\venv\pyvenv.cfg
在我的计算机上从绝对路径更改为 Python 到目标文件夹内的相对路径Here a reference to the updated pyvenv.cfg
:这里是对更新后的
pyvenv.cfg
的引用:
home = .\Python38 <-- updated: it was "C:\Python38"
implementation = CPython
version_info = 3.8.5.final.0
virtualenv = 20.4.7
include-system-site-packages = false
base-prefix = .\Python38 <-- updated
base-exec-prefix = .\Python38 <-- updated
base-executable = .\Python38\python.exe <-- updated
FYI, here the code I used to automatically update the pyvenv.cfg
, that works with different python versions and paths:仅供参考,这里是我用来自动更新
pyvenv.cfg
的代码,它适用于不同的 python 版本和路径:
import os
import re
from configparser import ConfigParser
# Set original and updated config paths
venv_cfg = 'pyvenv.cfg'
in_venv_cfg = os.path.join('.', 'venv', venv_cfg)
out_venv_cfg = os.path.join('.', 'config', venv_cfg) # changing folder to avoid override of venv config
# Parse data in config
with open(in_venv_cfg, 'r') as f:
config_lines = f.readlines()
config = ConfigParser()
config.read_string('[cfg]\n' + ''.join(config_lines))
# Get python version and location
py_path = config.get('cfg', 'home')
py_dir, py_name = os.path.dirname(py_path), os.path.basename(py_path)
# Update paths to python and save venv configuration
new_config_lines = [re.sub(r'(=\s*)' + py_dir.replace("\\", r"\\") + f'({py_name})', r'\1.\\\2', line) for line in config_lines]
with open(out_venv_cfg, 'w') as f:
f.writelines(new_config_lines)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.