Following the upgrade of a server machine to Ubuntu 16.04, all python programs started by cron fail upon the 'import pyodbc' statement. I installed that package normally via
pip3 install pyodbc
If I create a simple program containgin just the instruction 'import pyodbc', it works if i run it from the command line:
$ python3 /home/test.py
$
I instructed the same program to be run from cron (here is the relevant part of crontab):
$ crontab –l
0,5,10,15,20,25,30,35,40,45,50,55 8,10,12-18 * * * python3 /home/test.py 1>>/var/log/python3.log
Then, in the log file, I get:
Traceback (most recent call last):
File "/home/test.py", line 1, in <module>
import pyodbc
ImportError: No module named 'pyodbc'
The result is the same if I run from the 'main' cron or from a user crontab'. What can I do to solve the problem?
2 Answers
OK, thanks to the suggestion of Jacob Vlijm, I finally found out the solution: the package 'pyodbc' had been installed under user1 (/home/user1/.local/lib/python3.5/site-packages/). As I logged in as user1 in my terminal, the program could find the module. Using cron, I usually run the programs under another user (user2).
That was the problem. Running in cron via user1, everything was OK.
The last question I will investigate is why the command 'pip3 install pyodbc==3.0.10' made the package available to user1 and not to all users.
Alright , make your program like this
#!/usr/bin/env python3
import pyodbc
Give executable permissions like
chmod +x filename.py
and now add it cron and lets see.