python package import works locally, then blows up in cronjob
<p>running a cronjob I get the following error:</p>

<pre><code>From cchilders@C02S21TWG8WMMBP.localdomain  Fri Sep 30 15:58:00 2016
Return-Path: &lt;cchilders@C02S21TWG8WMMBP.localdomain&gt;
X-Original-To: cchilders
Delivered-To: cchilders@C02S21TWG8WMMBP.localdomain
Received: by C02S21TWG8WMMBP.localdomain (Postfix, from userid 501)
    id D84CE1453D2; Fri, 30 Sep 2016 15:58:00 -0500 (CDT)
From: cchilders@C02S21TWG8WMMBP.localdomain (Cron Daemon)
To: cchilders@C02S21TWG8WMMBP.localdomain
Subject: Cron &lt;cchilders@C02S21TWG8WMMBP&gt; /usr/local/bin/python ~/scripts/updates/update_files.py pull
X-Cron-Env: &lt;SHELL=/bin/sh&gt;
X-Cron-Env: &lt;PATH=/usr/bin:/bin&gt;
X-Cron-Env: &lt;LOGNAME=cchilders&gt;
X-Cron-Env: &lt;USER=cchilders&gt;
X-Cron-Env: &lt;HOME=/Users/cchilders&gt;
Message-Id: &lt;20160930205800.D84CE1453D2@C02S21TWG8WMMBP.localdomain&gt;
Date: Fri, 30 Sep 2016 15:58:00 -0500 (CDT)

Traceback (most recent call last):
  File "/Users/cchilders/scripts/updates/update_files.py", line 12, in &lt;module&gt;
    from ez_scrip_lib.updates import pull_system_file_from_scripts_project_and_update_it, push_system_file_to_scripts_project
ImportError: No module named ez_scrip_lib.updates
</code></pre>

<p>But this library is definitely available:</p>

<pre><code>In [1]: from ez_scrip_lib.updates import pull_system_file_from_scripts_project_and_update_it, push_system_file_to_scripts_project

In [2]: 
</code></pre>

<p>/scripts/updates/update_files.py:</p>

<pre><code>#!/usr/bin/env python


import sys
from ez_scrip_lib.updates import pull_system_file_from_scripts_project_and_update_it, push_system_file_to_scripts_project

files = [{'real_path': '.fake', 'repo_path': 'bash/fake-one'},
         {'real_path': '.fake2', 'repo_path': 'bash/fake2'},
         {'real_path': '.fake3', 'repo_path': 'bash/fake3'}
]

callbacks = {'pull': pull_system_file_from_scripts_project_and_update_it,
             'push': push_system_file_to_scripts_project}

args = sys.argv
purpose_arg = args[1]

for f in files:
    callbacks[purpose_arg](**f)
</code></pre>

<p>Recently I changed the shebang to match, using <code>#!/usr/local/bin/python</code>, still doesn't work</p>

<p>The way I find my package now is from my .bash_profile:</p>

<pre><code>SCRIPTS="$HOME/scripts"
export PYTHONPATH="${PYTHONPATH}:$SCRIPTS"
</code></pre>

<p>Scripts project:</p>

<p>~/scripts/</p>

<pre><code>__init__.py
~/scripts/__init__.py
~/scripts/ez_scrip_lib/__init__.py
~/scripts/ez_scrip_lib/updates.py
~/scripts/updates/update_files.py
</code></pre>

<p>My library <code>ez_scrip_lib</code> is also inside the scripts project, for convenience (I update all things in one editor at one time). It should probably be broken off, as it's very large, but not once have my scripts taken issue at finding things inside <code>ez_scrip_lib</code>. Only in this cronjob does it fail</p>

<p>This script does work fine from command line ran like normal:</p>

<pre><code>./scripts/updates/update_files.py pull
</code></pre>

<p>Removing the python interpreter from crontab since the script already has shebang (one SO suggestion) doesn't work either:</p>

<pre><code>*/1 * * * * ~/scripts/updates/update_files.py pull
</code></pre>

<p>same error</p>

<p>Cronjobs always seem to fail running python files for some reason, but at least on Mac I'm getting error logs automatically. How can I get this cronjob to find my package when it runs python script? Thank you</p>

<p><code>Cron</code> doesn't know which directory is relative to your project, only what is relative to itself. It's likely looking for the modules in some place off in la-la land. A quick fix might be to specify the working directory of the script then try importing the modules:</p>

<pre><code>os.chdir("/Users/cchilders/scripts/updates")
</code></pre>

<p>Or something along those lines should get things back in order...</p>

python bash osx unix crontab