Reputation: 21
I use Python 3.5 and I have installed cloudant package by an execute command:
sudo -H pip3 install cloudant
I try to connect with python database. According to the documentation - https://console.bluemix.net/docs/services/Cloudant/getting-started.html#getting-started-with-cloudant. This code should works:
from cloudant.client import Cloudant
client = Cloudant("username", "password", url="https://user_name.cloudant.com")
client.connect()
client.disconnect()
When I run it I get an error:
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
Upvotes: 1
Views: 2303
Reputation: 3737
I suspect your problem may be that the pip
used to install the module isn't the pip
that your python is using:
If I do pip3 install cloudant
I get the same problem as you:
(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'
This is because of this:
(py35) stefans-mbp:work stefan$ which pip
//anaconda/envs/py35/bin/pip
(py35) stefans-mbp:work stefan$ which pip3
/usr/local/bin/pip3
The wrong pip3
was used to install the cloudant
module. To remedy, ensure you use the pip
in the virtual env you're using:
(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
Now it works:
(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
>>>
Upvotes: 3
Reputation: 544
Are you running locally or in an app on Bluemix? There must be something wrong with your install, because the code looks fine. This code runs for me:
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()
If you're running on Bluemix, make sure you have a requirements.txt
file to trigger library imports. See https://pip.readthedocs.io/en/1.1/requirements.html
Upvotes: 1