0

I'm on a mac when I normally run

Jupyter notebook #or
python #or
python3
which python  # ===   /Users/myname/anaconda3/bin/python

This works as expected with python 3.6 being used.

HOWEVER, when I setup my python code to run via a cronjob I am running into errors as some of my code is NOT compatible with 2.7 vs 3.6. I'm noticing that my python script in my cronjob is being run with 2.7.

Obviously this is somewhat basic, but CAN'T figure out how to ensure that my python code that is run via cronjob is executed in Python3 vs Python2.

I run

vim ~/bash_profile and I see all of the following

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH

# added by Anaconda3 2.0.1 installer
export PATH="/Users/myname/anaconda/bin:$PATH"

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# added by Anaconda 2.0.1 installer
export PATH="/Users/myname/anaconda/bin:$PATH"

# added by Anaconda 2.1.0 installer
export PATH="/Users/myname/anaconda/bin:$PATH"


# added by Anaconda2 4.1.1 installer
export PATH="/Users/myname/anaconda/bin:$PATH"

# added by Anaconda3 5.0.0 installer
export PATH="/Users/myname/anaconda3/bin:$PATH"

What do I need to change so that chronjobs are run with 3.6?

runningbirds
  • 6,235
  • 13
  • 55
  • 94
  • See this: https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take – pault Feb 11 '18 at 17:59
  • 1
    How did you install Python 3.6? In your `.bash_profile` it's showing 3.4, and you got paths stepping all over each other... – l'L'l Feb 11 '18 at 17:59
  • Where is the path to Python 3.6 in your configuration above? – Inian Feb 11 '18 at 18:08
  • Also note that `cron` has a minimalist view of the system `PATH` variables. If you are unsure the paths are not reflecting, set the `PATH` variable as part of the crontab itself, initialize `PATH` to the value you need, just above your cron schedule definition – Inian Feb 11 '18 at 18:14

1 Answers1

0

I'm not sure how you have Python 3.6 installed on your machine. I see a reference to Python 3.4 in the included bash profile but not 3.6.

I see you have Anaconda installed. I make use of the conda environments feature to run the several Python scripts cron jobs on my desktop mac. You may want to consider using anaconda environments (or python virtual environments which could do a similar thing for you) as they will isolate dependencies your script might have including things like differences in major python version.

You can create a conda environment like this (below is for python 3.6, but change it if you want 2.7):

conda create --name Py36TestEnv python=3.6

you then activate it by typing

source activate Py36TestEnv

install any packages you want using pip or conda then in your crontab you can either explicitly call out the python interpreter like so:

0   *   *   *   *   /Users/user/anaconda3/envs/Py36TestEnv/bin/python /Users/user/Documents/Alerts/Email.py

Or you can put it in the bang at the top of your script file and just execute the script via cron. Here's an example of a script I have running via cron (at the top of the hour) to uses a bang link at the top of the file (#!/Users/user/anaconda3/envs/Python36TestEnv/bin/python):

0   *   *   *   *   /Users/user/Documents/Alerts/Email.py

Note: conda environments get placed under your anaconda install location which in my case is

~/anaconda3/envs/
Tim
  • 98
  • 4