属性错误:模块“importlib”没有“util”属性。

98

我刚从Fedora 32升级到Fedora 33(附带Python 3.9)。从那时起,gcloud命令停止工作:

[guy@Gandalf32 ~]$ gcloud 
Error processing line 3 of /home/guy/.local/lib/python3.9/site-packages/XStatic-1.0.2-py3.9-nspkg.pth:

  Traceback (most recent call last):
    File "/usr/lib64/python3.9/site.py", line 169, in addpackage
      exec(line)
    File "<string>", line 1, in <module>
    File "<frozen importlib._bootstrap>", line 562, in module_from_spec
  AttributeError: 'NoneType' object has no attribute 'loader'

Remainder of file ignored
Traceback (most recent call last):
  File "/usr/lib64/google-cloud-sdk/lib/gcloud.py", line 104, in <module>
    main()
  File "/usr/lib64/google-cloud-sdk/lib/gcloud.py", line 62, in main
    from googlecloudsdk.core.util import encoding
  File "/usr/lib64/google-cloud-sdk/lib/googlecloudsdk/__init__.py", line 23, in <module>
    from googlecloudsdk.core.util import importing
  File "/usr/lib64/google-cloud-sdk/lib/googlecloudsdk/core/util/importing.py", line 23, in <module>
    import imp
  File "/usr/lib64/python3.9/imp.py", line 23, in <module>
    from importlib import util
  File "/usr/lib64/python3.9/importlib/util.py", line 2, in <module>
    from . import abc
  File "/usr/lib64/python3.9/importlib/abc.py", line 17, in <module>
    from typing import Protocol, runtime_checkable
  File "/usr/lib64/python3.9/typing.py", line 26, in <module>
    import re as stdlib_re  # Avoid confusion with the re we export.
  File "/usr/lib64/python3.9/re.py", line 124, in <module>
    import enum
  File "/usr/lib64/google-cloud-sdk/lib/third_party/enum/__init__.py", line 26, in <module>
    spec = importlib.util.find_spec('enum')
AttributeError: module 'importlib' has no attribute 'util'
6个回答

119

GCP支持团队的更新

GCP支持提到,新版本318.0.02020.11.10发布,应该支持Python 3.9。

我将我的gcloud SDK更新到318.0.0,现在似乎支持Python 3.9.0了。

要解决此问题,请运行

gcloud components update

Fedora 33包括Python 2.7,如果要强制GCloud SDK使用它,请设置此环境变量。

export CLOUDSDK_PYTHON=python2

你可以将此导出命令添加到你的~/.bash_profile

Python 3.9非常新,预计Gcloud SDK 不支持3.9,它被编写成兼容2.7.x和3.6-3.8(3.8可能会引起一些兼容问题我建议使用3.7)

作为解决方法,配置Python 3.8或3.7(这些版本适用于Gcloud和大多数Linux发行版)为系统范围内的解释器,并尝试使用gcloud命令。


有点奇怪,python38Fedora 32Fedora 33 上不可用。但是 python37 似乎在两个版本上都可用。 - rickhg12hs
3
Python38现在也可以在Fedora上使用了。 - Frederik Baetens
1
啊,Fedora 32 上的默认 python 是指向当前版本为 python v 3.8python3 的链接。 - rickhg12hs
1
我已向上游提交了一个错误报告:https://issuetracker.google.com/issues/172647331 - tsuna
1
对于Debian或其他拥有多个Python版本的发行版:CLOUDSDK_PYTHON=python3.8 gcloud components update - V13
您可以将CLOUDSDK_PYTHON配置为Python二进制文件的绝对路径。我使用Homebrew安装了Python,因此在OSX上,我需要执行以下操作:CLOUDSDK_PYTHON=/usr/local/opt/python@3.8/bin/python3 gcloud components update - Michiel Bakker

102

对于 macOS/Homebrew:

brew install python@3.8
export CLOUDSDK_PYTHON=python3.8
ln -s /usr/local/Cellar/python@3.8/*/bin/python3.8 /usr/local/bin/python3.8

gcloud components update

# the issue is now resolved and you can return to python 3.9
unset CLOUDSDK_PYTHON

3
我还需要在/usr/local/bin创建一个指向Python 3.8的符号链接:/usr/local/bin/python3.8 -> ../Cellar/python@3.8/3.8.6_2/bin/python3.8 - Raphaël Lemaire
5
我原本只安装了Python@3.9,所以我需要按上面的方法安装3.8版本,然后像Raphaël建议的一样进行关联。具体操作为:brew unlink python@3.9,然后执行brew link python@3.8 - Peter W
就我个人而言,在其他地方我不想停止使用 python@3.9;我只希望在 gcloud CLI 中冻结到 3.8。 - Dr Nic
谢谢 @RaphaëlLemaire,我已经在解决方案中加入了一行代码。 - Dr Nic
12
您也可以执行CLOUDSDK_PYTHON=python2 gcloud components update - forresthopkinsa
由于某些原因,我的 brew 安装在 /opt 中,我需要使用 sudo 在根级别执行命令,因此我不得不将第三行更改为 sudo ln -s /opt/homebrew/Cellar/python@3.8/*/bin/python3.8 /usr/local/bin/python3.8 - Chris Redford

19

适用于 Mac OS 用户

首先,您应该运行 brew update

如果您遇到此错误:

Error: homebrew-core is a shallow clone. To `brew update` first run:
  git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core. We don't do this for you automatically to avoid
repeatedly performing an expensive unshallow operation in CI systems (which
should instead be fixed to not use shallow clones). Sorry for the inconvenience!

运行以下命令:
git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow
git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask" fetch --unshallow

现在,更新 Python 3.8:brew upgrade python@3.8 将 Python 3.8 添加到 PATH:export PATH="/usr/local/opt/python@3.8/bin:$PATH" 在 Cloud SDK 中使用 Python 3.8:export CLOUDSDK_PYTHON=python3.8 现在,您可以更新 gcloud 组件:gcloud components update

这个方法可行,即使我使用的是Python 3.9版本,但我仍需要使用3.8版本来运行上述命令。 - ziggrat

13

我在升级 brew 后出现了这个问题。使用 python 3.8 版本即可解决。

你需要将 python3.8 添加至 shell 路径中。执行以下命令即可解决问题。

export PATH="/usr/local/opt/python@3.8/bin:$PATH"
alias python=/usr/local/opt/python@3.8/bin/python3

谢谢!


谢谢,这个在之前的答案都不行时,在我的OSX上有效了。 - Richard

6

如果您不想使用Python 2,可以在Fedora 33上使用Python 3.8,直到SDK开始支持Python 3.9。

安装Python 3.8的方法如下:

sudo dnf install python3.8

您可以通过运行以下命令来验证您的安装:

python3.8 --version

然后将其设置为Google Cloud SDK解释器:

export CLOUDSDK_PYTHON=python3.8

完成此操作后,SDK应该能够正常工作。


Python 3.8与Gcloud SDK存在一些问题,请使用3.7或2.7版本,这些版本与Gcloud SDK更兼容。 - Jan Hernandez

-1

gcloud 将于11月10日更新到318版本,并将修复此问题。


禁用报告对我没有解决问题:gcloud config set disable_usage_reporting true CLOUDSDK_PYTHON=python3.9 gcloud config config-helper --format=json AttributeError: module 'importlib' has no attribute 'util' - tsuna

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