bash 如何将带有冒号的目录添加到 PYTHONPATH?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/295195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a directory with a colon to PYTHONPATH?
提问by Shane Breatnach
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
使用 bash,我想向我的 PYTHONPATH 添加一个目录以方便脚本执行。不幸的是,我想使用的目录中有一个 : 。所以我尝试以下每一个
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?
这些都不起作用。每次,该路径在 python 路径上创建为两个单独的目录。我的问题是,是否可以为 bash 执行此操作?如果是这样,需要什么语法?
回答by CesarB
The problem is not with bash. It should be setting your environment variable correctly, complete with the :character.
问题不在于bash。它应该正确设置您的环境变量,并带有:字符。
The problem, instead, is with Python's parsing of the PYTHONPATHvariable. Following the example set by the PATHvariable, 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.
相反,问题在于 Python 对PYTHONPATH变量的解析。按照PATH变量设置的示例,似乎根本没有转义字符,因此无法将其解释:为分隔符以外的其他内容。您可以在Python 解释器源代码中亲自查看。
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.
正如一些人已经提到的,唯一的解决方案是使用符号链接或其他东西来允许您为目录提供一个无冒号的名称。
回答by Kent Fredric
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.
我不能保证这会解决您的 python 路径问题,但它会将 \ 文字放入字符串中。
回答by nmgeek
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.
OP 试图将带有端口号的 URL 添加到文件路径列表中。这种类型的 URL 不是文件路径,因此 python 永远不会在该位置找到 python 文件。将带有端口号的 URL 放入 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:
在 Windows 路径上,驱动器指示符后跟一个冒号,例如C:/Python27/lib. 在 Windows 上的 bash 中,您可以使用如下分号向 PYTHONPATH 添加多个路径:
$ 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']
回答by Terje Mikal
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:
我不知道您想要的是否可以直接实现,但是如果您使用的是 linux 文件系统,则解决方法是创建一个指向“coloned”目录的符号链接,然后将此符号链接添加到您的 PYTHONPATH 中,如下所示:
ln -s /home/shane/mywebsite.com\:3344 /home/shane/mywebsite.3344
export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.3344
回答by JesperE
The symlink hack is probably the only viable option, unless there is some heuristic to determine how to handle colons in PYTHONPATH.
符号链接 hack 可能是唯一可行的选择,除非有一些启发式方法来确定如何在 PYTHONPATH 中处理冒号。

