简体   繁体   English

设置pythonpath的正确方法

[英]correct way of Setting pythonpath

#!/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. 我正在将脚本作为cron作业运行,并想将PYTHONPATH环境变量设置为'/ usr / bin / python'以便运行脚本。 What is the correct way of of the two mentioned in the snippet above? 上面摘录中提到的两个的正确方法是什么?

Updating sys.path.append() will change the paths that the current script searches for modules/packages. 更新sys.path.append()将更改当前脚本搜索模块/软件包的路径。 Updating os.environ[] will only affect subprocesses, if you pass them the environment. 如果将环境传递给子流程,则更新os.environ[]仅会影响子流程。 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. 这些仅影响搜索模块/软件包的目录,因为/usr/bin/python通常是python可执行文件,两者都不起作用。

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: 如果您尝试指定用于执行脚本的python版本,请在脚本顶部使用shebang:

#!/usr/bin/python

Make sure the script is set executable chmod +x script and then execute it directly via cron. 确保将脚本设置为可执行chmod +x script ,然后直接通过cron执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM