Shambhav Agrawal
Shambhav Agrawal

Reputation: 151

No module named psutil when executed using python filename.py but works perfectly with shebang & chmod

consider this code in python

#!/usr/bin/env python3
import shutil
import psutil

def check_disk_usage(disk):
    a=shutil.disk_usage(disk)
    free=a.free/a.total*100
    return free

print(check_disk_usage("/"))

when i execute this in terminal using "python health.py". it gives error

shambhav@shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ code health.py
shambhav@shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ python health.py
Traceback (most recent call last):
  File "health.py", line 3, in <module>
  import psutil
ImportError: No module named psutil

but when i execute like this it works perfectly. why so??

shambhav@shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ code health.py
shambhav@shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ chmod +x health.py
shambhav@shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ ./health.py
87.24079278480475

Can someone help me with this??

Upvotes: 0

Views: 107

Answers (1)

Derlin
Derlin

Reputation: 9881

In your shebang, you explicitly use python3, but not when you launch it from the terminal. Hence, try:

python3 health.py

In order to understand, try typing which python or python --version in your terminal. I bet it is not pointing to python3, but python2.

Upvotes: 1

Related Questions