如何使用Python2.6的pth文件使用自定义站点包?

如何使用Python2.6的pth文件使用自定义站点包?,python,Python,我正在尝试设置一个自定义站点包目录(WindowsVista上的Python2.6)。例如,目录应该是“~\lib\python2.6”(C:\Users\wierob\lib\python2.6)。因此,调用'setup.py install'应该将包复制到C:\Users\wierob\lib\python2.6 按照说明: 我在Python安装的site packages目录中创建了一个pth文件(C:\Python26\Lib\site packages)。此文件包含一行: import

我正在尝试设置一个自定义站点包目录(WindowsVista上的Python2.6)。例如,目录应该是“~\lib\python2.6”(C:\Users\wierob\lib\python2.6)。因此,调用'setup.py install'应该将包复制到C:\Users\wierob\lib\python2.6

按照说明:

我在Python安装的site packages目录中创建了一个pth文件(C:\Python26\Lib\site packages)。此文件包含一行:

import os, site; site.addsitedir(os.path.expanduser('~/lib/python2.6'))
此外,我还有一个pydistutils.cfg我的主目录(C:\Users\wierob),其中包含:

[install]
install_lib = ~/lib/python2.6
install_scripts = ~/bin
运行“setup.py install”时,我收到以下错误消息:

C:\Users\wierob\Documents\Python\workspace\rsreader>setup.py install
running install
Checking .pth file support in C:\Users\wierob\lib\python2.6\
C:\Python26\pythonw.exe -E -c pass
TEST FAILED: C:\Users\wierob\lib\python2.6\ does NOT support .pth files
error: bad install directory or PYTHONPATH

You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    C:\Users\wierob\lib\python2.6\
因此,似乎pth文件不起作用。虽然,如果我进去

site.addsitedir(os.path.expanduser('~/lib/python2.6'))
在交互式python会话中,目录成功添加到sys.path

有什么想法吗?谢谢。

根据,您应该将路径放在.pth文件中,以便输入:

C:\Users\wierob\lib\python2.6

将起作用

如果使用BOM以UTF-8编码,则pth文件似乎被忽略


将pth文件保存为ANSI或UTF-8而不保存BOM是可行的。

对我不起作用。文档中还声明“将执行以import开头的行(后跟空格或制表符)”——因此在pth文件中执行Python代码应该可以。另外,将路径直接添加到pth文件或将路径添加到另一个包的现有pth文件也不起作用。