在我的.bash_profile中正确格式化PYTHONPATH [英] Correctly formatting a PYTHONPATH in my .bash_profile

查看:53
本文介绍了在我的.bash_profile中正确格式化PYTHONPATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 PYTHONPATH 添加到我的.bash_profile中,但想检查我是否以正确的方式进行操作.我的 .bash_profile (没有 PYTHONPATH )看起来像:

 #.bash_profile#获取别名和函数如果[-f〜/.bashrc];然后.〜/.bashrc科幻#用户特定的环境和启动程序PATH = $ PATH:$ HOME/.local/bin:$ HOME/bin:/home/user/condor/bin:/home/user/merlin/bin导出路径 

我想添加到我的PYTHONPATH的路径是:

 /home/user/merlin/bin/strats/ 

因此,我更新的.bash_profile(带有PYTHONPATH)看起来像:

 #.bash_profile#获取别名和函数如果[-f〜/.bashrc];然后.〜/.bashrc科幻#用户特定的环境和启动程序PATH = $ PATH:$ HOME/.local/bin:$ HOME/bin:/home/user/condor/bin:/home/user/merlin/bin导出路径导出PYTHONPATH =/home/user/merlin/bin/strats/ 

如何正确格式化?

解决方案

如果您希望成为交互式登录Shell中有关 PYTHONPTAH 环境变量内容的唯一所有者和决策者,做对了:

〜/.bash_profile 〜/.profile

  export PYTHONPATH =/home/user/merlin/bin/strats/ 

如果您想继承 PYTHONPATH 环境变量的任何系统范围的设置,则应该:

〜/.bash_profile 〜/.profile

  export PYTHONPATH = $ PYTHONPATH:/home/user/merlin/bin/strats/ 

请注意,如果您正在一个无需登录即可启动新终端的系统(即:在Linux桌面上启动新的 xterm ),或者在需要该特定终端的情况下通过cron运行脚本的环境变量,不会执行 .bash_profile ,因此该脚本将无法使用环境变量.

如本答复注释中所述,您可以选择使用 ./〜profile 文件而不是〜/.bash_profile ,以具有与其他shell的其他兼容性./p>

有些人只是将所有环境配置添加到〜/.bashrc 中.由于您的 .bash_profile 模板调用〜/.bashrc ,您最终将在交互式登录和非登录外壳程序中获得这些环境变量.

对于通过cron运行的脚本,您应该直接在脚本本身或cron行上获取具有环境配置的文件,因为这不会自动为您完成(crontab会启动非交互式shell来运行脚本以及这些脚本不受〜/.bashrc 〜/.bash_profile 〜/.profile 的影响).

I would like to add a PYTHONPATH to my .bash_profile but would like to check I'm doing this the correct way. My .bash_profile (without a PYTHONPATH) looks like:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/user/condor/bin:/home/user/merlin/bin

export PATH

The path I would like to add to my PYTHONPATH is:

/home/user/merlin/bin/strats/

Therefore would my updated .bash_profile (with PYTHONPATH) looks like:

    # .bash_profile

    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi

    # User specific environment and startup programs

    PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/user/condor/bin:/home/user/merlin/bin

    export PATH

    export PYTHONPATH=/home/user/merlin/bin/strats/

How can I correctly format this?

解决方案

If it's your wish to be the sole owner and decision maker regarding PYTHONPTAH environment variable content on your interactive login shells, you're doing it right:

~/.bash_profile or ~/.profile

export PYTHONPATH=/home/user/merlin/bin/strats/

If you'd like to inherit any system-wide setting for PYTHONPATH environment variable, then you should:

~/.bash_profile or ~/.profile

export PYTHONPATH=$PYTHONPATH:/home/user/merlin/bin/strats/

Be aware that if you're working in a system where you can launch new terminals without logging in (i.e: launching a new xterm on your linux desktop), or in case you need that specific environment variable to run a script via cron, .bash_profile won't be executed and therefore the environment variable won't be available to that script.

As discussed in this answer comments, you can choose to use the ./~profile file instead of ~/.bash_profile to have additional compatibility with other shells.

Some folks simply add all environment configuration in ~/.bashrc. Since your .bash_profile template call ~/.bashrc, you'd end up having those environment variables available in interactive login and non-login shells.

For scripts that run via cron, you should directly source the file where you have your environment configuration on the script itself or on the cron line because that won't be done automatically for you (crontab launches non-interactive shells to run the scripts and these are not affected by ~/.bashrc, ~/.bash_profile or ~/.profile).

这篇关于在我的.bash_profile中正确格式化PYTHONPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