Stacey
Stacey

Reputation: 5107

Correctly formatting a PYTHONPATH in my .bash_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?

Upvotes: 4

Views: 10711

Answers (1)

Alexandre Juma
Alexandre Juma

Reputation: 3323

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).

Upvotes: 3

Related Questions