【问题标题】:How to use a custom site-package using pth-files for Python 2.6?如何使用 Python 2.6 的 pth 文件使用自定义站点包?
【发布时间】:2009-07-21 14:45:37
【问题描述】:

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

按照说明here

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

import os, site; site.addsitedir(os.path.expanduser('~/lib/python2.6'))

此外,我的主目录 (C:\Users\wierob) 有一个 pydistutils.cfg,其中包含:

[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 文件或将路径添加到另一个包的现有 pth 文件不起作用。

标签: python


【解决方案1】:

如果使用带有 BOM 的 UTF-8 编码,pth 文件似乎会被忽略。

在没有 BOM 的情况下以 ANSI 或 UTF-8 保存 pth 文件。

【讨论】:

    【解决方案2】:

    根据documentation,您应该输入 .pth 文件的路径,以便输入:

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

    会起作用

    【讨论】:

    • 对我不起作用。文档还指出“以导入开头的行(后跟空格或制表符)被执行。” -- 所以在 pth 文件中执行 Python 代码应该可以工作。
    猜你喜欢
    • 2013-11-23
    • 2017-09-11
    • 2013-02-18
    • 2010-10-16
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    相关资源
    最近更新 更多