【问题标题】:Specifying Tweepy API with sys.path.append( )使用 sys.path.append() 指定 Tweepy API
【发布时间】:2012-06-21 02:23:16
【问题描述】:

设置 OSX 10.7.4 Eclipse Indigo、Pydev、Tweepy-1.9

我认为这应该相对简单,但我不断收到错误消息。我希望使用 sys.path.append() 指定 Tweepy API 但是 PyDev 不断向我显示“未解决的导入:tweepy”错误

我的代码

import sys
sys.path.append('/Applications/tweepy-1.9')
import tweepy

this 上有几个类似的帖子,我看过,但它们似乎给出了关于是否链接到顶级文件或目录中的特定文件的相互矛盾的建议。

我的问题是我应该像现在这样直接链接到顶级文件夹 (/Applications/tweepy-1.9) 还是链接到该文件夹​​中的特定文件? Machaku 向我提供了一些关于 related question 的信息,说我可以两者都做,但我必须创建一个名为“init.py”的文件并链接到它。

我都试过了,但似乎都没有。

任何建议都非常受欢迎


错误

Traceback (most recent call last):
  File "/Users/brendan/Documents/workspace/Tweeter/src/rate_limit.py", line 13, in <module>
    print api.rate_limit_status()
  File "build/bdist.macosx-10.5-fat3/egg/tweepy/binder.py", line 185, in _call
    return method.execute()
  File "build/bdist.macosx-10.5-fat3/egg/tweepy/binder.py", line 149, in execute
    raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: [Errno 61] Connection refused

tweepy-1.9 文件结构

  • tweepy-1.9

    • 构建

      • bdist.macosx-10.5-fat3
        • tweepy
          • init.py (“init”两边各有2个下划线)
          • api.py
          • auth.py
          • binder.py
          • cache.py
          • cursor.py
          • error.py
          • models.py
          • oauth.py
          • parsers.py
          • streaming.py
          • utils.py
    • 距离

      • tweepy-1.9-py2.7.egg
    • PKG-INFO
    • 自述文件
    • setup.cfg
    • tweepy
      • init.py (“init”两边各有2个下划线)
      • api.py
      • auth.py
      • binder.py
      • cache.py
      • cursor.py
      • error.py
      • models.py
      • oauth.py
      • parsers.py
      • streaming.py
      • utils.py
    • tweepy.egg 信息
      • dependency_links.txt
      • PKG-信息
      • SOURCES.txt
      • top_level.txt
      • zip_safe

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    根据堆栈跟踪,找到了您的 Tweepy 库,因此您的 sys.path.append 已经工作。但是,如果您阅读最后一行,

    tweepy.error.TweepError: Failed to send request: [Errno 61] Connection refused

    错误似乎是库无法连接到 twitter 服务。

    关于在sys.path 中放入什么,它应该是包含顶级包所在目录的文件夹。例如,如果我有以下结构

    src
     | my_package
     |     | __init__.py
     |     | module_1.py
     |     | module_2.py
    

    我希望能够做到from my_package import module_1 然后sys.path 必须包含src 的绝对路径。此外,必须存在 __init__.py 文件才能使 my_package 成为 Python 包。

    但是,动态更新 sys.path 绝对不是让其他 Python 程序可以访问 Python 包和模块的推荐方法。

    有更好的解决方案:

    • 调用python setup.py install,按照Tweepy中的建议install guide(最好使用virtualenv

    • 更新 PYTHONPATH 以包含 Tweety 库的根路径。

    • 使用由site module读取的.pth文件

    您可以在Installing Python Modules 上的 Python 文档中找到更多详细信息,尤其是 Modifying Python's search path section

    【讨论】:

    • 您好,谢谢您的回复。当我可以访问代码时,我会在早上检查一下。我将此标记为已回答,因为您已表明它正在连接。我想像我在这里所做的那样连接到顶级文件夹。
    • @Deepend 我已经添加了一些解释,说明sys.path 中应该存在哪个文件夹,以便随后进行导入。
    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2014-02-14
    • 2012-06-13
    • 2018-07-03
    相关资源
    最近更新 更多