0

Environment:

Python 3.7

Windows 10 64bits

IDE Pycharm 2019

Problem:

The piece of code below used to work without issue. I had to move the folder of my Pycharm project and now I get this error message: "TypeError: 'str' object is not callable".

The code:

import os    
import psutil    

mypid = os.getpid()
print(f"PID of Program: {mypid}")
PROCNAME = "Program.exe"
for proc in psutil.process_iter():
    print(f"proc : {proc} - Type: {type(proc)}")
    print(f"proc.name : {proc.name} - Type: {type(proc.name)}")
    print(f"PROCNAME : {PROCNAME} - Type: {type(PROCNAME)}")
    print(f"proc.pid : {proc.pid} - Type: {type(proc.pid)}")
    print(f"mypid : {mypid} - Type: {type(mypid)}")
    if proc.name() == PROCNAME and proc.pid != mypid:  # < ====== Line 15 where is the error
        print(f"Program will kill this process : {proc}")
        proc.kill()

The error output:

PID of Program: 4176
proc : psutil.Process(pid=0, name='System Idle Process') - Type: <class 'psutil.Process'>
proc.name : System Idle Process - Type: <class 'str'>
PROCNAME : Program.exe - Type: <class 'str'>
proc.pid : 0 - Type: <class 'int'>
mypid : 4176 - Type: <class 'int'>
Traceback (most recent call last):
  File "E:/CFF Dropbox/Gauthier Buttez/cff/Python/PhoneBot_0002/test1.py", line 15, in <module>
    if proc.name() == PROCNAME and proc.pid != mypid:
TypeError: 'str' object is not callable

What I tried:

I isolated this piece of code in a blank python document. I wanted to be sure the issue is not connected to other piece of code located somewhere else. When I execute this isolated piece of code, I get same issue.

I watched here similar questions about "TypeError: 'str' object is not callable", but all the answers are specific to the code of the question and it didn't help me to understand the problem.

What I am doing wrong. As you can see I print the value and the type of each variable and I am comparing string to string and int to int. What is the issue? I don't understand. Can you help me, please?

1 Answer 1

2
proc.name()

proc.name is a string, not a method. Access it using proc.name, with no parentheses.

As a rule of thumb, whenever you are facing a *** object is not callable error you should look for a set of misplaced (...).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.