AttributeError: module 'importlib' has no attribute 'util'
Asked Answered
T

6

98

I've just upgraded from Fedora 32 to Fedora 33 (which comes with Python 3.9). Since then gcloud command stopped working:

[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'
Thurstan answered 22/9, 2020 at 12:59 Comment(0)
A
119

Update from GCP support

GCP support mentioned that the new version 318.0.0 released on 2020.11.10 should support python 3.9

I updated my gcloud sdk to 318.0.0 and now looks like python 3.9.0 is supported.

To fix this issue run

gcloud components update

Fedora 33 includes python 2.7 and to force GCloud SDK to use it please set this environment variable

export CLOUDSDK_PYTHON=python2

You can add this export command to your ~/.bash_profile

Python 3.9 is very new and is expected that Gcloud SDK does not support 3.9, it is written to be compatible with 2.7.x & 3.6 - 3.8 (3.8 can cause some compat issues I recommend to use 3.7)

As a workaround, configure Python 3.8 or 3.7 (these versions work well for Gcloud and most of linux distros) as system wide interpreter and try to use gcloud commands.

Ayurveda answered 22/9, 2020 at 13:53 Comment(6)
Kinda weird that python38 isn't avalibale for Fedora 32 or Fedora 33. But python37 seems to available for both.Monster
Python38 is available now on fedora as well.Pipistrelle
Ahhh, the default python on Fedora 32 is a link to python3 which is currently python v 3.8.Monster
I filed a bug upstream: issuetracker.google.com/issues/172647331Decant
For Debian or other distros that have multiple python versions: CLOUDSDK_PYTHON=python3.8 gcloud components updateNoyes
You can configure CLOUDSDK_PYTHON as an absolute path to the python binary as well. I have python installed using homebrew, so on OSX I had to do: CLOUDSDK_PYTHON=/usr/local/opt/[email protected]/bin/python3 gcloud components updateMoney
H
102

For macOS/Homebrew:

brew install [email protected]
export CLOUDSDK_PYTHON=python3.8
ln -s /usr/local/Cellar/[email protected]/*/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
Halm answered 4/11, 2020 at 21:18 Comment(6)
I also had to create a symbolic link to python 3.8 in /usr/local/bin : /usr/local/bin/python3.8 -> ../Cellar/[email protected]/3.8.6_2/bin/python3.8Phenazine
I had [email protected] only and so I had to install 3.8 as above and then link it like @Raphaël recommended brew unlink [email protected] then brew link [email protected]Thormora
Personally, I wouldn't want to stop using [email protected] everywhere else; I would only want to freeze to 3.8 for gcloud CLI.Halm
Thanks @RaphaëlLemaire I've now added a line to the solution.Halm
You could also just do CLOUDSDK_PYTHON=python2 gcloud components updateHeadgear
For some reason my brew installations are in /opt and I need sudo to execute commands at the root level so I had to change the third line to sudo ln -s /opt/homebrew/Cellar/[email protected]/*/bin/python3.8 /usr/local/bin/python3.8Rhyner
N
19

For Mac OS Users

First of all you should run brew update.

If you have this error:

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!

Run next commands:

git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow
git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask" fetch --unshallow

Now,

Update python 3.8 brew upgrade [email protected]

Add python 3.8 to PATH export PATH="/usr/local/opt/[email protected]/bin:$PATH"

Use python 3.8 in Cloud SDK export CLOUDSDK_PYTHON=python3.8

Now, you can update gcloud components gcloud components update

Narrow answered 10/12, 2020 at 23:39 Comment(1)
This worked, even though i had python 3.9, i had to run the above commands with 3.8Davina
F
13

Happened to me after a brew upgrade. Works with python 3.8.

You need to make python3.8 into your shell path. I executed following lines and it worked

export PATH="/usr/local/opt/[email protected]/bin:$PATH"
alias python=/usr/local/opt/[email protected]/bin/python3

Thank you!

Filing answered 9/11, 2020 at 10:47 Comment(1)
Thanks, this worked for me on OSX when the answers above didn't.Sticky
Q
6

If you don't want to use Python 2, you can use Python 3.8 on Fedora 33 until the SDK starts supporting Python 3.9.

Install python 3.8 using :

sudo dnf install python3.8

You can verify your installation by running:

python3.8 --version

Then set it as the Google Cloud SDK interpreter:

export CLOUDSDK_PYTHON=python3.8

After this the SDK should work normally.

Quaver answered 28/10, 2020 at 10:47 Comment(1)
Python 3.8 has some issues with Gcloud SDK, please use 3.7 or 2.7 these versions are more compatible with Gcloud SDKAyurveda
M
-1

gcloud will update to version 318 on November 10 and will fix this issue.

Mosely answered 4/11, 2020 at 20:50 Comment(1)
Disabling reporting didn't fix the issue for me: gcloud config set disable_usage_reporting true CLOUDSDK_PYTHON=python3.9 gcloud config config-helper --format=json AttributeError: module 'importlib' has no attribute 'util'Decant

© 2022 - 2024 — McMap. All rights reserved.