Connection to pymongo - mongodb

I am trying to connect to mongo in MAC using pymongo. I am getting the following error-
>>> from pymongo import MongoClient
Traceback (most recent call last):
File "", line 1, in
from pymongo import MongoClient
ImportError: cannot import name 'MongoClient'
I have tried Connection also. But it gives the same error. Any help?

Steps for troubleshooting:
First of all, verify if your environment is activated and you are in the correct environment.
If it is active and you're in the correct environment, then verify if you have installed pymongo.
If it's not installed in your environment, install it using pip install pymongo in the environment you want to work.

Related

flask_pymongo : PyMongo vs MongoClient : [SSL: CERTIFICATE_VERIFY_FAILED]

I have a huge flask application and I use the PyMongo class from flask_pymongo for MongoDB operations. My issue is in development environment.
I have a MONGO_URI in my config.py like this:
MONGO_URI = "mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority"
Usage in my app looks like this:
# This is how I have initialized it in '__init__.py'
from flask_pymongo import PyMongo
mongo = PyMongo(app)
# This is how I access collections in the specified DB
document = mongo.db[collection_name].find() # Throws the below error
This worked fine until a couple days ago when I reinstalled Windows and also Python from scratch.
Now the same code throws the following error:
pymongo.errors.ServerSelectionTimeoutError: cluster-name-shard-00-01.pihvl.gcp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
But using MongoClient instead of PyMongo works just fine, like this:
# Using MongoClient and setting 'ssl_cer_reqs'
from flask_pymongo import MongoClient
import ssl
mongo = MongoClient(app.config['MONGO_URI'], ssl_cert_reqs=ssl.CERT_NONE)
# This works just fine
document = mongo[db_name][collection_name].find()
Questions
What changed with PyMongo when I reinstalled everything? I don't want to use MongoClient simply for the fact that there are over 100 endpoints and it is a nightmare to shift now, and also I prefer PyMongo because I don't have to specify the db_name in every query, it gets that from the MONGO_URI.
What am I missing and how do I make it work with PyMongo? Is there a way to set the ssl_cert_reqs while using PyMongo?
Something in the certificate chain will have changed. pymongo has specific documentation on how to troubleshoot this issue:
https://pymongo.readthedocs.io/en/stable/examples/tls.html#troubleshooting-tls-errors
IMPORTANT: Do this only if you're facing this issue in development environment
All I had to do was add &ssl=true&ssl_cert_reqs=CERT_NONE to my MONGO_URI in my Development Configuration so now my uri would change from:
mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority
to:
mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE
pip install certifi
import pymongo
import certifi
myclient = pymongo.MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
...

PyMongo null bytes error on import

I'm just trying to import the PyMongo package using Python3 and get the following error:
from pymongo import MongoClient
File "C:\Anaconda3\lib\site-packages\pymongo__init__.py", line 83, in
ValueError: source code string cannot contain null bytes
I've tried updating, reinstalling (with pip), cloning from github etc with no joy.
Any suggestions gratefully received.
UPDATE: Must be a local config issue since pymongo working in virtualenv.

Why cannot I import 'pandas_udf' in Jupiter notebook?

I run the following code in Jupyter notebook, but get ImportError. Note that 'udf' can be imported in Jupyter.
from pyspark.sql.functions import pandas_udf
ImportError Traceback (most recent call
last) in ()
----> 1 from pyspark.sql.functions import pandas_udf
ImportError: cannot import name 'pandas_udf'
Anyone knows how to fix it? Thank you very much!
It looks like you start jupyter notebook by itself, rather than start pyspark with jupyter notebook, which is following command:
PYSPARK_DRIVER_PYTHON=jupyter PYSPARK_DRIVER_PYTHON_OPTS="notebook" pyspark
If your jupyter notebook server process are running from another machine, maybe you want to use this command to make it available to all IP addresses of your sever.
(NOTE: This could be a potential security issue if your server is on a public or untrusted network)
PYSPARK_DRIVER_PYTHON=jupyter PYSPARK_DRIVER_PYTHON_OPTS="notebook --ip=0.0.0.0 " pyspark
I will revised my answer if the problem still persist after you start jupyter notebook like that.

How to use read_gbq or other bq in IPython to access datasets hosted in BigQuery

I am using the iPython notebook to read the Google BigQuery public dataset for natality
I have done the installation for the google-api
easy_install --upgrade google-api-python-client.
However it still does not detect the installed API
Anyone has a iPython notebook to share on accessing the public dataset and loading it into a dataframe in iPython.
import pandas as pd
projectid = "xxxx"
data_frame = pd.read_gbq('SELECT * FROM xxxx', project_id = projectid)
303 if not _GOOGLE_API_CLIENT_INSTALLED:
--> 304 raise ImportError('Could not import Google API Client.')
305
306 if not _GOOGLE_FLAGS_INSTALLED:
ImportError: Could not import Google API Client
I have shared the iPython Notebook used at
http://nbviewer.ipython.org/urls/dl.dropbox.com/s/d77u2xarscagw0b/BigQuery_Trial8.ipynb?dl=0
Additional info:
I am running on a server with a docker instance used for the iPython server.
I have run the curl https://sdk.cloud.google.com | bash installation on the linux server
I have tried to run some of the shared notebooks
nbviewer.ipython.org/gist/fhoffa/6459195
or nbviewer.ipython.org/gist/fhoffa/6472099
However I also get
ImportError: No module named bq
I suspect it is a simple case of missing dependencies.
Anyone who has clues, help welcome
As I just said it here: https://stackoverflow.com/a/31708375/2533394
I solved the problem with this:
pip install --force-reinstall uritemplate.py
Make sure your Pandas is version 0.17 or higher:
pip install -U pandas
You can check with:
import pandas as pd
pd.__version__

MongoDB w/ PyMongo on Heroku -- error: no module named pymongo

I am making a heroku app using Flask with a mongoDB backend. I am using pymongo as my driver but when I push my code to git, it crashes and spits out this error:
2014-04-05T09:56:36.301695+00:00 app[web.1]: ImportError: No module named pymongo
The web-app works locally and pymongo works completely fine in that scenario. What do I have to do to have pymongo recognized on heroku's servers?
Thansks!
You need to make Heroku aware of PyMongo as a requirement. The easiest way to do this is my adding pymongo==x.x to your requirements.txt file.
If you are using a setup.py script instead of requirements.txt, add 'pymong==x.x', to the install_requires argument to setup.
In both cases, make sure to replace x.x with the appropriate version number.