1

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?

  • what lies in /home/test.py > – rɑːdʒɑ Aug 24 '16 at 10:45
  • As described, a single line import pyodbc. I edited the question to make it more visible. – Marc Vanhoomissen Aug 24 '16 at 11:05
  • 2
    Just a hunch (am on mobile atm), but cron runs with a limited set of env variables. Possibly python path is different. I believe if you set the abs path to the module, it should work. – Jacob Vlijm Aug 24 '16 at 11:16
  • @Jacob: There surely lies something in what you write: looking for the exact location of pyodbc, I found it under python3.4 but when i start python3, I end up in python 3.5. Is there a way to specify that i want to install module pyodbc for python 3.5? Or is it better to eliminate (how?) python 3.4? – Marc Vanhoomissen Aug 24 '16 at 12:04
  • Isn't it possible to just add the path (in the script) to the module, like: askubuntu.com/a/471168/72216 – Jacob Vlijm Aug 24 '16 at 12:17
1

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.

|improve this answer|||||
  • Because pip3 install by default only installs locally for the user invoking the command, which is user1 in this case. – edwinksl Aug 24 '16 at 14:56
  • 3
    You could install it for all users by getting it from the official Ubuntu repositories: sudo apt install python3-pyodbc – edwinksl Aug 24 '16 at 15:00
  • ...I nevere pip. – Jacob Vlijm Aug 24 '16 at 15:21
  • @edwinksl: thanks. I have then incorrectly interpreted the official documentation of python (packaging.python.org/installing/#use-pip-for-installing) specifying how to install just for one user. I derived from it that if I do not use the flag '--user', the installation is for everybody. – Marc Vanhoomissen Aug 24 '16 at 15:27
0

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.

|improve this answer|||||
  • I tried your proposal running both 'python3 /home/test.pt' and '/home/test.py': that works in terminal but result in an ImportError in cron. – Marc Vanhoomissen Aug 24 '16 at 11:36
  • @MarcVanhoomissen have you add in the cron as normal script like " ./scriptname.py" – rɑːdʒɑ Aug 24 '16 at 11:37
  • No, exactly what I wrote: python3 /home/test.py and /home/test.py I did not want trouble with relative path. – Marc Vanhoomissen Aug 24 '16 at 11:52
  • But you still can something like /home/./test.py – rɑːdʒɑ Aug 24 '16 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.