如何在PYTHONPATH中添加带冒号的目录?

时间:2023-01-19 23:11:08

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做这个?如果是这样,需要什么语法?

5 个解决方案

#1


8  

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 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.

相反,问题在于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.

正如几个人已经提到的,唯一的解决方案是使用符号链接或其他东西来允许您为目录提供无冒号的名称。

#2


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.

我不能保证这会修复你的python-path问题,但它会将\ literal放入字符串中。

#3


1  

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

#4


0  

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

符号链接hack可能是唯一可行的选项,除非有一些启发式方法来确定如何处理PYTHONPATH中的冒号。

#5


0  

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']

#1


8  

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 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.

相反,问题在于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.

正如几个人已经提到的,唯一的解决方案是使用符号链接或其他东西来允许您为目录提供无冒号的名称。

#2


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.

我不能保证这会修复你的python-path问题,但它会将\ literal放入字符串中。

#3


1  

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

#4


0  

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

符号链接hack可能是唯一可行的选项,除非有一些启发式方法来确定如何处理PYTHONPATH中的冒号。

#5


0  

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']