当没有PYTHONPATH时,PYTHONPATH是什么? [英] what's the PYTHONPATH when there is no PYTHONPATH?

查看:188
本文介绍了当没有PYTHONPATH时,PYTHONPATH是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 PYTHONPATH 中添加一个新的目录位置,但问题是我在一个干净的新安装的系统(Linux)中,没有code> PYTHONPATH 尚未定义。我已经阅读并使用了 PYTHONPATH ,我以为我的理解很好,但是我不知道当没有 PYTHONPATH 还存在。

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.

我不能附加到不存在的东西,但是我希望所有重要的图书馆现在仍然可以工作,所以谨慎, code> print str(sys.path)以获取所有标准值。然后我为 PYTHONPATH 定义了一个 env ,包括我刚才发现的所有节点,加上我的新目录。但哇做了很多东西停止工作! Python是如此混乱,新的 env 可变,我不得不删除它,在这一点上,一切都重新起作用。使用错误的 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.

我的问题不像简单的冒号一样简单,或者当我使用冒号时使用分号;我检查了。此外,我的新目录不会导致问题,因为即使没有新的节点仍然出现问题。那么任何人都可以解释为什么这种方法不起作用?

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?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

以下我根据要求提供额外的详细信息,但是我们不需要再阅读任何内容,我认为问题是固定的。 PYTHONPATH中列出的节点不会覆盖所有标准节点,而是成为新的附加条目(以前我相信可以控制先到者)是关键。

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.

从零开始,不定义PYTHONHOME或PYTHONPATH,可以在Python中产生:

Starting from scratch, defining no PYTHONHOME or PYTHONPATH, results in this from within 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   

将其用作PYTHONPATH(即在调用Python之前定义一个env变量),即使没有显式使用Python,也会导致功能非常差的命令提示符。例如:

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:

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

错误是认为PYTHONPATH需要包含所有需要的整个宇宙。是的,我在发布之前做了RTFM,但我想我错过了增加一词的意义。所以采取的建议并不是一切都需要明确的指定 - 那个人可以指定额外添加一个想要的,我试过:

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:

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

所以似乎是工作,虽然我没有' t尝试使用上述postgresql包。为什么在PYTHONPATH中添加丰富的不必要的节点,这样做会让事情发生得如此糟糕,特别是从我应该是可靠的来源sys.path得到条目之后,这有点神秘。

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.

但无论如何,这可能是解决了,所以谢谢!

But anyway, it's probably solved, so THANKS!

推荐答案

不清楚您的问题是什么,但请注意,您不需要将 sys.path 的默认值添加到您的 PYTHONPATH 变量。您在 PYTHONPATH 中放置的目录是要追加的目录;系统默认值附加到您的 PYTHONPATH 中。换句话说,大致来说:

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 = ":".split( os.environ['PYTHONPATH'] ) + sys.path

显示确切的价值 PYTHONPATH ,所得到的错误将有助于我们确定问题。

Showing the exact value of PYTHONPATH and the resultant errors would help us determine the problem.

这篇关于当没有PYTHONPATH时,PYTHONPATH是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