TypeError 'str' object is not callable in Python

I am trying to compile a python program and it is giving error as TypeError : 'str' object is not callable

def finaldata(auth_server_url, client_id, client_secret, tracelink_url, soap_auth):
    jsondata=callapi(auth_server_url, client_id, client_secret, token_payload)
    ams_path = jsontodf(jsondata)
    ams_data = pd.read_csv(ams_path)
    count = 0
    tracelink_df= pd.DataFrame(columns=df_cols)
    for row in ams_data.index:
        count=count+1
        tracelink_serial = "{}{}{}{}".format('010',ams_data['productPack.productCode'][row],'21',ams_data['productPack.serialNumber'][row])
        getsoapresponse=gettracelinkdata(tracelink_url, soap_auth, tracelink_payload.format(tracelink_serial))
        temp_tracelink_df = parse_XML(getsoapresponse, df_cols)
        tracelink_df = pd.concat([tracelink_df, temp_tracelink_df], ignore_index=True)

    new_ams_data = ams_data.join(tracelink_df)
    # Rearranging the columns as per Oracle Db
    new_ams_data.rename(columns={"uniqueAlertId":"ALERT_AMS_ID", "productPack.obpId":"OBP_ID","productPack.obpStatus":"OBP_STATUS",
        "productPack.productCode":"PRODUCT_CODE","productPack.productCodeScheme":"PRODUCT_CODE_SCHEMA","productPack.serialNumber":"SERIAL_NUMBER",
        "productPack.batchId":"BATCH_ID","productPack.storedBatchId":"STORED_BATCH_ID","productPack.expiryDate":"EXPIRY_DATE",
        "productPack.storedExpiryDate":"STORED_EXPIRY_DATE","productPack.productName":"PRODUCT_NAME","isManualEntry":"IS_MANUAL_ENTRY",
        "status":"ALERT_STATUS","clientId":"CLIENT_ID","errorMessage":"ERROR_MESSAGE","errorCode":"ERROR_CODE","timestamp":"TIME_STAMP",
        "marketId":"MARKET_ID","actualState":"ACTUAL_STATE","requestTargetState":"TARGET_STATE","PackagingItemCodeType":"TL_PACK_CODE_TYPE",
        "PackagingItemCode":"TL_PACK_CODE","ItemCode":"TL_IMN","Lot":"TL_LOT","ExpirationDate":"TL_EXPIRY_DATE","SerialNumberState":"TL_SN_STATE",
        "ItemState":"TL_ITEM_STATE"}, inplace=True)
    new_ams_data.to_csv(os.path.join(os.getcwd(), 'final_data.csv'), index=False)
    final_data_path = str(os.path.join(os.getcwd(), 'final_data.csv'))
    return final_data_path

So far, i see the program is working till ams_path = jsontodf(jsondata) but after that I dont know why there is this compile error.

NB: it is working fine in VS Code, but I have to execute it via python compiler and this is where it is giving problems.
I tried to run the code line by line and I noticed I am getting error at this point:

tracelink_serial = "{}{}{}{}".format('010',ams_data['productPack.productCode'][row],'21',ams_data['productPack.serialNumber'][row])
getsoapresponse=gettracelinkdata(tracelink_url, soap_auth, tracelink_payload.format(tracelink_serial)) 

Below is the code for function: gettracelinkdata

def gettracelinkdata(tracelink_url, tracesoap_auth, tracelink_payload):
    tracesoap_headers = {'Content-Type':'text/xml; charset=utf-8'}
    soap_response = requests.post(tracelink_url, headers=tracesoap_headers, data=tracelink_payload, verify=False, auth=tracesoap_auth)
    soaptext = soap_response.text
    return soaptext

I need help as I think I am missing some best practices of python in my code. :frowning:

Please post the complete traceback, wrapped in backticks:

```
(traceback lines here)
```
1 Like

With a traceback, and more info, this is a LOT of code to try to figure out!

But: that error means you have tried to use a string like a function – i.e. “calling” it. This is how you call a function (I’m sure you know this)

a_function(some, arguments)

so what is happening is that you have a variable that holds a string, but you are usiong is like a function:

In [369]: a_string = "a string"

In [370]: a_string(3,4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-370-d969263b6fb0> in <module>
----> 1 a_string(3,4)

TypeError: 'str' object is not callable

Look at the line giveing the error, and hopefully you’ll be able to figure out what’s wrong.

Good for you, you even run your code line by line, but just don’t paste the traceback.

Without the traceback, a wild guess is the soap_auth parameter passed in, since it will act as the auth argument of requests.post(), which requires a callable in some cases.