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

No module named azure.cli.__main__; #19027

Closed
dimitrisfinas opened this issue Jul 28, 2021 · 33 comments · Fixed by #19663
Closed

No module named azure.cli.__main__; #19027

dimitrisfinas opened this issue Jul 28, 2021 · 33 comments · Fixed by #19663

Comments

@dimitrisfinas
Copy link

dimitrisfinas commented Jul 28, 2021

Describe the bug
After installing Azure CLI using homebrew on MacOS, trying first command az login, I got error:

/usr/local/Cellar/azure-cli/2.26.1/libexec/bin/python: No module named azure.cli.__main__; 'azure.cli' is a package and cannot be directly executed

To Reproduce
open MacOS terminal and run:
brew update && brew install azure-cli
az login

Expected behavior
get Azure CLI login instructions

Environment summary
MacOS Catalina 10.15.7

@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 Jul 28, 2021
@yonzhan yonzhan added the Account az login/account label Jul 28, 2021
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Jul 28, 2021
@yonzhan yonzhan removed customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 28, 2021
@yonzhan yonzhan added this to the Jul 2021 (2021-08-03) milestone Jul 28, 2021
@yonzhan
Copy link
Collaborator

yonzhan commented Jul 28, 2021

@jiasli for awareness

@smereczynski
Copy link

+1

Environment summary
MacOS Big Sur 11.5

@jiasli jiasli added Packaging/Homebrew and removed Account az login/account labels Jul 29, 2021
@smereczynski
Copy link

Just in case: the same after installing 2.27.0.

@dimitrisfinas
Copy link
Author

8 days and no reply at all, is there anyone looking at these problems / bugs?

@smereczynski
Copy link

8 days and no reply at all, is there anyone looking at these problems / bugs?

It was added to the milestone 7 days ago and labeled. I think someone can be investigating it ;)

@Volatus
Copy link
Contributor

Volatus commented Aug 5, 2021

+1

Environment summary
MacOS Big Sur 11.5

MacOS Bir Sur 1.15.1 here and no issues. Which version of Python is installed in your case? (3.9.6 for me)

@Volatus
Copy link
Contributor

Volatus commented Aug 5, 2021

Describe the bug
After installing Azure CLI using homebrew on MacOS, trying first command az login, I got error:
/usr/local/Cellar/azure-cli/2.26.1/libexec/bin/python: No module named azure.cli.main; 'azure.cli' is a package and cannot be directly executed

To Reproduce
open MacOS terminal and run:
brew update && brew install azure-cli
az login

Expected behavior
get Azure CLI login instructions

Environment summary
MacOS Catalina 10.15.7

Can you include this info:

  1. Brew version - brew -v
  2. Python version - python3 --version
  3. The result of which az
  4. echo $PATH

@jiasli
Copy link
Member

jiasli commented Aug 6, 2021

Could be related to #16417. I tested on Big Sur 11.5.1 with Intel chip but couldn't repro this issue. Are you on M1 chip?

Azure CLI uses Python 3.8 installed by Homebrew:

$ which az
/usr/local/bin/az

$ cat /usr/local/bin/az
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python -m azure.cli "$@"

$ ls -l /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python
lrwxr-xr-x  1 user1  admin  84 Jul 30 19:10 /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.8/bin/python3.8
  1. Could you check if /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python points to the correct location?
  2. Could you check if /usr/local/Cellar/azure-cli/2.27.0/libexec/lib/python3.8/site-packages/azure/cli/__main__.py is installed correctly?
    ls /usr/local/Cellar/azure-cli/2.27.0/libexec/lib/python3.8/site-packages/azure/cli/__main__.py
    

@dimitrisfinas
Copy link
Author

See below information asked:
~/Documents/POCs/LODH $ brew -v
Homebrew 3.2.5
Homebrew/homebrew-core (git revision 9adb96828d; last commit 2021-07-28)
Homebrew/homebrew-cask (git revision 3e251b323a; last commit 2021-07-28)
~/Documents/POCs/LODH $ python3 --version
Python 3.9.0
~/Documents/POCs/LODH $ which az
/usr/local/bin/az
~/Documents/POCs/LODH $ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

I am on an Intel MAC (not M1)

and last:
~/Documents/POCs/LODH $ cat /usr/local/bin/az
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.26.1/libexec/bin/python -m azure.cli "$@"

