在Mac上出现“-bash:gcloud:命令未找到”的错误提示

43

我正在按照Mac OS X快速入门的步骤安装云SDK。完全按照以下步骤进行:

1)python -V返回“Python 2.7.10”

2)下载并使用./install.sh提取SDK的64位版本,进入google-cloud-sdk返回...

Welcome to the Google Cloud SDK!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
to opt out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:

    gcloud config set disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)?  Y


Your current Cloud SDK version is: 170.0.1
The latest available version is: 170.0.1

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                  Components                                                 │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│     Status    │                         Name                         │            ID            │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go            │  97.7 MiB │
│ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                      │   4.0 MiB │
│ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                 │   3.5 MiB │
│ Not Installed │ Cloud Datalab Command Line Tool                      │ datalab                  │   < 1 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │  15.4 MiB │
│ Not Installed │ Cloud Datastore Emulator (Legacy)                    │ gcd-emulator             │  38.1 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  33.2 MiB │
│ Not Installed │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │  14.5 MiB │
│ Not Installed │ Google Container Local Builder                       │ container-builder-local  │   3.7 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │   2.2 MiB │
│ Not Installed │ gcloud Alpha Commands                                │ alpha                    │   < 1 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                     │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java          │ 128.1 MiB │
│ Not Installed │ gcloud app PHP Extensions (Mac OS X)                 │ app-engine-php-darwin    │  21.9 MiB │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python        │   6.5 MiB │
│ Not Installed │ kubectl                                              │ kubectl                  │  15.9 MiB │
│ Installed     │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Installed     │ Cloud SDK Core Libraries                             │ core                     │   6.7 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                   │   3.0 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
To install or remove components at your current SDK version [170.0.1], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [170.0.1], run:
  $ gcloud components update

==> Source [/Users/shaneoseasnain/Desktop/google-cloud-sdk/completion.bash.inc] in your profile to enable shell command completion for gcloud.
==> Source [/Users/shaneoseasnain/Desktop/google-cloud-sdk/path.bash.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH.

For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

3) 我已经重新启动了终端,并从google-cloud-sdk内部运行gcloud init。 返回"command not found"。 如果我运行ls,我会看到以下目录结构:

LICENSE               completion.zsh.inc      path.fish.inc
README                deb                     path.zsh.inc
RELEASE_NOTES         install.bat             platform
VERSION               install.sh              properties
bin                   lib                     rpm
completion.bash.inc   path.bash.inc

4) 我已经尝试在bin中运行gcloud init,但是遇到相同的问题,command not found

我从安装指南中看到的唯一其他建议是“在您的bash shell中启用命令完成以及/或启用使用报告”。我不确定如何做到这一点,但命令完成和报告似乎与此问题无关。虽然有许多关于command not found的文章,但它们涉及其他问题,例如在一段时间后gcloud停止工作或旧版本的问题。是否有人有关于如何使云SDK工作的提示?

谢谢

13个回答

103

我也遇到了同样的问题。这个问题可以通过以下简单的方法来解决:

brew install --cask google-cloud-sdk

在谷歌上搜索可以找到该方法。


11
对我来说没有修好。 - Christof Henkel
1
不知道是否影响了其他内容,但gcloud又可以使用了。 :) - Joakim M
1
这并没有解决可能是路径问题的问题,使用该命令可能会复制此问题。如果您在配置文件中添加了路径变量或者在运行此命令时注意到它被添加了,那么这将是有帮助的。 - thleo
3
在执行了 brew install 后,我发现 gcloud 的位置在这里:/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin - Mike Pennington
1
谢谢。这真是太遗憾了,这不在官方文档中。 - Itay Tur
显示剩余2条评论

32

对于MAC OS - 使用Homebrew

brew install --cask google-cloud-sdk

然后使用

 source "$(brew --prefix)/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc"
 source "$(brew --prefix)/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc"

