如何从脚本而不是终端使用 PyInstaller?
How to use PyInstaller from script, not terminal?
短版:
如何从 Python 脚本而不是从终端使用 PyInstaller?
我需要在 Python 脚本中编写什么才能获得与在终端中编写的等效内容:
>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
长版:
我使用的库需要使用 PyInstaller 来分发可执行文件。但是我必须 运行 PyInstaller 一次,然后更改规范文件,然后通过 PyInstaller 运行 规范文件。
所以在终端中我会这样做:
>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
完成后 运行ning,我手动更改了 spec 文件。然后我运行:
>python -m PyInstaller WorkLogger.spec
我已经编写了一个脚本来为我完成体力劳动,作者是 运行ning
>change_spec.py
但我最终想在一个 Python 脚本中完成所有这些。我希望能够输入如下内容:
>distribute_python_project.py ./Worklogger
这意味着我的 Python 脚本需要如下所示:
#Psuedocode:
#python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
#Code from change_spec.py
#python -m PyInstaller WorkLogger.spec
但我不知道如何从 python 脚本而不是从终端使用 PyInstaller。这可能吗? (我使用的库是 Kivy,感兴趣的可以参考)。
感谢Employee和Canh!工作概念验证:
航站楼:
>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
Python 脚本:
subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py")
如果需要,您可以从特定工作目录启动子进程:
subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py", cwd=r"F:\KivyApps\WorkLogger_Dist")
如果需要,您甚至可以使用规范文件直接访问 PyInstaller 的模块。在此示例中,spec-file、dist-dir 和 build-dir 的位置不同。
import PyInstaller
# my spec file in "dev\config" dir
workdir = os.getcwd()
fn_msi_spec = os.path.join(workdir, 'main_msi.spec')
# define the "dev\dist" and "dev\build" dirs
os.chdir("..")
devdir = os.getcwd()
distdir = os.path.join(devdir, 'dist')
builddir = os.path.join(devdir, 'build')
# call pyinstaller directly
PyInstaller.__main__.run(['--distpath', distdir, '--workpath', builddir, fn_msi_spec])
Berniiii 的回答是正确的,但没有切中要害,我个人觉得有点混乱。
这是官方文档的回答:running-pyinstaller-from-python-code
import PyInstaller.__main__
PyInstaller.__main__.run([
'my_script.py',
'--onefile',
'--windowed'
])
相当于:
pyinstaller my_script.py --onefile --windowed
短版:
如何从 Python 脚本而不是从终端使用 PyInstaller?
我需要在 Python 脚本中编写什么才能获得与在终端中编写的等效内容:
>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
长版:
我使用的库需要使用 PyInstaller 来分发可执行文件。但是我必须 运行 PyInstaller 一次,然后更改规范文件,然后通过 PyInstaller 运行 规范文件。
所以在终端中我会这样做:
>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
完成后 运行ning,我手动更改了 spec 文件。然后我运行:
>python -m PyInstaller WorkLogger.spec
我已经编写了一个脚本来为我完成体力劳动,作者是 运行ning
>change_spec.py
但我最终想在一个 Python 脚本中完成所有这些。我希望能够输入如下内容:
>distribute_python_project.py ./Worklogger
这意味着我的 Python 脚本需要如下所示:
#Psuedocode:
#python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
#Code from change_spec.py
#python -m PyInstaller WorkLogger.spec
但我不知道如何从 python 脚本而不是从终端使用 PyInstaller。这可能吗? (我使用的库是 Kivy,感兴趣的可以参考)。
感谢Employee和Canh!工作概念验证:
航站楼:
>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
Python 脚本:
subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py")
如果需要,您可以从特定工作目录启动子进程:
subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py", cwd=r"F:\KivyApps\WorkLogger_Dist")
如果需要,您甚至可以使用规范文件直接访问 PyInstaller 的模块。在此示例中,spec-file、dist-dir 和 build-dir 的位置不同。
import PyInstaller
# my spec file in "dev\config" dir
workdir = os.getcwd()
fn_msi_spec = os.path.join(workdir, 'main_msi.spec')
# define the "dev\dist" and "dev\build" dirs
os.chdir("..")
devdir = os.getcwd()
distdir = os.path.join(devdir, 'dist')
builddir = os.path.join(devdir, 'build')
# call pyinstaller directly
PyInstaller.__main__.run(['--distpath', distdir, '--workpath', builddir, fn_msi_spec])
Berniiii 的回答是正确的,但没有切中要害,我个人觉得有点混乱。
这是官方文档的回答:running-pyinstaller-from-python-code
import PyInstaller.__main__
PyInstaller.__main__.run([
'my_script.py',
'--onefile',
'--windowed'
])
相当于:
pyinstaller my_script.py --onefile --windowed