Search code examples
pythonubuntufish

How can I set PYTHONPATH in fish?


The following works in bash:

~$ echo $PYTHONPATH
<nothing>
~$ export PYTHONPATH=/path/to/test/folder
~$ echo $PYTHONPATH
/path/to/test/folder
~$ python -m test_script
hello world

But not in fish:

Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
~> echo $PYTHONPATH
<nothing>
~> set --universal PYTHONPATH /path/to/test/folder
~> echo $PYTHONPATH
/path/to/test/folder
~> python -m test_script
/usr/bin/python: No module named test_script

I've tried set, set --global and set --universal.

How can I set PYTHONPATH in fish? (If it matters, I'm running Ubuntu 14.04.)


Solution

  • Setting the variable is not sufficient, you must export it too (as you do in bash). Exporting means that subprocesses (like the Python call) will get the value too.

    From the fish documentation:

    -x or --export causes the specified environment variable to be exported to child processes

    So a direct equivalent to the bash directive would be:

    ~> set --export PYTHONPATH /path/to/test/folder
    

    You could add --universal or other arguments, but here the value is temporary, which matches the original bash example.

    (Found answer via a similar question.)