如何修改python脚本中的系统路径变量?
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然接受的答案适用于OP的目的,并且虽然第二个答案对于更新 python sys.path 变量是正确的,但我认为,如果OP无法使用接受的答案(因为,比如说,有一个政策反对修改构建/测试机器上的操作系统路径变量),像 this SO 答案 这样的东西将是他们正在寻找的。总结这里的简单情况,更改操作系统路径环境变量:
至少,这是我在阅读问题时希望找到的。
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:
At least, this is what I was hoping to find when I read the question.
@unutbu 有正确的方法,但是对于它的价值,@Joe Schmoe,如果你需要信息:
sys. path 是一个数组,包含启动脚本的 PYTHONPATH 变量中的所有内容(或者 shell 的默认 PYTHONPATH 是什么)。
@unutbu has the right approach, but for what it's worth, @Joe Schmoe, if you ever need the info:
sys.path is an array containing everything that was in your initiating script's PYTHONPATH variable (or whatever your shell's default PYTHONPATH is).
您不需要在 python 脚本中设置 PATH。
相反,将类似的内容
放在 crontab 的顶部。这些环境变量将可供通过 crontab 运行的所有 cron 作业使用。
You shouldn't need to set the PATH from within the python script.
Instead, put something like
at the top of your crontab. These environment variables will then be available to all cron jobs run through your crontab.