~/Documents/POCs/LODH $ ls -l /usr/local/Cellar/azure-cli/2.26.1/libexec/bin/python
lrwxr-xr-x 1 dimitris.finas admin 84 Jul 14 10:02 /usr/local/Cellar/azure-cli/2.26.1/libexec/bin/python -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.8/bin/python3.8

@dimitrisfinas
Copy link
Author

I have upgraded to 2.27 to be on latest release, but issue is the same

@Volatus
Copy link
Contributor

Volatus commented Aug 6, 2021

I have upgraded to 2.27 to be on latest release, but issue is the same

Can you try $(brew --prefix)/bin/az login?

@Volatus
Copy link
Contributor

Volatus commented Aug 6, 2021

Could be related to #16417. I tested on Big Sur 11.5.1 with Intel chip but couldn't repro this issue. Are you on M1 chip?

Azure CLI uses Python 3.8 installed by Homebrew:

$ which az
/usr/local/bin/az

$ cat /usr/local/bin/az
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python -m azure.cli "$@"

$ ls -l /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python
lrwxr-xr-x  1 user1  admin  84 Jul 30 19:10 /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.8/bin/python3.8
  1. Could you check if /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python points to the correct location?
  2. Could you check if /usr/local/Cellar/azure-cli/2.27.0/libexec/lib/python3.8/site-packages/azure/cli/__main__.py is installed correctly?
    ls /usr/local/Cellar/azure-cli/2.27.0/libexec/lib/python3.8/site-packages/azure/cli/__main__.py
    

I think the azcli is installed wrong in some way. I'm on M1 with Big Sur 1.15.1. Could be something to do with $PATH and where brew keeps the Cellars.

🚀 which az
/opt/homebrew/bin/az
🚀 az -v
azure-cli                         2.27.0

core                              2.27.0
telemetry                          1.0.6

Python location '/opt/homebrew/Cellar/azure-cli/2.27.0/libexec/bin/python'
Extensions directory '/Users/xs/.config/azure/cliextensions'

Python (Darwin) 3.8.11 (default, Jun 28 2021, 19:02:52)
[Clang 12.0.5 (clang-1205.0.22.9)]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.

Please let us know how we are doing: https://aka.ms/azureclihats
and let us know if you're interested in trying out our newest features: https://aka.ms/CLIUXstudy
🚀 brew --version
Homebrew 3.2.6
Homebrew/homebrew-core (git revision fe7dc97635; last commit 2021-08-04)
Homebrew/homebrew-cask (git revision fabd9b3dfa; last commit 2021-08-04)
🚀 python3 --version
Python 3.9.6
🚀 which python3
/opt/homebrew/bin/python3
🚀 echo $PATH
/opt/homebrew/opt/llvm/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/xs/go/bin

@smereczynski
Copy link

I'm on Intel and have this issue. I can help with tests if needed.

@Volatus
Copy link
Contributor

Volatus commented Aug 6, 2021

I'm on Intel and have this issue. I can help with tests if needed.

Can you see my comment just above and post the results of those commands here so we can investigate a bit?

@smereczynski
Copy link

🚀 which az
/usr/local/bin/az
🚀 az -v
/usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python: No module named azure.cli.main; 'azure.cli' is a package and cannot be directly executed

🚀 brew --version
Homebrew 3.2.6
Homebrew/homebrew-core (git revision 1975fb7765; last commit 2021-08-05)
Homebrew/homebrew-cask (git revision 9961c0f592; last commit 2021-08-05)
🚀 python3 --version
Python 3.8.11
🚀 which python3
/usr/local/opt/[email protected]/bin/python3
🚀 echo $PATH
/usr/local/opt/node@14/bin:/usr/local/opt/[email protected]/bin:/usr/local/opt/[email protected]/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands

@Volatus
Copy link
Contributor

Volatus commented Aug 6, 2021

🚀 which az
/usr/local/bin/az
🚀 az -v
/usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python: No module named azure.cli.main; 'azure.cli' is a package and cannot be directly executed

🚀 brew --version
Homebrew 3.2.6
Homebrew/homebrew-core (git revision 1975fb7765; last commit 2021-08-05)
Homebrew/homebrew-cask (git revision 9961c0f592; last commit 2021-08-05)
🚀 python3 --version
Python 3.8.11
🚀 which python3
/usr/local/opt/[email protected]/bin/python3
🚀 echo $PATH
/usr/local/opt/node@14/bin:/usr/local/opt/[email protected]/bin:/usr/local/opt/[email protected]/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands

