how do I modify the system path variable in python script?
Asked Answered
D

3

22

I'm trying to run a python script from cron, but its not running properly so I'm assuming its the different path env variable. Is there anyway to change the variable within a python script?

David answered 2/11, 2010 at 19:21 Comment(6)
You should provide your operating system details. Also, this looks a lot like a possible duplicate - I recommend searching harder.Meit
What does "not running properly" mean? Are you sure you're even starting your script (i.e. it can find your python binary at all)Austere
I'm doing alot of Popen's with programs that have no paths with them.David
the cronlog says its running but the script isn't doing what its supposed to be doing.David
Please do not comment on your own question. It's your question. You can update it. Please update your question to contain all the facts. We can't guess.Patriarchate
The title of this question should change. I came here because I actually want to change the path variable in a python script, not because I need information about running a python script from a cron job.Lump
B
5

You shouldn't need to set the PATH from within the python script. Instead, put something like

USER=joe
HOME=/home/joe
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/some/other/path
PYTHONPATH=/home/joe/pybin
MAILTO=joe
LANG=en_US.UTF-8

#min hr    day   mon dow
*/5  12    *     *   *     reminder.py 'Eat lunch'

at the top of your crontab. These environment variables will then be available to all cron jobs run through your crontab.

Baseborn answered 2/11, 2010 at 19:32 Comment(0)
B
55

While the accepted answer works for the OP's purposes, and while the second answer is correct for updating the python sys.path variable, I think, if the OP weren't able to use the accepted answer (because, say, there was a policy against modifying the OS PATH variable on build/test machines), something like this SO answer would be what they are looking for. Summarizing the simple case here, to change the OS PATH environment variable:

app_path = os.path.join(root_path, 'other', 'dir', 'to', 'app')
os.environ["PATH"] += os.pathsep + app_path

At least, this is what I was hoping to find when I read the question.

Bename answered 15/12, 2016 at 18:40 Comment(2)
+1 for the case you want to set PATH (and PYTHONPATH is similar code) , though note that sys.path and os.environ['PATH'] are / can be different.Medici
This is what I understood while reading the OP's question as well. I was trying to set the PATH using 'export PATH=$PATH:' + THE_OTHER_PATH and it wasn't working.Pitch
T
18

@unutbu has the right approach, but for what it's worth, @Joe Schmoe, if you ever need the info:

import sys
print sys.path
['.', '/usr/local/bin', '/usr/local/lib/python2.6/dist-packages',...]
sys.path.append('/home/JoeBlow/python_scripts')
print sys.path
['.', '/usr/local/bin', '/usr/local/lib/python2.6/dist-packages', '/home/JoeBlow/python_scripts',...]
   

sys.path is an array containing everything that was in your initiating script's PYTHONPATH variable (or whatever your shell's default PYTHONPATH is).

Tuber answered 2/11, 2010 at 19:46 Comment(0)
B
5

You shouldn't need to set the PATH from within the python script. Instead, put something like

USER=joe
HOME=/home/joe
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/some/other/path
PYTHONPATH=/home/joe/pybin
MAILTO=joe
LANG=en_US.UTF-8

#min hr    day   mon dow
*/5  12    *     *   *     reminder.py 'Eat lunch'

at the top of your crontab. These environment variables will then be available to all cron jobs run through your crontab.

Baseborn answered 2/11, 2010 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.