Reputation: 954
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?
Upvotes: 8
Views: 17265
Reputation: 2197
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']
Upvotes: 3
Reputation: 45575
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.
Upvotes: 11
Reputation: 64434
The symlink hack is probably the only viable option, unless there is some heuristic to determine how to handle colons in PYTHONPATH.
Upvotes: 0
Reputation: 947
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
Upvotes: 1
Reputation: 57374
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.
Upvotes: 1