1
#!/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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Both the statments work. I want to know the difference between sys.path.append('/usr/bin/python') and os.environ['PYTHONPATH'] = '/usr/bin/python'
This is exactly wat i wanted. I am running sub scripts from the main script as subprocesses. So for each of the subscript I run , I need to change the environment var's. SO I guess from ur explaination , I should user os.environ[]. Thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.