I need to add a new directory location to my PYTHONPATH
, but the problem is I'm on a clean, newly-installed system (Linux) where no PYTHONPATH
has yet been defined. I've read about and used PYTHONPATH
and I thought I understood it quite well, but I do not know what's happening when no PYTHONPATH
yet exists.
我需要在PYTHONPATH中添加一个新的目录位置,但问题是我在一个干净的,新安装的系统(Linux)上,还没有定义PYTHONPATH。我已经阅读并使用了PYTHONPATH,我认为我理解得很清楚,但是当不存在PYTHONPATH时我不知道发生了什么。
I can't append to something that doesn't exist, but I want all important libraries currently found to still work, so being cautious, from within Python I did print str(sys.path)
to get all the standard values. Then I defined an env
-variable for PYTHONPATH
including all the nodes I had just found, plus my new directory. But wow did a lot of stuff stop working! Python is so messed up with the new env
-variable that I had to remove it, at which point everything worked again. With the bad PYTHONPATH
the system was so confused it couldn't even find an error message to display when an incorrect command was typed in at the prompt.
我不能附加不存在的东西,但是我希望当前发现的所有重要库仍然有用,所以要谨慎,从Python中我打印str(sys.path)来获取所有标准值。然后我为PYTHONPATH定义了一个env变量,包括我刚刚找到的所有节点,以及我的新目录。但哇做了很多东西停止工作! Python对于我必须删除它的新env变量如此混乱,此时一切都再次起作用。对于糟糕的PYTHONPATH,系统非常混乱,甚至在提示输入错误的命令时甚至找不到要显示的错误消息。
My problem is not something simple like a missing colon, or using semi-colons when I should use colons; I checked. Also my new directory doesn't cause the problem because even without the new node the problems still occur. So can anyone explain why this approach doesn't work?
我的问题不是像缺少结肠那样简单,或者当我应该使用冒号时使用分号;我检查了。此外,我的新目录不会导致问题,因为即使没有新节点,问题仍然会发生。那么有谁可以解释为什么这种方法不起作用?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Below I provide extra details as requested, but one need not read any futher, I think the problem is fixed. The explanation that the nodes listed in PYTHONPATH do not override all "standard" nodes but rather become new, additional entries (prepended I believe, so one can control what comes first) was the key.
下面我按要求提供额外的细节,但不需要再阅读,我认为问题是固定的。解释PYTHONPATH中列出的节点不会覆盖所有“标准”节点,而是成为新的,附加条目(我相信,所以人们可以控制首先出现的内容)是关键。
Starting from scratch, defining no PYTHONHOME or PYTHONPATH, results in this from within Python:
从头开始,不定义PYTHONHOME或PYTHONPATH,从Python中产生:
print ':'.join(sys.path)
:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/python2.7/dist-packages/ubuntu-sso-client
Using this as a PYTHONPATH (i.e., defining an env-variable before invoking Python), results in a very poorly functioning command prompt, even without explicitly using Python. For example:
使用它作为PYTHONPATH(即,在调用Python之前定义一个env变量),即使没有显式使用Python,也会导致命令提示功能非常糟糕。例如:
$> export PYTHONPATH='/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/python2.7/dist-packages/ubuntu-sso-client'
$> echo $PYTHONPATH
/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/python2.7/dist-packages/ubuntu-sso-client
$> IntentionalBadCommand
Fatal Python error: Py_Initialize: Unable to get the locale encoding
File "/usr/lib/python2.7/encodings/__init__.py", line 123
raise CodecRegistryError,\
^
SyntaxError: invalid syntax
Aborted
The mistake was thinking that PYTHONPATH needs to contain the whole universe of everything needed. Yes, I did RTFM before posting, but I guess I missed the significance of the beginning word "Augment". So taking the advice that not everything needs to be explicitly specified -- that one can just specify the extra additions one wants, I tried:
错误在于认为PYTHONPATH需要包含所需的全部宇宙。是的,我在发帖前做过RTFM,但我想我错过了开头词“Augment”的重要性。因此,采取不一切都需要明确指定的建议 - 一个人可以只指定想要的额外添加,我试过:
$> export PYTHONPATH=/usr/lib/python2.7/dist-packages/postgresql-pkg
$> echo $PYTHONPATH
/usr/lib/python2.7/dist-packages/postgresql-pkg
$> IntentionalBadCommand
IntentionalBadCommand: command not found
So it seems to be working, though I haven't yet tried to use the postgresql package mentioned above. Still it's a little mysterious why prepending an abundance of unnecessary nodes to the PYTHONPATH would make things break as badly as it did, especially since I got the entries from what should be a reliable source: sys.path.
所以它似乎工作,虽然我还没有尝试使用上面提到的postgresql包。仍然有点神秘的是为什么在PYTHONPATH中添加大量不必要的节点会使事情像它一样严重破坏,特别是因为我从应该是可靠的源:sys.path获得条目。
But anyway, it's probably solved, so THANKS!
但无论如何,它可能已经解决了,所以谢谢!
2 个解决方案
#1
7
It's not clear what your problem could be, but note that you don't need to add the default value of sys.path
to your PYTHONPATH
variable. The directories you put in PYTHONPATH
are additional directories to search; the system default is appended to your PYTHONPATH
. In other words, roughly speaking:
目前尚不清楚您的问题是什么,但请注意,您不需要将sys.path的默认值添加到PYTHONPATH变量中。您在PYTHONPATH中放置的目录是要搜索的其他目录;系统默认值将附加到您的PYTHONPATH。换句话说,粗略地说:
sys.path = ":".split( os.environ['PYTHONPATH'] ) + sys.path
Showing the exact value of PYTHONPATH
and the resultant errors would help us determine the problem.
显示PYTHONPATH的确切值以及由此产生的错误将有助于我们确定问题。
#2
1
On Unix Systems, it should translate to /usr/local/lib/python** where ** is Python version...Like 2.7 and so on...
在Unix系统上,它应该转换为/ usr / local / lib / python **,其中**是Python版本......比如2.7等等......
Your answer actually lies in the definition of PYTHONPATH and PYTHONHOME variables.
你的答案实际上在于PYTHONPATH和PYTHONHOME变量的定义。
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONHOME
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.
默认搜索路径是依赖于安装的,但通常以prefix / lib / pythonversion开头(参见上面的PYTHONHOME)。它总是附加到PYTHONPATH。
I would suggest you also try this
我建议你也尝试这个
import sys
sys.path.append('Directory you wanna add to the path')
Hope this helps.
希望这可以帮助。
#1
7
It's not clear what your problem could be, but note that you don't need to add the default value of sys.path
to your PYTHONPATH
variable. The directories you put in PYTHONPATH
are additional directories to search; the system default is appended to your PYTHONPATH
. In other words, roughly speaking:
目前尚不清楚您的问题是什么,但请注意,您不需要将sys.path的默认值添加到PYTHONPATH变量中。您在PYTHONPATH中放置的目录是要搜索的其他目录;系统默认值将附加到您的PYTHONPATH。换句话说,粗略地说:
sys.path = ":".split( os.environ['PYTHONPATH'] ) + sys.path
Showing the exact value of PYTHONPATH
and the resultant errors would help us determine the problem.
显示PYTHONPATH的确切值以及由此产生的错误将有助于我们确定问题。
#2
1
On Unix Systems, it should translate to /usr/local/lib/python** where ** is Python version...Like 2.7 and so on...
在Unix系统上,它应该转换为/ usr / local / lib / python **,其中**是Python版本......比如2.7等等......
Your answer actually lies in the definition of PYTHONPATH and PYTHONHOME variables.
你的答案实际上在于PYTHONPATH和PYTHONHOME变量的定义。
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONHOME
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.
默认搜索路径是依赖于安装的,但通常以prefix / lib / pythonversion开头(参见上面的PYTHONHOME)。它总是附加到PYTHONPATH。
I would suggest you also try this
我建议你也尝试这个
import sys
sys.path.append('Directory you wanna add to the path')
Hope this helps.
希望这可以帮助。