Shell Script For Python Throws ModuleNotFoundError - python

I'm trying to run a python script at start for my Raspberry Pi 3B.
My shell script "atboot.sh" looks like this:
#!/bin/bash
cd /home/pi/pythoncodefolder
date >> logs
sudo python3 test.py >> logs
When I try to run it on cmd with sh atboot.sh command, I get an import error which is:
Traceback (most recent call last):
File "test.py", line 5, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
But when I run the program on cmd without shell script, using python3 test.py, I get no errors.
Thanks.

The use of sudo causing it. When you run python3 program.py you invoke it in your $USER environment setup.
You can remove Defaults !env_reset from sudoers. Or to add Defaults env_keep += "PYTHONPATH".
But I would assert that you can do it without sudo in the first place.
#!/bin/bash
cd /home/pi/pythoncodefolder
date >> logs
python3 test.py >> logs
Also, that's probably a (not exact) duplicate.

Related

Running python from command line asks me to import modules

I have a script written in Spyder(Python 3.8) on Linux. While on the Spyder console the script runs fine, while I an calling it from the Linux terminal it seems like it doesn't see the modules I import in the script. Opening the terminal I run: python3 /zhome/c9/f/144817/Desktop/ChargersDaniel.py but, here is the error I take:
Traceback (most recent call last): File "/zhome/c9/f/144817/Desktop/ChargersDaniel.py", line 9, in <module>
import GPyOpt ModuleNotFoundError: No module named 'GPyOpt'
where GPyOpt is the very first library I have at the first lines in my script.
It looks like for some reason, python3 doesn't see the libraries installed. I have checked the solution here but that's not my case cause I am already calling Python3 at my terminal.
Any suggestions ?
Thanks in advance
Maybe check if GPyOpt is installed when running from your terminal:
python3 -c "import GPyOpt"
if [ $? -eq 0 ]
then
echo "GPyOpt is installed!"
fi

crontab and ImportError: No module named psycopg2

Im trying to create a cron job that runs every 15 minutes
*/15 * * * * cd Desktop && /usr/bin/python myFile.py >> myLog 2>&1
When this tries to run, I get this error in myLog
Traceback (most recent call last):
File "myFile.py", line 1, in <module>
import psycopg2
ImportError: No module named psycopg2
Even though when I run it manually in visual studio code with command python myFile.py it runs with no errors and has no issues with psycopg2...any idea whats happening?
Your system might be using Python2 by default at /usr/bin/python. Check if that is the issue and change it to the path either your Python3 executable path or the path of your virtual environment's executable and run.
Also, a better approach would be to have a shell script be executed via crontab and that script internally either activates your virtual environment or runs the program directly after cd-ing into the directory

crontab failing to use anaconda python even after adding path (cron calls .sh file which in turn calls .py file)

I have a cronjob:
1 11 * * * /home/ubuntu/anaconda3/bin/python /home/ubuntu/ga_data/gaV4/tva_gaApiUpdate.sh > /dev/null 2 >> tvaUpdater.log
And here is the .sh file:
#!/bin/bash
cd /home/ubuntu/ga_data/gaV4
python gaAPIWorkingVersion.py
echo "ran cron at" | tee -a "$tvaUpdater.log"
echo $(date) | tee -a "$tvaUpdater.log"
The output of the log file is:
Traceback (most recent call last):
File "gaAPIWorkingVersion.py", line 13, in <module>
from apiclient.discovery import build
ImportError: No module named apiclient.discovery
I'm calling a .sh file with cron that in turn calls a .py file. I do this because in the .sh file I change directory with cd, so that I don't have to change paths within my .py script to absolute paths. This is because my larger .py script opens several files from within it's directory. So, rather than call python directly in the cron I do so indirectly by calling the .sh file first.
When I run the .sh or .py script directly myself, everything works. It's only when cron tries to do so that I get the error above.
If I go which python in the terminal I get /home/ubuntu/anaconda3/bin/python.
I tried adding that line into my .sh file like so:
#!/bin/bash
cd /home/ubuntu/ga_data/gaV4
python /home/ubuntu/anaconda3/bin/python gaAPIWorkingVersion.py
But that also threw an error.
How can I get cron to use the right version of python (anaconda in this case)?
Try change your cron to:
1 11 * * * /home/ubuntu/ga_data/gaV4/tva_gaApiUpdate.sh > /dev/null 2 >> tvaUpdater.log
and script to
#!/bin/bash
cd /home/ubuntu/ga_data/gaV4
/home/ubuntu/anaconda3/bin/python gaAPIWorkingVersion.py
echo "ran cron at" | tee -a "$tvaUpdater.log"
echo $(date) | tee -a "$tvaUpdater.log"
Make sure that user under which scipt is running directly is same as the one of cronjob.
If you still have problems, check Cannot Import Python MySQL module when running a script using crontab

