在子目录中添加 tweepy 时导入错误

发布于 2024-10-23 23:19:56 字数 976 浏览 3 评论 0原文

我正在尝试将 tweepy 打包到子目录中。但我无法让进口工作。

情况是这样的:

import socialmedia
import tweepy
import logging


# This file is in socialmedia / twitter / __init__.py
# Tweepy is located in socialmedia / twitter / tweepy / __init__.py

# I am getting this error: 
"""
Traceback (most recent call last):
  File "/home/samos/workspace/socialmedia-api/src/test/test.py", line 1, in <module>
    from socialmedia.twitter import TwitterAPI
  File "/home/samos/workspace/socialmedia-api/src/socialmedia/twitter/__init__.py", line 5, in <module>
    from socialmedia.twitter import tweepy
  File "/home/samos/workspace/socialmedia-api/src/socialmedia/twitter/tweepy/__init__.py", line 12, in <module>
    from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, ModelFactory
ImportError: No module named tweepy.models
"""

我已经尝试过不使用 init.py 并使用 twitter.py,所以这似乎不是问题。 tweepy 的导入似乎也有效,但 tweepy 内部的导入效果不佳。

I am trying to package tweepy in a subdirectory. But I can't get the imports working.

This is the case:

import socialmedia
import tweepy
import logging


# This file is in socialmedia / twitter / __init__.py
# Tweepy is located in socialmedia / twitter / tweepy / __init__.py

# I am getting this error: 
"""
Traceback (most recent call last):
  File "/home/samos/workspace/socialmedia-api/src/test/test.py", line 1, in <module>
    from socialmedia.twitter import TwitterAPI
  File "/home/samos/workspace/socialmedia-api/src/socialmedia/twitter/__init__.py", line 5, in <module>
    from socialmedia.twitter import tweepy
  File "/home/samos/workspace/socialmedia-api/src/socialmedia/twitter/tweepy/__init__.py", line 12, in <module>
    from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, ModelFactory
ImportError: No module named tweepy.models
"""

I already tried not using the init.py and using twitter.py, so this doesn't seem to be the problem. It also seems that the import of tweepy is working, but the imports inside tweepy are not working well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-10-30 23:19:56

看起来 tweepy 期望位于 Python 路径上 - 它尝试加载 tweepy.models 。但是,由于您将 tweepy 放在子目录中,因此 models 模块现在位于 socialmedia.twitter.tweepy.models

您要么必须将 Socialmedia/twitter/ 添加到 Python 路径,要么必须更改 tweepy 的导入以补偿新的包结构。这两种解决方案都不是很好。前者引入了必须设置的特殊配置。后者将要求您在更新 tweepy 代码时修复导入(因为更新的代码将包含原始的 tweepy.whatever 导入)。这就是为什么像您这样移动包通常不是一个好主意。相反,只需像平常一样安装它(运行 setup.py 或 easy_install,或您喜欢的任何方法),然后在使用该包的代码中导入它即可。

除非您绝对必须移动您描述的目录结构,否则我会正常安装软件包。如果你不这样做,你将面临一场艰苦的战斗。否则,

Looks like tweepy expects to be on the Python path--it tries to load tweepy.models. However, since you put tweepy in a subdirectory, the models module is now located at socialmedia.twitter.tweepy.models.

You either have to add socialmedia/twitter/ to the Python path, or you have to change tweepy's imports to compensate for the new package structure. Neither solution is great. The former introduces a special configuration that must be set. The latter will require you to fix the imports any time the tweepy code is updated (because the updated code will contain the original tweepy.whatever imports). This is why it is generally not a good idea to move packages around like you're doing. Instead, just install it as normal (run setup.py or easy_install, or whatever method you prefer) and then in the code that uses the package just import it.

Unless you absolutely must move have the directory structure you describe, I would just install packages normally. You're fighting an uphill battle to do otherwise. Otherwise,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文