如何修改python脚本中的系统路径变量?

发布于 2024-09-30 07:46:09 字数 76 浏览 1 评论 0原文

我正在尝试从 cron 运行 python 脚本,但它运行不正常,所以我假设它的路径环境变量不同。有没有办法改变Python脚本中的变量?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┈┾☆殇 2024-10-07 07:46:10

虽然接受的答案适用于OP的目的,并且虽然第二个答案对于更新 python sys.path 变量是正确的,但我认为,如果OP无法使用接受的答案(因为,比如说,有一个政策反对修改构建/测试机器上的操作系统路径变量),像 this SO 答案 这样的东西将是他们正在寻找的。总结这里的简单情况,更改操作系统路径环境变量:

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

至少,这是我在阅读问题时希望找到的。

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.

十年不长 2024-10-07 07:46:10

@unutbu 有正确的方法,但是对于它的价值,@Joe Schmoe,如果你需要信息:

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 是一个数组,包含启动脚本的 PYTHONPATH 变量中的所有内容(或者 shell 的默认 PYTHONPATH 是什么)。

@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).

囚你心 2024-10-07 07:46:10

您不需要在 python 脚本中设置 PATH。
相反,将类似的内容

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'

放在 crontab 的顶部。这些环境变量将可供通过 crontab 运行的所有 cron 作业使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文