made a shell to run script on startup, suddently it gives me an importerror

I followed this guide: guide to create a startupfile which excecutes a python file on startup.
in step 2 it says I have to test the startupfile I just created and suddently my script says:
Traceback (most recent call last):
File "Display.py", line 1, in <module>
import pyowm
ImportError: No module named pyowm
the python file works perfect if I run it directly.
what I allready tried: run pip again to see if the lib was okay
check the /usr/local/lib/python3.4/dist-packages folder to see if it was there and it is.
I think this is a python issue and not a RaspberryPi issue thats why I uploaded it here.
runned by:
sh launcher.sh
inside is:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd /home/pi/arduino/Python/Main/Master
sudo python Display.py
cd /
Simple fix: define the version of python which will be used. It used python 2.7. Yet the lib was for 3.4.

Convert shell script command to command line

I am executing python script with multiple command line parameter, but using shell script.
command i execute for shell script execution is:
./scripts/run_qa.sh data/questions/questions.txt data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
run_qa.sh files looks like below (please explain how it works):
#!/bin/bash
set -u
set -e
if [ $# != 4 ]; then
echo "Usage: run.sh questions lexicon weights db"
exit 1
fi
questions=$1
lexicon=$2
weights=$3
db=$4
PYTHONPATH=$PWD/python python -m lex.gearman_worker $lexicon $weights $db < $questions
I tried to execute python command as below in Command line :
python -m python/lex/gearman_worker.py data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
which gives error :
/usr/bin/python: Import by filename is not supported.
Update1 :
gearman_worker.py file import other files like ths:
import lex.parse
import lex.semantics
from collections import namedtuple
from collections import defaultdict
import line gives error like this:
ImportError: No module named lex.lexicon
Update2 (executed on linux terminal):
export PYTHONPATH=$/mnt/paralex-evaluation-gearman/python
PYTHONPATH = ./python python -m python/lex/gearman_worker data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
gives:
PYTHONPATH: command not found
Then
python -m python/lex/gearman_worker data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
gives:
File "/mnt/paralex-evaluation-gearman/python/lex/gearman_worker.py", line 3, in <module>
import lex.lexicon
ImportError: No module named lex.lexicon
You just need to execute the following command:
PYTHONPATH=./python python -m lex.gearman_worker ARGUMENT_2 ARGUMENT_3 ARGUMENT_4 < ARGUMENT_1
If that doesn't work then you may have to export the PYTHONPATH setting:
export PYTHONPATH=${PWD}/python
python -m lex.gearman_worker ARGUMENT_2 ARGUMENT_3 ARGUMENT_4 < ARGUMENT_1
The original arguments that you would pass to the script are listed as ARGUMENT_N.
The script just:
sets some sensible defaults (see the documentation for set)
tests the the right number of arguments have been supplied
invokes the command above
Your attempt to invoke it:
misses the PYTHONPATH setting which is present in the script
passes the gearman_worker module as a file rather than a python module import

Categories