首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImportError:没有名为“cloudant.client”的模块;“cloudant”不是包

ImportError:没有名为“cloudant.client”的模块;“cloudant”不是包
EN

Stack Overflow用户
提问于 2017-11-01 01:01:58
回答 3查看 1.6K关注 0票数 1

我使用的是Python3.5,我已经通过执行命令安装了cloudant包:

代码语言:javascript
复制
sudo -H pip3 install cloudant

我试着连接python数据库。根据文档- https://console.bluemix.net/docs/services/Cloudant/getting-started.html#getting-started-with-cloudant。这段代码应该可以工作:

代码语言:javascript
复制
from cloudant.client import Cloudant

client = Cloudant("username", "password", url="https://user_name.cloudant.com")
client.connect()
client.disconnect()

当我运行它时,我得到一个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tomek/Projects/stage-control/cloudant.py", line 1, in <module>
    from cloudant.client import Cloudant
ImportError: No module named 'cloudant.client'; 'cloudant' is not a package
EN

回答 3

Stack Overflow用户

发布于 2017-11-01 17:37:01

我怀疑您的问题可能是用于安装模块的pip不是您的python正在使用的pip

如果我使用pip3 install cloudant,我会遇到和你一样的问题:

代码语言:javascript
复制
(py35) stefans-mbp:work stefan$ python
Python 3.5.3 |Anaconda 4.4.0 (x86_64)| (default, Mar  6 2017, 12:15:08) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from cloudant.client import Cloudant
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'cloudant.client'

这是因为:

代码语言:javascript
复制
(py35) stefans-mbp:work stefan$ which pip
//anaconda/envs/py35/bin/pip
(py35) stefans-mbp:work stefan$ which pip3
/usr/local/bin/pip3

安装cloudant模块时使用了错误的pip3。要进行补救,请确保在正在使用的虚拟环境中使用pip

代码语言:javascript
复制
(py35) stefans-mbp:work stefan$ pip install cloudant
Collecting cloudant
Using cached cloudant-2.7.0.tar.gz
Requirement already satisfied: requests<3.0.0,>=2.7.0 in 
/anaconda/envs/py35/lib/python3.5/site-packages (from cloudant)
Building wheels for collected packages: cloudant
  Running setup.py bdist_wheel for cloudant ... done
  Stored in directory: ...
Successfully built cloudant
Installing collected packages: cloudant
Successfully installed cloudant-2.7.0

现在它可以工作了:

代码语言:javascript
复制
(py35) stefans-mbp:work stefan$ python
Python 3.5.3 |Anaconda 4.4.0 (x86_64)| (default, Mar  6 2017, 12:15:08) 
Type "help", "copyright", "credits" or "license" for more information.
>>> from cloudant.client import Cloudant
>>> 
票数 3
EN

Stack Overflow用户

发布于 2017-12-13 19:01:53

只需将文件cloudant.py重命名为其他名称即可。

票数 3
EN

Stack Overflow用户

发布于 2017-11-01 02:50:10

您是在本地运行还是在Bluemix上的应用程序中运行?一定是你的安装出了什么问题,因为代码看起来没问题。下面的代码为我运行:

代码语言:javascript
复制
from cloudant.client import Cloudant
from cloudant.document import Document

client = Cloudant("username", "pw", url="https://username.cloudant.com")
client.connect()
thedb = client["databasename"]
for document in thedb:
    print(document)
client.disconnect()

如果您在Bluemix上运行,请确保您有一个requirements.txt文件来触发库导入。请参阅https://pip.readthedocs.io/en/1.1/requirements.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47040754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档