Displaying system information using psutil

In summary: The Process class in psutil apparently has a member named memory_info(). You can use this to get information about the memory usage of a process.
  • #1
zak100
462
11
TL;DR Summary
I am trying to display system information. I found some commands but I am getting a sytax error
Hi,
I am getting following error message:
Traceback (most recent call last):
File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 31, in <module>
obj_Counting_SysInfo.main()
File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 27, in main
self.system_info()
File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 16, in system_info
mem = process.get_memory_info()[0] / float(2 ** 30) # memory in GB
AttributeError: 'Process' object has no attribute 'get_memory_info'

I have written the following code which should display the system information of a simple program which I am running as a function:
Python:
import psutil

import osclass Counting_SysInfo:

    def __init__(self, i):

        self.i = i    def Print_Numbers(self):

        for i in range(50):

            print (i)    def system_info(self):

        # return the memory usage in MB

        self.Print_Numbers()

        process = psutil.Process(os.getpid())

        mem = process.get_memory_info()[0] / float(2 ** 30)  # memory in GB

        cpu = process.cpu_percent() / psutil.cpu_count()

        disk = process.disk_usage(part.mountpoint).percent  # https://github.com/giampaolo/psutil/blob/master/scripts/disk_usage.py

        vMem = int(process.virtual_memory().available)

        print(process.memory_info().rss)  # in bytes

        RSS = process.memory_info().rss

        # https://stackoverflow.com/questions/17990227/whats-the-unit-of-rss-in-psutil-process-get-memory-info

        pageFaults = process.memory_info().pfaults  # https://readthedocs.org/projects/giamptest/downloads/pdf/latest/

        print("mem= ", mem, "cpu= ", cpu, 'disk= ',disk, "VMem= ", vMem, "RSS= ", RSS, "page faults=", pageFaults )    def main(self):

        self.system_info()if __name__ == "__main__":

    obj_Counting_SysInfo = Counting_SysInfo(10)

    obj_Counting_SysInfo.main()

Somebody please guide me how to solve this problem.

Zulfi.
 
Technology news on Phys.org
  • #2
Looking at the documentation for the Process class in psutil, there is no member named get_memory_info, which is exactly what the error message says. The Process class apparently has a member named memory_info().
 
  • Like
Likes sysprog and pbuk
  • #3
Hi,
Thanks, I solved the above but I am getting cpu utilization zero.

Code:
def systeminfo(self):
        print("memory usage=", psutil.Process(os.getpid()).memory_info()[0])
        print(psutil.disk_usage('/'))
        print(psutil.Process(os.getpid()).cpu_percent(interval=1))
        print("virtual mem= ", psutil.virtual_memory().available)
        # print(psutil.Process(os.getpid()).memory_info().pfaults)
        print("rss=", psutil.Process(os.getpid()).memory_info().rss)
        self.selectionSort()
        print(psutil.Process(os.getpid()).cpu_percent(interval=1))    def main(self):
        self.generate1000_5digit_RandomNum()
        self.systeminfo()if __name__ == "__main__":
    objSSSort = SSSort(1000)
    objSSSort.main()

I am trying with 1000 elements in the sorting list:

Following is one portion of my output:
memory usage= 15269888
sdiskusage(total=982900588544, used=141417086976, free=791483416576, percent=15.2)
0.0
virtual mem= 12050067456
rss= 15269888
(0, 10000)
Somebody please guide me.

Zulfi.
 

Related to Displaying system information using psutil

1. How do I install psutil?

To install psutil, you can use pip or easy_install by running one of the following commands in your terminal:
- "pip install psutil"
- "easy_install psutil"

2. How do I use psutil to display system information?

After installing psutil, you can import the library into your Python script and use its functions to retrieve system information. For example, you can use psutil.cpu_percent() to get the current CPU usage, or psutil.virtual_memory() to get information about the system's memory usage.

3. Can I use psutil to get real-time system information?

Yes, psutil can be used to retrieve real-time system information. By using psutil's functions, you can get live updates on CPU usage, memory usage, disk usage, and more. This makes psutil a great tool for monitoring system performance.

4. What operating systems are supported by psutil?

Psutil is a cross-platform library and supports various operating systems, including Windows, Linux, macOS, FreeBSD, OpenBSD, NetBSD, AIX, and Sun Solaris. It can also be used on a Raspberry Pi running Raspbian.

5. Is psutil open-source and free to use?

Yes, psutil is an open-source library released under the BSD license. This means it is free to use, modify, and distribute for both personal and commercial purposes. You can find the source code on the project's GitHub page.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
851
  • Programming and Computer Science
Replies
5
Views
3K
Replies
10
Views
2K
Replies
2
Views
5K
  • Special and General Relativity
Replies
13
Views
2K
Back
Top