tags:

views:

2140

answers:

4

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?

A: 

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.

Kent Fredric
+1  A: 

I don't know if what you want is directly possible, but a workaround if you are using a linux filesystem would be to create a symlink to your "coloned" directory and add this symlink to your PYTHONPATH like this:

ln -s /home/shane/mywebsite.com\:3344 /home/shane/mywebsite.3344
export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.3344
Terje Mikal
A: 

The symlink hack is probably the only viable option, unless there is some heuristic to determine how to handle colons in PYTHONPATH.

JesperE
+4  A: 

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.

CesarB
So it is a bug in Python: a special character that cannot be escaped.
Svante
@Harleqin: So you don't know what a bug is: something that doesn't work as the specification says.
ΤΖΩΤΖΙΟΥ