I tried to reproduce the same in my environment and got below results:
I have one Azure VM named srivm
with Public IP as below:

To fetch this via Python API, I created one service principal with client secret as below:

I tried using below code to get Public IP address of virtual machine and got it successfully:
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
credential = ClientSecretCredential(
tenant_id=<tenantID>, #Your TenantID
client_id=<appID>, #Your AppID
client_secret=<secret> #Your Client Secret Value
)
compute_client = ComputeManagementClient(
credential=credential,
subscription_id=<subscriptionID> #Your SubscriptionID
)
network_client = NetworkManagementClient(
credential=credential,
subscription_id=<subscriptionID> #Your SubscriptionID
)
rg = 'sri' #Resource group name
publicip = 'xxxxxxxxx' #Subnet name
result_get = network_client.public_ip_addresses.get(
rg, publicip
)
print(result_get.ip_address)
Response:

You can try using the same code in your environment in order to fetch the Public IP of virtual machine.
Make sure to install required modules and assign required role to the service principal before running the code.