好的,所以我有以下情况。我需要在目标PC上动态编辑PYTHONPATH。目前该项目的结构是:sh脚本相当于python脚本
trunk
bin
start_script
dependencies
dependencies
蟒蛇我能做到,从Start_script中:
root_path = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
dependencies_root = os.path.join(root_path, 'dependencies')
from sys import path
path.extend([root_path, dependencies_root])
这会做的伎俩,但我需要先从process.Popen和新的Python进程看来,对sys.path的改变消失了。
现在我正在考虑一个sh脚本会在这里更好地完成这项工作,不幸的是我在这里总是noob,并且不知道如何继续。该sh脚本应该基本上做的事情蟒蛇从上面一样,所以:
[1] Get the absolute path of the directory the script is located
[2] Get the parent of that folder (say parent_path)
[3] export PYTHONPATH=$PYTHONPATH:parent_path
[4] python start_script.py
所以基本上前两个步骤是我需要帮助的人。另外,如果有一种方法可以对使用subprocess.Popen打开的子进程修改python的sys.path,请让我知道。
在使用'os.environ'字典更新start_script中的'sys.path'的同时,您还可以更新'PYTHONPATH'环境变量。 – MatToufoutu
什么是目标平台?例如,在Linux上,你可以期望人们有'realpath',但是在OSX上,例如,它不是标准的。 Que Larry Wall在这里引用:“编写可移植shell比编写可移植shell脚本更容易”。 – tripleee
@MatToufoutu这是一个答案!回答并获得投票! –