Python does not get environment variable when using cron.

Asun Friere afriere at yahoo.co.uk
Mon Aug 18 22:47:10 EDT 2008


On Aug 18, 11:17 pm, "Shawn Milochik" <Sh... at Milochik.com> wrote:
<snip>
>
> The easiest solution (in my opinion) is to write a bash script to
> execute your Python script, and use that bash script to add those
> environment variables.

Agreed.  Wrap it in a shell script, easier to read and grow than a
oneliner on the crontab.

> The most likely file you'll want to run is
> .bashrc in your home directory. If you're on a Mac, it's .bash_login
> instead.
>
> Example:
>
> #/usr/bin/env bash
>
> source ~/.bashrc
> path/my_script.py
>

I for one don't have $HOSTNAME defined in my .bashrc file.  I doubt
this is likely to give him much joy.

> Something like that should take care of it. If not, get creative --
> add the "env" command to your bash script and have it send the output
> to a file: env > cron_env.txt
>

Again no. The reason os.environ can't find HOSTNAME is that it is NOT
defined in the environment, if env can find it os.environ should be
able to as well.





More information about the Python-list mailing list