What happens if you try $(brew --prefix)/bin/az -v?

@smereczynski
Copy link

$(brew --prefix)/bin/az -v

/usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python: No module named azure.cli.main; 'azure.cli' is a package and cannot be directly executed

@dimitrisfinas
Copy link
Author

dimitrisfinas commented Aug 6, 2021 via email

@mowtschan
Copy link

Guys, any updates? I have the same issue on my mac (macOS 11.5.2, 6-Core Intel Core i7)
Yesterday az just stopped working (but worked before!)

$ az -v
/usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python: No module named azure.cli.__main__; 'azure.cli' is a package and cannot be directly executed
$ brew --version
Homebrew 3.2.6
Homebrew/homebrew-core (git revision e266b2d3b9; last commit 2021-08-13)
Homebrew/homebrew-cask (git revision 159634a772; last commit 2021-08-13)
$ cat $(which az)
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python -m azure.cli "$@"
$ ls -la  /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python
lrwxr-xr-x  1 mowtschan  admin  84 30 Jul 13:10 /usr/local/Cellar/azure-cli/2.27.0/libexec/bin/python -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.8/bin/python3.8
$ /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.8/bin/python3.8 --version
Python 3.8.11

@mowtschan
Copy link

brew install azure-cli, installs now v2.27.1 and it works again for me!

@smereczynski
Copy link

brew install azure-cli, installs now v2.27.1 and it works again for me!

I can confirm. It works after upgrade to 2.27.1.

@jiasli
Copy link
Member

jiasli commented Aug 15, 2021

I am glad to know it works.

