Python 无法导入 tweepy 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31431002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:59:11  来源:igfitidea点击:

Unable to import tweepy module

pythontweepy

提问by Jake

I am new to installing new python modules.

我是安装新 python 模块的新手。

I installed tweepy using pip install tweepy. The installation was successful and 2 folders tweepy & tweepy-3.3.0.dist-info are created in the Lib/site-packages, hence I assumed all should be fine.

我使用 pip install tweepy 安装了 tweepy。安装成功,在 Lib/site-packages 中创建了 2 个文件夹 tweepy 和 tweepy-3.3.0.dist-info,因此我认为一切都应该没问题。

However, when I went to the IDE and import tweepy. It is unable to detect the module:

但是,当我去 IDE 并导入 tweepy 时。无法检测到模块:

>>> import tweepy
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: No module named tweepy

What is wrong?

怎么了?

I am running python 2.7.5.

我正在运行 python 2.7.5。

[Update 1]I am using windows 7.

[更新 1]我使用的是 Windows 7。

I first installed pip using another forum's suggestion (How do I install pip on Windows?). basically saving the get-pip.py script and double clicking it (unable to get "python get-pip.py" to work in cmd prompt as suggested). Then, I went to cmd and nagivated to C:/Python27/Scripts and type in pip install tweepy. I remembered seeing the result as a successful installation.

我首先使用另一个论坛的建议安装了 pip如何在 Windows 上安装 pip?)。基本上是保存 get-pip.py 脚本并双击它(无法让“python get-pip.py”按照建议在 cmd 提示符下工作)。然后,我转到 cmd 并导航到 C:/Python27/Scripts 并输入 pip install tweepy。我记得看到结果是安装成功。

[Update 2]Using a file with import tweepy and running it, I have a similar error.

[更新 2]使用带有 import tweepy 的文件并运行它,我遇到了类似的错误。

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\xxxx\Desktop\Script1.py", line 2, in <module>
    from tweepy import Stream
ImportError: No module named tweepy

[Update 3]Typed "pip freeze" in cmd. It does show tweepy=3.3.0

[更新 3]在 cmd 中输入“pip freeze”。它确实显示 tweepy=3.3.0

C:\Python27\Scripts>pip freeze 
oauthlib==0.7.2 
requests==2.7.0 
requests-oauthlib==0.5.0 
six==1.9.0 
tweepy==3.3.0 
wheel==0.24.0

[Answer]Thanks for all the help guys, especially Cleb & omri_saadon suggestion that there might be something wrong with the file path.

[Answer]感谢所有帮助的人,尤其是 Cleb & omri_saadon 建议文件路径可能有问题。

I just realised that my GIS software, ArcGIS by default installed another Python into the Python27 folder, and everything is taken from that folder, C:\Python27\ArcGIS10.2, instead of C:\Python27. After I install tweepy from C:\Python27\ArcGIS10.2\Scripts, everything works well.

我刚刚意识到我的 GIS 软件 ArcGIS 默认将另一个 Python 安装到 Python27 文件夹中,并且所有内容都来自该文件夹 C:\Python27\ArcGIS10.2,而不是 C:\Python27。从 C:\Python27\ArcGIS10.2\Scripts 安装 tweepy 后,一切正常。

采纳答案by omri_saadon

Try to pip uninstall tweepy

尝试 pip uninstall tweepy

and then again pip install tweepy

然后再 pip install tweepy

Make sure you don't have several interpreters on your computer, if you have several, make sure that your pycharm(or any other editor you use) is configured with the same interpreter where you installed tweepy.

确保您的计算机上没有多个解释器,如果有多个,请确保您的 pycharm(或您使用的任何其他编辑器)配置了与安装 tweepy 相同的解释器。

回答by user3291173

If you are using Jupyter notebook on Anaconda try:

如果您在 Anaconda 上使用 Jupyter Notebook,请尝试:

 sudo conda install tweepy

This worked for me on OSX.

这在 OSX 上对我有用。

回答by Alekz7

The same for me, typing direct in the console import tweepy it works, but when I tried to run from the script it says 'No module named 'tweepy'' i tried to uninstall and install again but the solution was more simple, instead of run like

对我来说也是一样,在控制台中直接输入 import tweepy 它可以工作,但是当我尝试从脚本运行时,它说“没有名为‘tweepy’的模块”我尝试卸载并再次安装,但解决方案更简单,而不是像跑

C:\script.py

I run

我跑

C:\python script.py

It works, Python version is 3.6.2 tweepy version is 3.5.0

它有效,Python 版本是 3.6.2 tweepy 版本是 3.5.0

回答by Hadij

If you are using Anaconda

如果您正在使用 Anaconda

conda install -c conda-forge tweepy

you may also use

你也可以使用

easy_install tweepy

回答by u7768490

If multiple versions of pythonare installed on the computer, you need to make sure under which version the package has been installed into. I have two versions of python installed on my mac, both python2and python3under /usr/local/lib path.

如果python计算机上安装了多个版本,则需要确定软件包安装在哪个版本下。我的 mac 上安装了两个版本的 python python2python3/usr/local/lib path.

pipinstall tweepyonly installs the package into python2.7/site-packages, while VSCodecomplies python3. Run pip3install tweepyto get the package under python3.7/site-packagesso the module can be recognized by the compiler.

pipinstall tweepy仅将软件包安装到 中python2.7/site-packages,而VSCode符合python3. 运行pip3install tweepy以获取包,python3.7/site-packages以便编译器可以识别该模块。

回答by harsh pamnani

If you are using ubuntu try: sudo apt install python-pip

如果您使用的是 ubuntu,请尝试: sudo apt install python-pip

and then run: python3 -m pip install tweepy

然后运行: python3 -m pip install tweepy

I hope that helps!

我希望这有帮助!

回答by Alvis

I had the same issue where after installing/ uninstalling via pip it still did not work. As a last ditch effort I actually moved the 'tweepy' folder in the '...Lib > site-packages' back to the main Python directory and 'import tweepy' then worked. No idea why this worked, but it did for me... Good luck!

我遇到了同样的问题,在通过 pip 安装/卸载后,它仍然无法正常工作。作为最后的努力,我实际上将“...Lib > site-packages”中的“tweepy”文件夹移回了 Python 主目录,然后“import tweepy”开始工作。不知道为什么这有效,但它对我有用......祝你好运!

回答by Yosra ADDALI

I tried this command py -m pip install tweepyand worked for me

我试过这个命令py -m pip install tweepy并为我工作

回答by Debjyoti Banerjee

I tried this, I was having the same error, but the thing that I did was, first installing the package using pip command, then spot the path where it got stored, then using os module change the directory to where the packages got stored, and then import it, it will work..

我试过了,我遇到了同样的错误,但我做的事情是,首先使用 pip 命令安装包,然后找到它存储的路径,然后使用 os 模块将目录更改为存储包的位置,然后导入它,它会工作..

回答by Hashan Malawana

Very common error wherever your computer have different python environments in pycharm. Your computer may have different python interpreters as you install pip several times. Try to config the exact python interpreter using the following steps. PyCharm, Settings -> Project Interpreter -> This will show the installed package list and dedicated interpreter at the top right-hand side. Try your right interpreter within the dropdown.enter image description here

无论您的计算机在 pycharm 中具有不同的 Python 环境,都会出现非常常见的错误。当您多次安装 pip 时,您的计算机可能有不同的 python 解释器。尝试使用以下步骤配置确切的 python 解释器。PyCharm, Settings -> Project Interpreter -> 这将在右上角显示已安装的包列表和专用解释器。在下拉列表中尝试使用正确的解释器。在此处输入图片说明