Question

#!/usr/bin/python

import os
import sys

sys.path.append('/usr/bin/python')    
vs
os.environ['PYTHONPATH'] = '/usr/bin/python'

I am running a script as a cron job and want to set PYTHONPATH environ variable to '/usr/bin/python' for the script to be run. What is the correct way of of the two mentioned in the snippet above?

Était-ce utile?

La solution

Updating sys.path.append() will change the paths that the current script searches for modules/packages. Updating os.environ[] will only affect subprocesses, if you pass them the environment. These only affect the directories that are searched for modules/packages, as /usr/bin/python is usually the python executable neither would have any effect.

If you are trying to specify a version of python to use to execute the script then use a shebang at the top of the script:

#!/usr/bin/python

Make sure the script is set executable chmod +x script and then execute it directly via cron.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top