重要错误:没有名为'cloudant.客户端';'cloudant'不是包

2024-04-28 03:47:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python 3.5,并通过一个execute命令安装了cloudant包:

sudo -H pip3 install cloudant

我尝试连接python数据库。根据文档-https://console.bluemix.net/docs/services/Cloudant/getting-started.html#getting-started-with-cloudant。该规范应适用于:

^{pr2}$

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

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

Tags: installin命令client数据库executelinepip3
3条回答

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

如果我做了pip3 install cloudant我会遇到和你一样的问题:

(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'

这是因为:

^{pr2}$

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

(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

现在它起作用了:

(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
>>> 

你是在本地运行还是在Bluemix上的应用程序中运行?你的安装一定有问题,因为代码看起来不错。以下代码为我运行:

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

只需将文件cloudant.py重命名为其他文件。在

相关问题 更多 >