Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 854
Stacey Avatar asked Oct 18 '25 18:10

Stacey


1 Answers

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

like image 132
Alexandre Juma Avatar answered Oct 20 '25 09:10

Alexandre Juma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!