Python :- os.cpu_count() – Error – “AttributeError: ‘module’ object has no attribute ‘cpu_count'”

Background

For our last post, reviewing options for counting processors and cores on our Linux hosts.

Stack Overflow

As always, Stack Overflow has options.

User jumped in and offered a short line via Python.

Code

Python

Image

How to obtain the number of CPUs/cores in Linux from the command line?
Link

python.stackoverflow.01.20190804.1040PM

Code Snippet


python -c "import os; import pprint; print(os.cpu_count())"

Output

Output – Text

AttributeError: ‘module’ object has no attribute ‘cpu_count’

Output – Image

attributeError.01.20190804.0931PM.PNG

Output – Explanation

  1. The error message is a bit clear
  2. The module, os, does not have the attribute, cpu_count, that we referenced

Trouble Shooting

Module – List Methods & Attributes

Code


python -c "import os; import pprint; pprint.pprint(vars(os))" | sort | more

Output

os_listFunctions_01_20190804_0957PM

Explanation

  1. cpu_count is not one of the functions returned by issuing vars against our module

Documentation

Checked documentation for Python :-

Documentation » The Python Standard Library » Generic Operating System Services
os — Miscellaneous operating system interfaces
Link

Version

Image

docs.cp_count.v3.4.PNG

Explanation

The documentation is clear.

This method was introduced in Python version 3.4

What is our Version ?

Code


python -V

Output

metadata.version.01.20190804.1008PM

Explanation

The version of Python installed on our OS is 2.6.6.

os.cpu_count() requires version 3.4

Works

Code


python -c "import multiprocessing; import pprint; print(multiprocessing.cpu_count())"

Output

mutiprocessing.cpu_count.01.20190804.1014PM

Explanation

Multiprocessing.cpu_count works!

Summary

We live in a time where we never have enough time.

But, do not let that alone discourage you.

Keep pushing!

 

References

  1. Python
    • Documentation » The Python Standard Library » Generic Operating System Services
      • os — Miscellaneous operating system interfaces
        Link
  2. CodeVSColor
    • Write a python program to find the number of cpu count
      Link
  3. Stack Overflow
    • How to obtain the number of CPUs/cores in Linux from the command line?
      Link
    • How do I look inside a python object
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s