当我运行source "$(brew --prefix)/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc"时,我发现以下问题: -bash: autoload: command not found -bash: zmodload: command not found -bash: ((: $+functions[compdef] : syntax error: operand expected (error token is "$+functions[compdef] ") -bash: autoload: command not found - nawaz anjum
@nawazanjum,你可能遇到了问题,因为上面的说明是针对zsh而不是bash的。你可以尝试在上面的路径中将zsh替换为bash。通过brew安装后,它会打印: source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc" - Giselle Serate

12

gcloud可执行文件位于google-cloud-sdk/bin中,要运行gcloud,您需要执行以下操作之一:

  1. Add this directory to your $PATH. In particular, adding the line source /Users/shaneoseasnain/Desktop/google-cloud-sdk/path.bash.inc to your .bash_profile file (more detail on what this file does and how to pick one) will do the trick.
  2. Specify the full path to the gcloud executable when you run it. In your case, this is /Users/shaneoseasnain/Desktop/google-cloud-sdk/bin/gcloud. So for example:

    $ /Users/shaneoseasnain/Desktop/google-cloud-sdk/bin/gcloud init
    

快速入门相关部分(强调添加):

运行安装脚本以将Cloud SDK工具添加到您的路径中,在bash shell中启用命令自动完成和/或启用使用报告:

至于为什么安装脚本无法正常工作,我无法说。请提交问题,我们会进行调查。


9

这里是macOS。我有过相同的问题,遇到以下错误:

gcloud: command not found

经过运行brew install --cask google-cloud-sdk命令后,我发现我需要手动安装它。

  • 首先,在google-cloud-sdk目录下找到install.sh脚本,并在终端中运行 .../google-cloud-sdk/install.sh命令。 对于我的情况, 该文件位于 /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk

  • 其次,在终端中运行/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/install.sh命令

  • 第三步,会提示你重新启动终端,因为你的bash文件或zsh文件已更新。

  • 第四步,在启动新终端后运行gcloud -v,测试gcloud-cli是否正常工作。


2
我只完成了第二步 /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/install.sh 并按照提示进行操作,它成功了。 - Kevin Danikowski

5

简而言之:

emacs -nw ~/.zshrc

在开头添加以下内容:

# The next line updates PATH for the Google Cloud SDK.
source '/home/lesaint/GOOGLE_CLOUD/google-cloud-sdk/path.zsh.inc'

# The next lines enables bash completion in Zsh for gcloud. 
autoload -U compinit compdef
compinit
source '/home/lesaint/GOOGLE_CLOUD/google-cloud-sdk/completion.zsh.inc'

以下文章提出的解决方案适合我: http://www.javatronic.fr/tips/2014/10/17/installing_google_cloud_sdk_on_ubuntu_with_oh-my-zsh.html 该文章介绍的方法对我有效。

非常感谢,这对我有用。我正在使用macOS Catalina操作系统。 - Tobi
我发现这个相关评论很有帮助。 - feihcsim

4
以下方法对我有效:
  • 我将安装程序的解压包移动到~目录。
  • 然后运行./google-cloud-sdk/install.sh
  • 再次重新启动终端,运行'gcloud init'即可。

1
忘记重新启动终端了。 - Wojtek

4
TL;DR - 跟随安装Cloud SDK中的交互式安装程序部分,SDK将正确安装。
然而,这里存在一个真正的问题,我认为这与缺失环境变量有关:
起初,我也遵循了Mac OS X快速入门以便为'Mac OS X(x86_64)平台'安装Google Cloud SDK。 这复制了Shane在上面提出的问题的准确结果。 以下是更多详细信息:
  • 在2017年款MacBook Pro上进行干净安装macOS Sierra 10.12.6(最新版本)。
  • Google Cloud SDK版本173.0.0。具体来说:google-cloud-sdk-173.0.0-darwin-x86_64.tar.gz,这是本文撰写时的最新版本,并且是快速入门指南所指向的版本。
我能够进一步确定Mac OS X快速入门的'开始之前'部分第4步中的问题:

配置Cloud SDK以供使用。 如果您无法让gcloud命令正常工作,请确保适当定义了$ PATH。 运行安装脚本以将Cloud SDK工具添加到您的路径中,在bash shell中启用命令完成和/或启用使用报告:

./google-cloud-sdk/install.sh

'install.sh'调用'install.py',在某个时候调用isatty()以查找安装过程是否从shell运行,这意味着它应该是交互式的并提示用户提供一些信息。由于某种原因,isatty()返回False而不是True! 这导致脚本非交互式地执行,并且 - 因为没有使用参数调用'install.sh' - 失败

2
有时,在安装完成后,您只需要重新启动终端。就这样 :)
请看这里:macOS快速入门

0

看起来你的google-cloud-sdk/bin没有出现在PATH中。

从以下位置复制你当前的PATH:

echo $PATH

现在将您的google-cloud-sdk/bin绝对路径添加到PATH中,不要忘记在添加新路径之前加上冒号“:”,应该像这样:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/Users/user/google-cloud-sdk/bin

0

对我来说,那就是解决方案:

mkdir -p $sdk_dir/.install/

简而言之:

Python库似乎通过搜索.install目录来定位sdk_root。请参见config.pyCLOUDSDK_STATE_DIR的定义。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接