使用 bash 脚本添加到 $PYTHONPATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38579398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:56:07  来源:igfitidea点击:

Adding to $PYTHONPATH with bash script

linuxbashenvironment-variables

提问by cmoses

I wrote a bash script to add to my $PYTHONPATH. My .shfile has the following:

我写了一个 bash 脚本来添加到我的$PYTHONPATH. 我的.sh文件有以下内容:

sudo echo export PYTHONPATH=$PYTHONPATH:/path/to/new/python/module >> ~/.bashrc

What I want to be added to my .bashrcis:

我想添加到我的.bashrc是:

PYTHONPATH=$PYTHONPATH:/path/to/new/python/module

However I can only get it to add:

但是我只能让它添加:

PYTHONPATH=/all/other/python/modules/already/on/path:/path/to/new/python/module

I don't want the actual $PYTHONPATHvalue to be added to my .bashrc, just the variable name. Please help!

我不希望将实际$PYTHONPATH值添加到 my 中.bashrc,而只是将变量名添加到。请帮忙!

采纳答案by John1024

Use single-quotes:

使用单引号:

$ echo 'export PYTHONPATH=$PYTHONPATH:/path/to/new/python/module' >> .bashrc
$ cat .bashrc 
export PYTHONPATH=$PYTHONPATH:/path/to/new/python/module

The shell does not perform variable expansion on single-quoted strings.

Shell 不会对单引号字符串执行变量扩展。

Note also that, if you are writing to ~/.bashrc, you should not need sudo. A user should own his own ~/.bashrc. Further, as written, the sudocommand only operated on echo. The redirection >~/.bashrcis done with the user's level of permission. Since echohas no need of and gets no benefit from sudo, sudois a practically a no-op. [Hat tip: tripleee]

另请注意,如果您要写入 ,则~/.bashrc不需要sudo. 用户应该拥有自己的~/.bashrc. 此外,如所写,该sudo命令仅对 进行操作echo。重定向>~/.bashrc是在用户的权限级别下完成的。由于echo不需要也不从中受益sudosudo因此实际上是无操作的。[帽子提示:三重奏]

回答by payam jome yazdian

try

尝试

echo 'export PYTHONPATH=/path/to/caff-dir/python'

Also, you may need to run following:

此外,您可能需要运行以下命令:

pip install -r requirement.txt