Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a directory with a colon to PYTHONPATH?

Tags:

python

bash

shell

The problem is simple:

Using bash, I want to add a directory to my PYTHONPATH for ease of script execution. Unfortunately, the directory I want to use has a : in it. So I try each of the following

export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.com:3344/
export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.com\:3344/
export PYTHONPATH=${PYTHONPATH}:"/home/shane/mywebsite.com:3344/"

None of these work. Every time, the path is created as two separate directories on the path in python. My question is, is it possible to do this for bash? If so, what's the syntax required?

like image 690
Shane Breatnach Avatar asked Nov 17 '08 09:11

Shane Breatnach


2 Answers

The problem is not with bash. It should be setting your environment variable correctly, complete with the : character.

The problem, instead, is with Python's parsing of the PYTHONPATH variable. Following the example set by the PATH variable, it seems there is no escape character at all, so there is no way to make it interpret the : as something other than a separator. You can see it for yourself in the Python interpreter source code.

The only solution is, as several people already mentioned, to use a symlink or something else to allow you to give a colon-less name for your directories.

like image 186
CesarB Avatar answered Sep 19 '22 02:09

CesarB


There is only one you didn't try:

export PYTHONPATH=${PYTHONPATH}:"/home/shane/mywebsite.com\:3344/"

The problem is without the quotes, the escaping is interpreted directly, and converted into a literal ":" in the string. But the ":" needs to be evaluated later.

$ echo "foo:" 
foo:
$ echo \:foo
:foo
$ echo ":foo"
:foo
$ echo "\:foo"
\:foo

I can't guarantee this will fix your python-path problem, but it will get the \ literal into the string.

like image 37
Kent Fredric Avatar answered Sep 21 '22 02:09

Kent Fredric