Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'json' has no attribute 'JSONEncoder' #19691

Closed
JoCo356 opened this issue Sep 25, 2021 · 11 comments
Closed

AttributeError: module 'json' has no attribute 'JSONEncoder' #19691

JoCo356 opened this issue Sep 25, 2021 · 11 comments
Assignees
Labels
Core CLI core infrastructure customer-reported Issues that are reported by GitHub users external to the Azure organization.
Milestone

Comments

@JoCo356
Copy link

JoCo356 commented Sep 25, 2021

Describe the bug
I can't run "az login" , "az versions" or "az upgrade", it shows up an AttributeError.

Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/Cellar/[email protected]/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/azure/cli/__main__.py", line 14, in <module>
    import azure.cli.core.telemetry as telemetry
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/azure/cli/core/__init__.py", line 13, in <module>
    from knack.cli import CLI
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/knack/__init__.py", line 8, in <module>
    from knack.cli import CLI
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/knack/cli.py", line 12, in <module>
    from .output import OutputProducer
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/knack/output.py", line 25, in <module>
    class _ComplexEncoder(json.JSONEncoder):
AttributeError: module 'json' has no attribute 'JSONEncoder'

To Reproduce
Run "az login" or "az versions" on the terminal.

Expected behavior
I expected that when I runned az login an isolated window could open on the internet logging in.

Environment summary
I am using a macOS Catalina version 10.15.7 and I use .zshrc. I am trying to run "az login" in a pyenv which uses python 3.7.5.

Additional context
When I run "ls -l /System/Library/Frameworks/Python.framework/Versions" it gives me:

total 0
lrwxr-xr-x   1 root  wheel    3 Jul 20  2020 2.3 -> 2.7
lrwxr-xr-x   1 root  wheel    3 Jul 20  2020 2.5 -> 2.7
lrwxr-xr-x   1 root  wheel    3 Jul 20  2020 2.6 -> 2.7
drwxr-xr-x  11 root  wheel  352 Sep 12 16:18 2.7
lrwxr-xr-x   1 root  wheel    3 Jul 20  2020 Current -> 2.7

I already run "brew update && brew install python3 && brew upgrade python3" and "brew link --overwrite python3" and relinked again. And I am aware of this webpage https://docs.microsoft.com/pt-pt/cli/azure/install-azure-cli-macos?view=azure-cli-latest. I also run "brew upgrade" because it asked to do that after running "brew update", I had nghttp2 and openjdk outdated. and "brew reinstall azure-cli". When I run "brew update && brew install python3 && brew upgrade python3" it says:

Already up-to-date.
Warning: [email protected] 3.9.7 is already installed and up-to-date.
To reinstall 3.9.7, run:
  brew reinstall [email protected]
Warning: python3 3.9.7 already installed

And when "brew link --overwrite python3":

Warning: Already linked: /usr/local/Cellar/[email protected]/3.9.7
To relink, run:
  brew unlink [email protected] && brew link [email protected]

Tried the relink but didn't worked.

Also uninstalled with command "brew uninstall azure-cli" and re-installed with "brew update && brew install azure-cli", at my pyenv. Didn't work.

Hope you can help me.

@ghost ghost added needs-triage This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Sep 25, 2021
@yonzhan yonzhan added the Core CLI core infrastructure label Sep 25, 2021
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Sep 25, 2021
@yonzhan yonzhan removed the question The issue doesn't require a change to the product in order to be resolved. Most issues start as that label Sep 25, 2021
@yonzhan yonzhan added this to the Backlog milestone Sep 25, 2021
@yonzhan
Copy link
Collaborator

yonzhan commented Sep 25, 2021

@jiasli for awareness

@jiasli
Copy link
Member

jiasli commented Sep 26, 2021

Azure CLI uses the python installed by brew, instead of the one installed by pyenv.

