首页 > 解决方案 > 使用 pyinstaller 将 .py 转换为 .exe 时出错(pyinstaller:找不到命令,mac)

问题描述

我是 python 新手,正在尝试将我编写的简单 python 应用程序转换为 .exe。我安装了pyinstaller:

pip install pyinstaller

然后导航到我的文件正在使用的文件夹cd。这样做之后,我就跑了

pyinstaller 'filename.py'

并得到这个错误:

-bash:pyinstaller:找不到命令

我怎样才能解决这个问题?

标签: pythonmacospyinstallerexe

解决方案


“我从你的问题中推断出你试图在 Mac 上而不是在 Windows 上运行 pyinstaller。如果是,那么当我面临同样的情况时,这对我有用”</p>

First Remember : 
If you are working on Mac, then your standalone file generated will not have .exe extension, rather it would be .app

看起来您正确安装了 pyinstaller,但它不在您的 bash“PATH”中,bash 通常会在其中查找以运行任何已安装的程序。

要检查 pyinstaller 的安装位置,请在终端中运行此命令

    me:~ user$ sudo find pyinstaller ~/ | grep pyinstaller

 

这是我在我的 Mac 上看到的输出

    /Users/user//Library/Python/2.7/bin/pyinstaller

现在让我们像这样将它添加到 PATH

me:~ user$ sudo nano /etc/paths     (you can use vi instead of nano if you wish to)

这是我看到的示例输出(你的可能有点不同)

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

转到最后一个空行并在那里输入 ~/Library/Python/2.7/bin

your file should now look like this

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
~/Library/Python/2.7/bin

保存这个编辑过的文件并退出终端

  • 再次打开终端,这次终端将更新路径。只需键入 pyinstaller 并按回车键,您应该会得到这样的输出

我:〜用户$ pyinstaller

sage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
                   [--add-data <SRC;DEST or SRC:DEST>]
                   [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
                   [--hidden-import MODULENAME]
                   [--additional-hooks-dir HOOKSPATH]
                   [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
                   [--key KEY] [-d {all,imports,bootloader,noarchive}] [-s]
                   [--noupx] [--upx-exclude FILE] [-c] [-w]
                   [-i <FILE.ico or FILE.exe,ID or FILE.icns>]
                   [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
                   [--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
                   [--win-no-prefer-redirects]
                   [--osx-bundle-identifier BUNDLE_IDENTIFIER]
                   [--runtime-tmpdir PATH] [--bootloader-ignore-signals]
                   [--distpath DIR] [--workpath WORKPATH] [-y]
                   [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
                   scriptname [scriptname ...]
pyinstaller: error: too few arguments   
  • 现在 cd 进入包含程序的 .py 文件的文件夹并键入以下命令以获取独立的 mac 应用程序

只需运行此命令即可为 macOS 创建独立程序。它的名称将与您的 .py 文件名相同

me:~ user$ pyinstaller --windowed --onefile filename.py


if you want a different name for your standalone program then use following command



me:~ user$ pyinstaller --windowed --onefile --name myapp filename.py




if you want to add custom icon to your newly created standalone program then use following command 
    (before running this command make sure your icon flies in the same directory as your .py file)


me:~ user$ pyinstaller --windowed --onefile --icon "custom_icon.icns" --name myapp filename.py

如果您没有让上述任何内容为您工作,请尝试在这样的命令中提供 pyinstaller 的完整路径

me:~ user$ /Users/user//Library/Python/2.7/bin/pyinstaller --windowed --onefile filename.py

或者

me:~ user$ ~/Library/Python/2.7/bin/pyinstaller --windowed --onefile filename.py

推荐阅读