python script fails in terminal via 'import psutil' - python-3.x

I wrote a simple script to check the battery time on my laptop (the built in one on xubuntu seems somewhat inaccurate), the code runs fine in a python terminal but when I run it without starting in the python terminal I get an error related to 'import psutil'.
the script is
I'm very inexperienced with python and haven't been able to find anything helpful online.
#!/usr/bin/env python3.6.7
import psutil
import time
a=psutil.sensors_battery()
b=str(a)
c=b[b.rfind(start)+len(start):b.rfind(end)]
d=int(c)/3600
print(d)
time.sleep(130)
exit()
what I expected is the value c divided by 3600 to be printed on the terminal, display for a while and then exit. instead i get
Desktop$ python battery.py
Traceback (most recent call last):
File "battery.py", line 2, in <module>
import psutil
ImportError: No module named psutil
i also tried running through the menu options when i right click the script and select python3.6, where a terminal flashes and closes immediatly (to quickly to read any of the print in it).
i've also changed
#!/usr/bin/env python3
#!/usr/bin/env python3.6
#!/usr/bin/env python3.6.7
each has the same result
thank you for any advice
david

Related

Cannont run PyGlossary on Windows10

Absolute noob here, so I apologize in advance for stupid questions.
I'm running Windows 10, I've got Python 3.7.3 and I have...
Successfully installed pyglossary-3.2.1
But I cannot run PyGlossary from the command line or the tkinter GUI. If I run any of the following lines
python3 pyglossary.pyw --help
python3 pyglossary.pyw --ui=tk
python3 pyglossary.pyw --version
Nothing happens and I don't get any sort of error message. I've also tried...
python pyglossary.pyw --help
and I get...
Traceback (most recent call last):
File "pyglossary.pyw", line 30, in
from pyglossary import core # essential
File "C:\mypath\pyglossary\pyglossary.pyw", line 30, in
from pyglossary import core # essential
ImportError: cannot import name 'core' from 'pyglossary' (C:\mypath\pyglossary\pyglossary.pyw)
I've checked Windows Path variables, but I think everything's ok there, and I'm able to run other python stuff. Thanks in advance.

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??
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.

Unable to run python code from Linux command line

I have gone through the explanations given in this forum and have tried them in my program. However, none of the suggestions worked. That's why I am opening this thread.
Below is the tree for my project. There are 2 packages: com and main.
When I try to run the code for ProcessRiskModelbyRecordID.py from command line, I am getting below error message:
$ python3 /AppDev/XXXX/py/riskScore/main/ProcessRiskModelbyRecordID.py
Traceback (most recent call last):
File "/AppDev/XXXX/py/riskScore/main/ProcessRiskModelbyRecordID.py", line 6, in
from main.ConnectAPI import *
ModuleNotFoundError: No module named 'main'
When I run the same code from PyDev, I am able to execute it.
Below is the import code from ProcessRiskModelbyRecordID.py:
from main.ConnectAPI import *
from com import DBOperations as DBO,SourceProfile,TargetProfile
Can you please help so that I can run this code from command line?
PyDev is probably setting PYTHONPATH for you. On the command line you would need to set it yourself:
cd riskScore
export PYTHONPATH=`pwd`
python3 main/ProcessRiskModelbyRecordID.py

Python Import Error: No module named mysql.connector

I am running into a problem with mysql.connector in Python 3.7 on Windows. I don't know if it's a conflict with the Python 2.7 installation that I did a few days ago. The original script was working fine last time I checked (more than a month ago). The system variables seem fine. Python 3.7 is there.
I isolated the issue to demonstrate better what is going on.
When I attempt to import mysql.connector with IDLE it raises no issues.
IDLE import mysql.connector
Now, with a simple script
import mysql.connector
print("Hello!")
input("Exit.")
again it's fine if I run it with IDLE editor:
Run script with IDLE editor
In fact, the original script runs as intended with IDLE editor.
But, when I attempt to run the script from cmd with 'python test.py' I get this error:
CMD execute script
Of course, when I try to open the script by double-clicking on it, the window opens and closes immediately. I'd appreciate your help on this issue.
First check your python version using python -V if its not 3.7, then use below to execute code from cmd.
python3 test.py

python module not found error

This question has been asked a few times, but the remedy appears to complicated enough that I'm still searching for a user specific solution.
I recently installed Quandl module using pip command. Even after successful installation my python idle is still showing
Traceback (most recent call last):
File "F:\Python36\Machine Learning\01.py", line 3, in <module>
import Quandl
ModuleNotFoundError: No module named 'Quandl'
I have used import command in my code.
I am using python 3.6.1 version.
I am working on a windows 10 Desktop.
I have also tried re-installation of module.
You can better navigate to your python scripts folder and open a command window there and try pip3 install quandl .Hope this helps.

Resources