Search code examples
python-3.5psutil

module 'psutil' has no attribute 'cpu_percent'


Hello guys am new to python and i just started learning System tools in Python 3.5. I want to get my cpu usage for every 5 seconds interval, so i use psutil. But i am having an error. My code is below->

import psutil

cpu = psutil.cpu_percent(interval=5, percpu=True)

print(cpu)

and when i run this code am having following error->

Traceback (most recent call last):
  File "/home/ayush/Documents/psutil.py", line 1, in <module>
    import psutil
  File "/home/ayush/Documents/psutil.py", line 3, in <module>
    cpu = psutil.cpu_percent(interval=5, percpu=True)
AttributeError: module 'psutil' has no attribute 'cpu_percent'

Why am getting this error!


Solution

  • You should rename your file from psutil.py to something else.

    Python now tries to import cpu_percent from your psutil as it has the same name as the module that you're trying to import.

    After that things should work as intended.