Python 如何从脚本而不是终端使用PyInstaller?

Python 如何从脚本而不是终端使用PyInstaller?,python,pyinstaller,Python,Pyinstaller,短版: subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py") 如何从Python脚本中而不是从终端使用PyInstaller 我需要在Python脚本中编写什么才能获得与在终端中编写此脚本相同的效果: >python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/ma

短版:

subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py")
如何从Python脚本中而不是从终端使用PyInstaller

我需要在Python脚本中编写什么才能获得与在终端中编写此脚本相同的效果:

>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py

长版本:

subprocess.call(r"python -m PyInstaller --noconsole --name WorkLogger F:\KivyApps\WorkLogger\main.py")
我使用的库需要使用PyInstaller来分发可执行文件。但是我必须运行PyInstaller一次,然后更改spec文件,然后通过PyInstaller运行spec文件

所以在终点站我会这样做:

>python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
运行完成后,我手动更改spec文件。然后我跑:

>python -m PyInstaller WorkLogger.spec
我写了一个脚本,通过运行

>change_spec.py
但我最终希望在一个Python脚本中完成所有这些。我希望能够键入如下内容:

>distribute_python_project.py ./Worklogger
#Psuedocode:
#python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
#Code from change_spec.py
#python -m PyInstaller WorkLogger.spec
这意味着我的Python脚本需要如下所示:

>distribute_python_project.py ./Worklogger
#Psuedocode:
#python -m PyInstaller --noconsole --name WorkLogger ../WorkLogger/main.py
#Code from change_spec.py
#python -m PyInstaller WorkLogger.spec

但我不知道如何从python脚本而不是从终端使用PyInstaller。这可能吗?(对于那些感兴趣的人,我使用的图书馆是Kivy)。

感谢员工和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")

如果需要,您甚至可以使用spec文件直接访问PyInstaller的模块。在本例中,它使用spec file、dist dir和build dir的不同位置

导入PyInstaller
#“dev\config”目录中的我的规范文件
workdir=os.getcwd()
fn_msi_spec=os.path.join(workdir,'main_msi.spec')
#定义“dev\dist”和“dev\build”目录
os.chdir(“…”)
devdir=os.getcwd()
distdir=os.path.join(devdir,'dist')
builddir=os.path.join(devdir'build')
#直接调用pyinstaller
PyInstaller.\uuuu main\uuuu.run(['--distpath',distdir'--workpath',builddir,fn\u msi\u spec])

贝尔尼III的回答是正确的,但不是直截了当的,我个人觉得有点困惑

这是官方文件的答案:

导入PyInstaller.\uuu main__
PyInstaller.\uuuuu main\uuuuuuu.run([
“my_script.py”,
“--onefile”,
“开着窗户”
])
相当于:

pyinstaller my_script.py--onefile--已打开窗口

您可以使用子流程来完成这项工作吗?您可以在python脚本中调用终端命令,就像我能够使用子流程运行命令一样。但现在我明白了,要运行命令,我必须在一个特定的文件夹中(终端必须用cd刻录到文件夹中)。你知道有什么方法可以通过子流程来实现吗?我想出来了。谢谢你的提示!使用这种方法,如果我想添加额外的搜索路径、二进制文件等,该命令会是什么样子?我知道这是docs中的示例,docs不在任何复杂命令中进行解释,所以我不确定该怎么办。基本上,命令行中的任何字符串都会在数组中创建一个条目:PyInstaller.\uuu main\uuuu.run(['some_script.py'、'-name'、'my_exe_name'、'-specpath'、'some_dir'、'-path'、'some_other_dir'、'-onefile'、'-windowed']这意味着:像平常一样编写完整的命令行,然后将其拆分为字符串,然后在数组中添加一个条目,每个字符串一个条目。注意:这是可行的,但它会影响您设置的任何记录器。例如,在PyInstaller之前进行日志记录。_umain_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu将打印两次。一次由您的记录器打印,一次由PyInstaller的记录器打印。不知道为什么。。。