简体   繁体   English

从 Pyinstaller 运行外部 Python 脚本

[英]Running external Python script from Pyinstaller

I write an application using Pyinstaller which installs Python on the target machine and some packages.我使用 Pyinstaller 编写了一个应用程序,它在目标机器和一些包上安装了 Python。 At the end of the application I am supposed to spawn a Python script and I use subprocess.Popen() .在应用程序结束时,我应该生成一个 Python 脚本并使用subprocess.Popen() However, it appears that script cannot execute it as it appears to still use the application's (pyinstaller's) runtime environment.但是,脚本似乎无法执行它,因为它似乎仍在使用应用程序(pyinstaller 的)运行时环境。 However, running the resulting script stand alone after the installation works.但是,在安装工作后独立运行生成的脚本。

Is there a way to spawn a process outside the pyinstaller's context?有没有办法在 pyinstaller 的上下文之外生成进程?

Just sharing some approaches I made for this problem (at least for my use case).只是分享我为这个问题所做的一些方法(至少对于我的用例)。

The first approach would be in the resulting script that is spawned during installation, I explicitly remove the first entry in the PYTHONPATH if it is empty (ie the current directory).第一种方法是在安装期间生成的结果脚本中,如果PYTHONPATH中的第一个条目为空(即当前目录),我会明确删除它。 This ensures that the necessary Python libraries that are outside the current context (the PyInstaller bootloader) are loaded first.这确保首先加载当前上下文之外的必要 Python 库(PyInstaller 引导加载程序)。 ie IE

if not sys.path[0]:
    sys.path.pop(0)

For the second approach, there are instances that LD_LIBRARY_PATH environment variable refers to the directory of the extracted installer.对于第二种方法,有些情况下LD_LIBRARY_PATH环境变量指的是提取的安装程序的目录。 Usually occurring in Linux, I would send a modified environment variable set to the subprocess.Popen() where LD_LIBRARY_PATH is removed.通常发生在 Linux 中,我会将修改后的环境变量集发送到subprocess.Popen() ,其中LD_LIBRARY_PATH已被删除。

pip_env = os.environ
if 'LD_LIBRARY_PATH' in pip_env:
    pip_env.pop('LD_LIBRARY_PATH')
subprocess.call("<command here>", shell=True, env=pip_env)

Beware of using the above approaches, though.但是,请注意使用上述方法。 Although it worked on my use case, it may cause unexpected issues for other use cases.虽然它适用于我的用例,但它可能会导致其他用例出现意外问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM