question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Command not working anymore within python script, but still working with cli

See original GitHub issue

Describe the bug

Command Name az network vnet list

Errors:

The credential data used by CLI has been expired because you might have changed or reset the password. Please clear browser's cookies and run 'az login'

To Reproduce:

Script to reproduce the behavior.

#!/usr/bin/python3
from azure.cli.core import get_default_cli

def azurer(args_str):
    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args)
    return cli.result.result

subscriptions = azurer("account list")
for s in subscriptions:
    vnets = azurer("network vnet list --subscription " + s['name'])
    if type(vnets) == dict:
        for v in vnets:
            print(v['name'])

Expected Behavior

Last time I used this script (some weeks ago) it was working fine. Also, the same command (network vnet list --subscription xxxxxx) executed from the CLI is still working fine.

Environment Summary

Linux-4.4.0-19041-Microsoft-x86_64-with-debian-bullseye-sid, Ubuntu 20.04.3 LTS
Python 3.6.10
Installer: DEB

azure-cli 2.30.0

Additional Context

I’ve cleared my account (az account clear) and tried again, same result. I’ve also tried with this environment:

Windows-10-10.0.19041-SP0, 
Python 3.9.4
Installer: PIP

azure-cli 2.30.0

obtaining this output:

User xxxxxxxxxxxxxxx does not exist in MSAL token cache. Run `az login`.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mcomisso-eluxcommented, Nov 29, 2021

Thanks a lot @jiasli, I’ve been able to solve the issue!

Regards, Matteo

1reaction
jiaslicommented, Nov 25, 2021

This is because the Python script and direct az network vnet list command are calling different versions of Azure CLI.

The error message The credential data used by CLI ... has long been removed by #17072. Please make sure the correct version of Azure CLI is called by upgrading to the latest version

pip install --upgrade azure-cli

By the way, your script uses subscription name as --subscription which may break if there are spaces in it. You can use either subscription ID of shlex.split.

Use subscription ID:

from azure.cli.core import get_default_cli


def azurer(args_str):
    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args)
    return cli.result.result

subscriptions = azurer("account list")
for s in subscriptions:
    vnets = azurer("network vnet list --subscription " + s['id'])
    if type(vnets) == dict:
        for v in vnets:
            print(v['name'])

Use shlex.split:

from azure.cli.core import get_default_cli
import shlex


def azurer(args_str):
    args = shlex.split(args_str)
    cli = get_default_cli()
    cli.invoke(args)
    return cli.result.result

subscriptions = azurer("account list")
for s in subscriptions:
    vnets = azurer("network vnet list --subscription '" + s['name'] + "'")
    if type(vnets) == dict:
        for v in vnets:
            print(v['name'])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python script won't run from the command line. It shows no error
If you have Python in PATH but script execution still doesn't work, inspect the C:\Users<user>\AppData\Local\Microsoft\WindowsApps folder.
Read more >
python script does not run from commandline
hello,. I'm having problems suddenly running .py scripts from the command line. (CMD). Before, *.py was associated with python.exe (The ...
Read more >
-bash: python: command not found error and solution - nixCraft
This error means Python is either not installed or your installation damaged including wrong $PATH settings.. Here is how you can solve this ......
Read more >
Python is not recognized as an internal or external command
One way to fix the error would be to launch Python from the Command Prompt by passing in the full path to the...
Read more >
Troubleshooting Common Python Problems - IONOS
If you get a "permission denied" error when you attempt to run this Python script from the command line, most likely the permissions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found