好的,所以我有以下情况。我需要在目标 PC 上动态编辑 PYTHONPATH。现在项目的结构是:
trunk
bin
start_script
dependencies
dependencies
从python我可以做,从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 脚本会在这里做得更好,不幸的是我在这里完全是菜鸟,不知道如何继续。sh 脚本应该基本上完成上面的 python 所做的事情,所以:
[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 进行更改,请告诉我。