Could you check the version of /usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python (#19027 (comment)):

# You may need to replace version 2.28.0 with the one you are using in the future
ls -l /usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python
/usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python -V

Also, can JSONEncoder be imported?

/usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python -c "from json import JSONEncoder"

@JoCo356
Copy link
Author

JoCo356 commented Sep 26, 2021

Thanks for helping, @jiasli and @yonzhan.

ls -l /usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python
lrwxr-xr-x  1 joco  staff  84 Sep  3 06:01 /usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9
/usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python -V
Python 3.9.7
 /usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python -c "from json import JSONEncoder"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'JSONEncoder' from 'json' (unknown location)

I don't understand

You may need to replace version 2.28.0 with the one you are using in the future

Should I reinstall python 3.9.7. at /usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python directory ? But it was installed by azure-cli, right? When I did:
brew update && brew install azure-cli

This could be of use: https://docs.brew.sh/Homebrew-and-Python

@jiasli
Copy link
Member

jiasli commented Sep 27, 2021

You may need to replace version 2.28.0 with the one you are using in the future

The current Azure CLI is 2.28.0. I am only saying in the future you may need to replace the version number with 2.29.0, 2.30.0, etc. 😄

`/usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python -c "from json import JSONEncoder"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'JSONEncoder' from 'json' (unknown location)

/usr/local/Cellar/azure-cli/2.28.0/libexec/bin/python points to /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9. As you cannot import JSON, that means the Python installation is broken.

What is the result of

/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -c "from json import JSONEncoder"

Could you reinstall Python and see if JSONEncoder can be imported?

brew reinstall [email protected]

@JoCo356
Copy link
Author

JoCo356 commented Sep 27, 2021

brew reinstall [email protected]
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.9/manifests/3.9.7
Already downloaded: /Users/joco/Library/Caches/Homebrew/downloads/4f570a18a1f2293b20b50278454402c7503a213c057fd9f8cae6db39a5add57b--python@3.9-3.9.7.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.9/blobs/sha256:304126999e2d730bc76d13584f47326a2e8626a7f0b36
Already downloaded: /Users/joco/Library/Caches/Homebrew/downloads/0c1c55f24906c27b273e5190f703730e61745e5ff76eef288460f0f12dc11487--python@3.9--3.9.7.catalina.bottle.tar.gz
==> Reinstalling [email protected]
==> Pouring [email protected]
==> /usr/local/Cellar/[email protected]/3.9.7/bin/python3 -m ensurepip
==> /usr/local/Cellar/[email protected]/3.9.7/bin/python3 -m pip install -v --no-deps --no-index --upgrade --isolated --targe
🍺  /usr/local/Cellar/[email protected]/3.9.7: 3,080 files, 54.7MB

Still didn't worked:

/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -c "from json import JSONEncoder"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'JSONEncoder' from 'json' (unknown location)
az login
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/Cellar/[email protected]/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/azure/cli/__main__.py", line 14, in <module>
    import azure.cli.core.telemetry as telemetry
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/azure/cli/core/__init__.py", line 13, in <module>
    from knack.cli import CLI
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/knack/__init__.py", line 8, in <module>
    from knack.cli import CLI
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/knack/cli.py", line 12, in <module>
    from .output import OutputProducer
  File "/usr/local/Cellar/azure-cli/2.28.0/libexec/lib/python3.9/site-packages/knack/output.py", line 25, in <module>
    class _ComplexEncoder(json.JSONEncoder):
AttributeError: module 'json' has no attribute 'JSONEncoder'

Any more ideas? I have no clue. Maybe it's reinstalling on a different directory, but, as you said, user/local/cellar/azure-cli/2.28.0/libexec/bin/python is pointing to user/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9. Again, thanks for the help @jiasli .

@JoCo356
Copy link
Author

JoCo356 commented Sep 27, 2021

Okay one information, the python3.9. comes from my operating system.

@jiasli
Copy link
Member

jiasli commented Sep 28, 2021

@JoCo356, apparently the Python installation is broken:

/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -c "from json import JSONEncoder"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'JSONEncoder' from 'json' (unknown location)

I am not really sure why it fails. Perhaps you may reach out to Homebrew? https://github.com/Homebrew/discussions/discussions

@JoCo356
Copy link
Author

JoCo356 commented Oct 5, 2021

Hello, first I had this:

joco@Jo-MBP ~ % python3
Python 3.9.7 (default, Sep  3 2021, 12:45:31)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from json import JSONEncoder
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'JSONEncoder' from 'json' (unknown location)
>>> exit()

Then I did >> sudo rm -rf /usr/local/bin/python3
I run brew doctor to find symlinks that needed to delete. (https://apple.stackexchange.com/questions/284824/remove-and-reinstall-python-on-mac-can-i-trust-these-old-references) None was found.
I wanted to reinstall with homebrew:

joco@Jo-MBP ~ % brew install python3
Warning: [email protected] 3.9.7 is already installed and up-to-date.
To reinstall 3.9.7, run:
  brew reinstall [email protected]

Now I have:

joco@Jo-MBP ~ % python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from json import JSONEncoder
>>> exit()

And

joanacosta@Joanas-MBP ~ % azure login
zsh: command not found: azure

Hope you can help me, I am not an expert on this, but I want to be able to solve my problem. If you still think it is better to reach out homebrew, okay.

@jiasli
Copy link
Member

jiasli commented Oct 8, 2021

The command name of Azure CLI is az, not azure. Does az work now?

@JoCo356
Copy link
Author

JoCo356 commented Oct 12, 2021

Yes, now az login works!

@JoCo356
Copy link
Author

JoCo356 commented Oct 12, 2021

Gonna mark this issue as done. :)

@JoCo356 JoCo356 closed this as completed Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core CLI core infrastructure customer-reported Issues that are reported by GitHub users external to the Azure organization.
Projects
None yet
Development

No branches or pull requests

3 participants