【问题标题】:"import site' failed; use -v for traceback" - mac os lion, python 2.7, subprocess“导入站点”失败;使用 -v 进行回溯” - mac os lion,python 2.7,子进程
【发布时间】:2012-01-27 22:51:38
【问题描述】:

我正在尝试在 python 中运行一个子进程,这是我的代码的一部分:

def update(self):
        currentTime = strftime("%d.%m.%y %H:%M", gmtime()) #strftime("%d-%m-%y %H:%M", gmtime)
        resultString = "======== " + currentTime + " ========\n\n"  
        bzrMergeCommand = "cd %s ; /usr/local/bin/bzr merge" % self._directoryName
        print "Getting the updated code from bzr..."
        mergeResult = sp.Popen(bzrMergeCommand, shell=True, stdout=sp.PIPE, stderr=sp.PIPE, cwd= self._directoryName)

        communicated = mergeResult.communicate()

但它无法运行,这是我得到的异常:

 'import site' failed; use -v for traceback Traceback (most recent call
 last):   File "/usr/local/bin/bzr", line 21, in <module>
     import os   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py",
 line 398, in <module>
     import UserDict   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py",
 line 84, in <module>
     _abcoll.MutableMapping.register(IterableUserDict)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py",
 line 109, in register
     if issubclass(subclass, cls):   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py",
 line 151, in __subclasscheck__
     if subclass in cls._abc_cache:   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py",
 line 69, in __contains__
     return ref(item) in self.data TypeError: cannot create weak reference to 'classobj' object

我在 Google 上搜索并阅读了很多关于“TypeError:无法创建对 'classobj' 对象的弱引用”: https://stackoverflow.com/questions/7753181/making-my-python-script-executable-causes-a-import-site-failed-use-v-for-tra

这里:https://github.com/pypa/virtualenv/issues/108

有什么想法吗?

【问题讨论】:

  • 脚本是否在 cli 中工作?
  • 是的!不知道我之前没试过。如果可以的话,我会给50分。非常感谢!

标签: python macos subprocess bazaar


【解决方案1】:

看来问题出在/usr/local/bin/bzr 而不是您的脚本。尝试从命令行运行 bzrMergeCommand,即不使用脚本。你应该得到同样的错误。

尝试按照链接的 StackOverflow 问题中的建议编辑 /usr/local/bin/bzr 中的 shebang。

【讨论】:

    【解决方案2】:

    引发的错误来自 bzr,而不是您的脚本。尝试运行 python 并输入 import site 如果失败,您的 python 安装可能有问题。

    此外,作为一般规则,除非您有理由在 Popen 中设置 shell=True,否则最好设置 shell=False。

    【讨论】:

    • shell=False 给出:raise child_exception OSError: [Errno 2] No such file or directory 另外,我从命令行运行它,没问题。
    • @ron:那是因为如果 shell=True,它将启动您的默认 shell 作为子进程并将字符串 bzrMergeCommand 作为参数传递给它。如果您设置 shell=False,那么它会查找一个名为 bzrMergeCommand 的值的文件(不是程序,它是 bzrMergeCommand 中的第一个单词)来执行。尝试 shell=False 并将 bzrMergeCommand 拆分为一个列表,例如: os.chdir(self._directoryName);bbzrMergeCommand = ["/usr/local/bin/bzr","merge"] 然后子进程将在 dirName 和将使用参数 merge 启动 bzr。 (这将使您留在新目录中。)
    • 我在 Windows 上尝试从不同版本导入库时看到此错误。你安装了哪些版本的python?哪个是默认值?如果它正在启动 2.6 以运行 bzr,并尝试从 2.7 导入站点,它将失败。另外,bzr 在导入 os 时失败,所以在 python shell 中,尝试导入 os 并确保它可以正常工作。
    • 我尝试从 CLI 运行脚本。有用。所以问题出在eclipse上?谢谢。
    【解决方案3】:

    正如其他人所指出的,问题出在 Bazaar 本身 (/usr/local/bin/bzr),而不是您的脚本。

    根据Bazaar website,您需要调整 Bazaar 以在 OS X Lion 上使用 Python 2.6:

    注意:要在 OS X Lion (10.7) 中使用 Bazaar,您应该更改版本 'bzr' 脚本使用的 Python 到 2.6。你可以用一个来做到这一点 终端中的命令:

    sudo sed -i '' s,/usr/bin/python,/usr/bin/python2.6, /usr/local/bin/bzr

    【讨论】:

    • 这是我在你的建议后得到的错误:/bin/sh: /usr/local/bin/bzr: /usr/bin/python2.62.6: bad interpreter: No such file or directory跨度>
    • 看起来您可能已经 a) 已经运行了这个命令,或者 b) 在这个会话中成功地运行了两次? (或 c)从已经硬连线以使用 python2.6 的源安装 Bazaar。)无论如何,您最终将 /usr/bin/python2.6 中的 /usr/bin/python 替换为/usr/bin/python2.6,导致 /usr/bin/python/2.62.6 显然不存在。要修复它,请尝试:sudo sed -i '' s,/usr/bin/python2.62.6,/usr/bin/python2.6, /usr/local/bin/bzr
    猜你喜欢
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 2013-02-12
    • 2011-10-10
    • 2012-11-25
    相关资源
    最近更新 更多