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

13,469

Solution 1

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.

Solution 2

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.

Solution 3

The OP was trying to add a URL with a port number to a list of file paths. This type of URL is not a file path so python would never find a python file at that location. It doesn't make sense to put a URL with a port number into PYTHONPATH.

Regardless, some people may end up at this question because of the following:

On Windows paths have drive designators followed by a colon, like C:/Python27/lib. In bash on Windows you can add multiple paths to PYTHONPATH with a semicolon like this:

$ export PYTHONPATH="C:\MYPATH1;C:\MYPATH2"
$ python -i
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\MYPATH1', 'C:\\MYPATH2', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin']

Solution 4

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
Share:
13,469
Shane Breatnach
Author by

Shane Breatnach

Updated on June 22, 2022

Comments

  • Shane Breatnach
    Shane Breatnach over 1 year

    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?

  • Svante
    Svante almost 15 years
    So it is a bug in Python: a special character that cannot be escaped.
  • Ishbir
    Ishbir almost 15 years
    @Harleqin: So you don't know what a bug is: something that doesn't work as the specification says.