We bumped the underlying Python to 3.9 (#18224, #19222, Homebrew/homebrew-core#83088), though I don't exactly know why it fails previously on Python 3.8. Looks like something else installed by Homebrew conflicts with Azure CLI. We never see this problem in the tests of our release pipeline, which run on a clean MacOS 10.14.

@egilh
Copy link

egilh commented Aug 16, 2021

az cli no longer working on my machine, running macOS 11.5.1.
Started to fail after i ran brew update && brew upgrade
Tried to reinstall python3 and az cli with brew, still no luck.

➜  ~ az -v
/usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python: No module named azure.cli.__main__; 'azure.cli' is a package and cannot be directly executed
➜  ~ brew --version
Homebrew 3.2.6
Homebrew/homebrew-core (git revision c0d11ed420c; last commit 2021-08-16)
Homebrew/homebrew-cask (git revision 268e4412bd; last commit 2021-08-16)
➜  ~ cat $(which az)
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -m azure.cli "$@"
➜  ~ /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9 --version
Python 3.9.6 (default, Jun 29 2021, 05:25:02)
[Clang 12.0.5 (clang-1205.0.22.9)]

@srvrguy
Copy link

srvrguy commented Aug 17, 2021

I can confirm this issue still exists. I went the extra mile of removing all my installed brew formula (only keeping casks), verifying that all the formula were removed, and then reinstalling only azure-cli so that only that formula and the required dependencies exist. This is on an M1-based MBP on 11.5.

$ az
/opt/homebrew/Cellar/azure-cli/2.27.1/libexec/bin/python: No module named azure.cli.__main__; 'azure.cli' is a package and cannot be directly executed

$ brew -v
Homebrew 3.2.7
Homebrew/homebrew-core (git revision a11ae642bb; last commit 2021-08-17)
Homebrew/homebrew-cask (git revision fff08235c5; last commit 2021-08-18)

$ python3 --version
Python 3.9.6

$ which az
/opt/homebrew/bin/az

$ echo $PATH
/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands

@jiasli
Copy link
Member

jiasli commented Aug 18, 2021

Could you help verify if the installation of Azure CLI by Homebrew is correct by running:

$ which az
/usr/local/bin/az

$ cat $(which az)
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -m azure.cli "$@"

# Use python from above output to check 'sys.path'
$ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import sys; print(sys.path)"
['', '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages', '/usr/local/lib/python3.9/site-packages']

# Check 'azure' module
$ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import azure; print(azure.__path__)"
['/usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages/azure']

# List the path from above output
$ ls /usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages/azure
__init__.py
__pycache__
appconfiguration
batch
cli
common
core
cosmos
datalake
graphrbac
identity
keyvault
loganalytics
mgmt
multiapi
profiles
storage
synapse

# Check 'azure.cli' module
$ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import azure.cli; print(azure.cli.__path__)"
_NamespacePath(['/usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages/azure/cli'])

# List the path from above output
$ ls /usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages/azure/cli
__main__.py
__pycache__
command_modules
core
telemetry

# Check if there are old installations
$ ls --directory /usr/local/Cellar/azure-cli/*/libexec/lib/python*/site-packages/azure/cli

@egilh
Copy link

egilh commented Aug 18, 2021

I think I found my culprit:

$ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import azure.cli; print(azure.cli.__path__)"
['/usr/local/lib/python3.9/site-packages/azure/cli']

I did the following:
Uninstall all pip packages:

pip uninstall -y -r <(pip freeze)

Delete all pip cache:

rm -rf ~/Library/Caches/pip/*

Uninstall pip:

pip uninstall pip

Fix possible old package permissions:

sudo chown -R $USER /usr/local/bin/*

Reinstall python3.9 using brew:

brew reinstall python

Reinstall azure-cli:

brew reinstall azure-cli

Seems to solve path for the azure module:

➜  ~ which az
/usr/local/bin/az
➜  ~ cat $(which az)
#!/usr/bin/env bash
AZ_INSTALLER=HOMEBREW /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -m azure.cli "$@"
➜  ~ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import sys; print(sys.path)"
['', '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages', '/Users/ehaugli/Library/Python/3.9/lib/python/site-packages', '/usr/local/lib/python3.9/site-packages']
➜  ~ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import azure; print(azure.__path__)"
['/usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages/azure']
➜  ~ az --version
azure-cli                         2.27.1

core                              2.27.1
telemetry                          1.0.6

Python location '/usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python'
Extensions directory '/Users/ehaugli/.azure/cliextensions'

Python (Darwin) 3.9.6 (default, Jun 29 2021, 05:25:02)
[Clang 12.0.5 (clang-1205.0.22.9)]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.

This is probably not the solution for everyone (and probably not the best or easiest), but worked for me.

@egilh
Copy link

egilh commented Aug 18, 2021

Follow up from my last comment:
This all of sudden stopped working again

➜  test git:(master) ✗ az -v
/usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python: No module named azure.cli.__main__; 'azure.cli' is a package and cannot be directly executed

Checking path for the azure module:

➜  ~ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import azure.cli; print(azure.cli.__path__)"
_NamespacePath(['/usr/local/lib/python3.9/site-packages/azure/cli'])

This suddenly changed again.
All I have done post re-installing azure-cli as per my last post, is to install ansible and ansible-lint using pip, as well as installing the required packages for the Ansible collection, see (https://github.com/ansible-collections/azure/blob/dev/requirements-azure.txt) .
Tested this again, installing these requirements brakes the azure-cli installation:

Successfully installed PyJWT-1.7.1 adal-1.2.7 applicationinsights-0.11.10 argcomplete-1.12.3 azure-cli-core-2.11.1 azure-cli-nspkg-3.0.2 azure-cli-telemetry-1.0.6 azure-common-1.1.11 azure-core-1.17.0 azure-graphrbac-0.61.1 azure-keyvault-1.0.0a1 azure-mgmt-apimanagement-0.2.0 azure-mgmt-authorization-0.51.1 azure-mgmt-automation-0.1.1 azure-mgmt-batch-5.0.1 azure-mgmt-cdn-3.0.0 azure-mgmt-compute-10.0.0 azure-mgmt-containerinstance-1.4.0 azure-mgmt-containerregistry-2.0.0 azure-mgmt-containerservice-9.1.0 azure-mgmt-core-1.2.0 azure-mgmt-cosmosdb-0.5.2 azure-mgmt-datalake-nspkg-2.0.0 azure-mgmt-datalake-store-0.5.0 azure-mgmt-devtestlabs-3.0.0 azure-mgmt-dns-2.1.0 azure-mgmt-eventhub-2.0.0 azure-mgmt-hdinsight-0.1.0 azure-mgmt-iothub-0.7.0 azure-mgmt-keyvault-1.1.0 azure-mgmt-loganalytics-1.0.0 azure-mgmt-managedservices-1.0.0 azure-mgmt-managementgroups-0.2.0 azure-mgmt-marketplaceordering-0.1.0 azure-mgmt-monitor-0.5.2 azure-mgmt-network-12.0.0 azure-mgmt-notificationhubs-2.0.0 azure-mgmt-nspkg-2.0.0 azure-mgmt-privatedns-0.1.0 azure-mgmt-rdbms-1.9.0 azure-mgmt-recoveryservices-0.4.0 azure-mgmt-recoveryservicesbackup-0.6.0 azure-mgmt-redis-5.0.0 azure-mgmt-resource-10.2.0 azure-mgmt-search-3.0.0 azure-mgmt-servicebus-0.5.3 azure-mgmt-sql-0.10.0 azure-mgmt-storage-11.1.0 azure-mgmt-trafficmanager-0.50.0 azure-mgmt-web-0.41.0 azure-nspkg-2.0.0 azure-storage-0.35.1 bcrypt-3.2.0 certifi-2021.5.30 charset-normalizer-2.0.4 humanfriendly-8.2 idna-3.2 isodate-0.6.0 jmespath-0.10.0 knack-0.7.2 msal-1.0.0 msal-extensions-0.1.3 msrest-0.6.21 msrestazure-0.6.4 oauthlib-3.1.1 paramiko-2.7.2 pkginfo-1.7.1 portalocker-1.7.1 pynacl-1.4.0 pyopenssl-20.0.1 python-dateutil-2.8.2 requests-2.26.0 requests-oauthlib-1.3.0 six-1.16.0 tabulate-0.8.9 urllib3-1.26.6 xmltodict-0.12.0
➜  ansible-test git:(master) ✗ az -v
/usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python: No module named azure.cli.__main__; 'azure.cli' is a package and cannot be directly executed

Not a python-guy, but seems obvious that these packages install new modules that get in PATH and breaks the azure-cli installation from homebrew.

@srvrguy
Copy link

srvrguy commented Aug 18, 2021

I was starting to run through the new troubleshooting when I discovered the same thing as egilh. Once I got to the interactive import, I saw two paths, one to the homebrew install and one to a local site-packages (/opt/homebrew/lib/python3.9/site-packages/azure for me on am M1 machine). Once I purged those packages, the az command began to work again.

@jiasli
Copy link
Member

jiasli commented Aug 19, 2021

According to Homebrew/brew#1790, system site-packages (/usr/local/lib/python3.9/site-packages) shouldn't be included when executing /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python, but they are:

$ /usr/local/Cellar/azure-cli/2.27.1/libexec/bin/python -c "import sys; print(sys.path)"
[ ... '/usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages', '/usr/local/lib/python3.9/site-packages']

Checked the pyvenv.cfg and it does show include-system-site-packages = true:

$ cat /usr/local/Cellar/azure-cli/2.27.1/libexec/pyvenv.cfg
home = /usr/local/opt/[email protected]/bin
include-system-site-packages = true
version = 3.9.6

Python for Formula Authors also doesn't mention this behavior. I asked a question at Homebrew/discussions#2009.

@jiasli
Copy link
Member

jiasli commented Aug 19, 2021

Cause

As discussed in Homebrew/discussions#2009, this issue is caused by installing Ansible collection for Azure to the system site-packages with pip.

https://github.com/ansible-collections/azure/blob/dev/requirements-azure.txt requires:

azure-cli-core==2.11.1
azure-cli-nspkg==3.0.2

The installation of azure-cli-nspkg==3.0.2 (now deprecated) makes /usr/local/lib/python3.9/site-packages/azure/cli a regular package by installing an empty __init__.py (#13293 (comment)). Now Azure CLI is a namespace package (#14372).

After Homebrew/brew#11611, the virtual environment created by Homebrew virtualenv_create sets include-system-site-packages = true in pyvenv.cfg of the virtual environment (defined by PEP 405), breaking the import resolution as there are now 2 azure.clis appearing on sys.path:

  • /usr/local/Cellar/azure-cli/2.27.1/libexec/lib/python3.9/site-packages/azure/cli (new namespace package without __init__.py, installed by Homebrew)
  • /usr/local/lib/python3.9/site-packages/azure/cli (old regular packages with empty __init__.py, installed by pip, which takes precedence)

Solution

To avoid 2 azure.clis appearing on sys.path, please create a virtual environment to install Ansible collection for Azure.

If you has already installed Ansible collection for Azure to the system site-packages, uninstall all azure-related packages:

pip3 uninstall -r <(pip freeze | grep azure) --yes

@dimitrisfinas
Copy link
Author

brew install azure-cli, installs now v2.27.1 and it works again for me!

I can confirm. It works after upgrade to 2.27.1.

same for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants