0

I am trying to set up crontab for a script written in python which is intalled via Anaconda. The simple .py runs (it just loads a module for a demo) and can be run in terminal with python a.py . The problem is getting crontab the path for python and imported modules.

I have been trying to set the PATH and PYTHONPATh to the python directory and also where pandas is located. I think this is a env issue but have no idea what that means. Any ideas?

The crontab is:

SHELL=/bin/sh
PYTHONPATH=/Users/Esel/anaconda3/bin/python
* * * * *  cd /Users/Esel/Documents/x/y/z && python a.py

The python (test) script:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 21:36:34 2019

@author: Esel
"""

import pandas

print('hello kitty')
# This is a test 

Crontab mails the following statement:

Traceback (most recent call last): File "a.py", line 3, in import pandas ImportError: No module named pandas

2 Answers2

0

You're setting an env.var, cd'ing to a directory, and calling an interpreter, all from crontab.

Under those circumstances, personally I'd find it more elegant to just create a small wrapper script (here called pyscript) that does all that, and call that script from crontab:

$> cat /home/me/bin/pyscript
#!/bin/bash
# a.py needs module pandas in /foo/bar
export PYTHONPATH=/foo/bar
cd /Users/Esel/Documents/x/y/z || exit
python a.py

$> chmod ug+rx /home/me/bin/pyscript

$> cat /var/spool/cron/crontabs/me
...
* * * * *  /home/me/bin/pyscript
0

With some ideas from a roadowl (thanx) and another SOverflow question (54564187) I think I got it running.

SHELL=/bin/sh
PATH=$PATH/Users/Esel/anaconda3/bin:/Users/Esel/anaconda3/condabin:/Applications/anaconda3/bin:/Applications/anaconda3/bin:/Users/Esel/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
* * * * *  cd /Users/Esel/Documents/x/y/z && python a.py

The path came from

echo $PATH