【问题标题】:ImportError: No module named 'cloudant.client'; 'cloudant' is not a packageImportError:没有名为“cloudant.client”的模块; 'cloudant' 不是一个包
【发布时间】:2018-04-12 22:30:53
【问题描述】:

我使用 Python 3.5,并通过执行命令安装了 cloudant 包:

sudo -H pip3 install cloudant

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

from cloudant.client import Cloudant

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

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

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

【问题讨论】:

  • 在命令行输入pip show cloudant。报告什么?如果没有,则没有安装该软件包。此外,您很少希望使用sudo 安装软件包。只需以普通用户的身份进行即可。见What are the risks of running 'sudo pip'?

标签: python cloudant python-cloudant


【解决方案1】:

您是在本地运行还是在 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

【讨论】:

    【解决方案2】:

    我怀疑您的问题可能是用于安装模块的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'
    

    这是因为:

    (py35) stefans-mbp:work stefan$ which pip
    //anaconda/envs/py35/bin/pip
    (py35) stefans-mbp:work stefan$ which pip3
    /usr/local/bin/pip3
    

    错误的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
    >>> 
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-07
        • 2018-10-16
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 2016-01-23
        相关资源
        最近更新 更多