无法在 python 脚本中使用源命令

无法在 python 脚本中使用源命令

我基本上想为 Unix 中的一些变量设置路径。但source命令在这里不起作用。有相同的建议吗?

答案1

我尝试了你所描述的,解决方案是使用.而不是source,它基本上是前者的别名。
您还必须明确指定./FILENAME该文件是否位于当前目录中。

请参阅我的示例会话:

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("source .bashrc")
sh: 1: source: not found
32512
>>> os.system(". .bash_aliases")
sh: 1: .: .bash_aliases: not found
512
>>> os.system(". ./.bash_aliases")
0
>>> os.system(". ~/.bash_aliases")
0
>>> os.system(". /home/USERNAME/.bash_aliases")
0

返回值0表示成功。

但是,我不确定以这种方式获取文件是否会产生您想要的结果,因为此方法在子 shell 中运行给定的命令,并且我不确定这是否也会影响您想要的 shell 会话。

相关内容