【问题标题】:Errors converting .py to .exe with pyinstaller (pyinstaller: command not found, mac)使用 pyinstaller 将 .py 转换为 .exe 时出错(pyinstaller:找不到命令,mac)
【发布时间】:2021-01-12 21:11:22
【问题描述】:

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

pip install pyinstaller

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

pyinstaller 'filename.py'

得到了这个错误:

-bash: pyinstaller: 找不到命令

我该如何解决这个问题?

【问题讨论】:

  • 你试过python -m pyinstaller 'filename.py'吗?还是重启你的终端?
  • 是的,我试过了;它创建了两个文件夹,这两个文件夹都包含许多文件,但它们都不是我想要的 .exe。
  • @CatopietopaultiousCat pyinstaller 中的默认输出设置为--onedir 这意味着所有的包和依赖项以及您的 exe 都将以目录的形式输出,您的 exe 需要从该目录执行。除非您正在使用特定的东西,否则其他文件夹和文件并不那么重要。也请通过docs 了解更多。

标签: python macos pyinstaller exe


【解决方案1】:

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

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 并回车,您应该会得到这样的输出

我:~ user$ 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

【讨论】:

  • 命令完成后,您将在 .py 文件所在的目录中看到两个新文件夹“build”和“dist”。您的独立应用程序将位于 dist 文件夹中,如果对您有帮助,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2021-05-02
  • 2021-06-14
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
相关资源
最近更新 更多