23

I know you can add multiple locations to python path by separating them by colons ie:

export PYTHONPATH=~/one/location:~/second/location

etc.

I have several locations to add and it looks messy using the above method. Is there a way of adding them in multiple lines? This is what I tried and the last line erases the first.

export PYTHONPATH=~/one/location
export PYTHONPATH=~/second/location

Thanks

Anake
  • 7,201
  • 12
  • 45
  • 59

3 Answers3

31
PYTHONPATH=~/one/location:$PYTHONPATH
PYTHONPATH=~/second/location:$PYTHONPATH
export PYTHONPATH

Note the order here: I've made them so that each has higher precedence than the one before; you could switch what goes on each side of the colon if you want later entries to have lower precedence.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
6

Assuming you are exporting from different files, called one after another:

export PYTHONPATH=~/one/location:${PYTHONPATH}

and

export PYTHONPATH=~/second/location:${PYTHONPATH}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

If you have many such paths, you can store them in array

declare -a array
array[0]=~/one/location 
array[1]=~/two/location 
array[2]=.....
export PYTHONPATH=$(printf "%s:${PYTHONPATH}" ${array[@]})
ghostdog74
  • 327,991
  • 56
  • 259
  • 343