Juniper notebook not recognizing the pymongo

I have python installation on my windows 10 at C:\Users\$USERID\APPDATA. After joining this course I installed Ananconda3 . I was running Junyper notebook activated from Junyper navigator. I want to use Junyper notebaook as it is convenient to test it.
If run

import pymongo
uri = “mongodb+srv://m220-user:m220-pass@m220-lessons-mcxlm.mongodb.net/test”
client = pymongo.MongoClient(uri)
m220 = client.m220
movies = m220.movies

I get —
ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import pymongo
2 uri = “mongodb+srv://m220-user:m220-pass@m220-lessons-mcxlm.mongodb.net/test”
3 client = pymongo.MongoClient(uri)
4 m220 = client.m220
5 movies = m220.movies

ModuleNotFoundError: No module named 'pymongo’

It looks this is related to anacoda3 not having pymongo. I know the above uri is incorrect, but that is not related to Module not found.
I do have pymongo in c:\Users$USERID\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
I ran
— import sys
sys.path

ther result i see is

[’’,
‘C:\Users\$USERID’,
‘C:\new\Software\Anaconda3\python37.zip’,
‘C:\new\Software\Anaconda3\DLLs’,
‘C:\new\Software\Anaconda3\lib’,
‘C:\new\Software\Anaconda3’,
‘C:\new\Software\Anaconda3\lib\site-packages’,
‘C:\new\Software\Anaconda3\lib\site-packages\win32’,
‘C:\new\Software\Anaconda3\lib\site-packages\win32\lib’,
‘C:\new\Software\Anaconda3\lib\site-packages\Pythonwin’,
‘C:\new\Software\Anaconda3\lib\site-packages\IPython\extensions’,
‘C:\Users\$USERID\.ipython’]

How do I make pymongo work on Junyper notebook? appreciate any help

Hi,

I met the same issue with you, and I did following steps:

  1. Uninstall python version 3 in AppData first.
  2. Remove the python environment variable.
  3. Delete the mflix that I created by Conda.
  4. Re-install all dependency packages from the requirement.txt file.

Hope this help you,
Hieu Tran

Thank you Hieu for the information. I am doing another training from udemy related to python. That installation is done as a part of that training. It took some time for me that environment to be stabilized. Are there any other options. Are there any way I just install pymongo on the Anaconda or just temporarily change the PATH to make this work?

Hello Rao_97297,

Thanks for the message. You should be able to install Pymongo on Anaconda through the requirements.txt file.

Have you been able to install the dependencies onto your Anaconda environment?

Matt

Thank you Matt for the reminder. I am not sure. Now, I installed

Line 16: Requirement already satisfied: pymongo==3.7.0 in c:\users$USERID\appdata\local\programs\python\python37-32\lib\site-packages (from -r requirements.txt (line 4)) (3.7.0)

Interstingly, I still get the message

ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import pymongo
2 uri = “mongodb+srv://m220-user:m220-pass@mflix-XXXX.mongodb.net/test”
3 client = pymongo.MongoClient(uri)
4 m220 = client.m220
5 movies = m220.movies

ModuleNotFoundError: No module named ‘pymongo’

Probably the python path from Junyper is different from the windows command line?

Hello Rao_97297,

Yes, Jupyter is probably using a different Python path.

Have you activated the Anaconda environment? Make sure Anaconda has been appended to your PATH environment variable.

Matt

Hi Rao,

Because I could not make sure that some dependency packages will be install in AppData python library or Anaconda python, so I have to remove Python in AppData to easier to debug some issue. Anaconda only uses its python and python library in it.

Hieu Tran

Thank you Matt and Hieu for the feedback.

After unsuccessfully trying options suggested, I decided to uninstall my anaconda3 and old python and reinstall. It happened that my old python is 32 bit and python that came with Anaconda 64 bit. I uninstalled Anaconda first and my old python next , manually deleted all PATH variables from windows 10 advanced settings. Did the following:

  1. Installed Anaconda3 , installed dependencies,
  2. Ran cd mflix-python under the Anaconda3 directory ( previous mflix-python directory is in different location, the previous assignments worked well before)
  3. ran conda create --name mflix
  4. copied mflix-python directory from old location to new directory under Anaconda3\mflix-python (so all the changes from previous assaignments will be in the new location)

I was hoping everything will be good. but it didn’t.

  • Opened the Anaconda navigator, installed Jupyter for the environment “mflix”. tried import pymongo. Well, it didn’t work.
  • went to cmd and tried Jupyter notebooks and tried “import pymongo”, it works.
  • To check whether environment is good:
    • compared directory contents from the old mflix-python to new mflix-python ( at binary level). All seems to be match
  • Ran pytest -m connection. Now i am getting a strange error. Not sure what the issue is

Traceback (most recent call last):
File “C:\new\Software\Anaconda3\lib\site-packages_pytest\config_init_.py”, line 381, in _getconftestmodules
return self._path2confmods[path]
KeyError: local(‘C:\new\Software\Anaconda3\mflix-python\tests’)

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\new\Software\Anaconda3\lib\site-packages_pytest\config_init_.py”, line 412, in _importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local(‘C:\new\Software\Anaconda3\mflix-python\tests\conftest.py’)

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\new\Software\Anaconda3\lib\site-packages_pytest\config_init_.py”, line 418, in _importconftest
mod = conftestpath.pyimport()
File “C:\new\Software\Anaconda3\lib\site-packages\py_path\local.py”, line 686, in pyimport
raise self.ImportMismatchError(modname, modfile, self)
py._path.local.LocalPath.ImportMismatchError: (‘tests.conftest’, ‘c:\new\work\MongoDB\mflix-python\tests\conftest.py’, local(‘C:\new\Software\Anaconda3\mflix-python\tests\conftest.py’))
ERROR: could not load C:\new\Software\Anaconda3\mflix-python\tests\conftest.py

No change done to the .ini file it is same as before

[PROD]
SECRET_KEY = secret_string
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-XXXX.mongodb.net

[TEST]
SECRET_KEY = secret_string
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-XXXX.mongodb.net

In short… after reinstallation

  1. pytest -m connection not working now.
  2. pymongo not getting recognized from Junpter notebook opened from Anaconda3, but works from cmd ( I didn’t check whether pymongo worked before from cmd)

I appreciate any help.

Well, I found out what the issue is.

In my old mflix-python it created a directory called tests\ pycache directory and in that there *.pyc files exist. It looks these files are specific to the directory path and shouldn’t be copied( these files contains the exact file name and path in it). I missed this directory when I compared directories.

Now I deleted the directory and ran the pytest -m connection . ALL is good. I will deal with pymongo issue from Anaconda navigator initated Jupyter notebook issue later. at least i can initiate Jupyter notepad from cmd and work

I thought I could use Jupyter notebook from command line. though import pymongo is not giving error, any other command related to cursor is fiving error
############
import pymongo
uri = “mongodb+srv://m220-user:m220-pass@mflix-XXXXmongodb.net/test”
client = pymongo.MongoClient(uri)
m220 = client.m220
movies = m220.movies

No issues when ran

cursor = movies.find( { “cast”: “Salma Hayek” }, { “title”: 1 } )
print(dumps(cursor, indent=2))


OperationFailure Traceback (most recent call last)
in ()
1 cursor = movies.find( { “cast”: “Salma Hayek” }, { “title”: 1 } )
----> 2 print(dumps(cursor, indent=2))

C:\new\Software\Anaconda3\lib\site-packages\bson\json_util.py in dumps(obj, *args, **kwargs)
406 “”"
407 json_options = kwargs.pop(“json_options”, DEFAULT_JSON_OPTIONS)
–> 408 return json.dumps(_json_convert(obj, json_options), *args, **kwargs)
409
410

C:\new\Software\Anaconda3\lib\site-packages\bson\json_util.py in _json_convert(obj, json_options)
447 for k, v in iteritems(obj)))
448 elif hasattr(obj, ‘iter’) and not isinstance(obj, (text_type, bytes)):
–> 449 return list((_json_convert(v, json_options) for v in obj))
450 try:
451 return default(obj, json_options)

C:\new\Software\Anaconda3\lib\site-packages\bson\json_util.py in (.0)
447 for k, v in iteritems(obj)))
448 elif hasattr(obj, ‘iter’) and not isinstance(obj, (text_type, bytes)):
–> 449 return list((_json_convert(v, json_options) for v in obj))
450 try:
451 return default(obj, json_options)

C:\new\Software\Anaconda3\lib\site-packages\pymongo\cursor.py in next(self)
1187 if self.__empty:
1188 raise StopIteration
-> 1189 if len(self.__data) or self._refresh():
1190 if self.__manipulate:
1191 _db = self.__collection.database

C:\new\Software\Anaconda3\lib\site-packages\pymongo\cursor.py in _refresh(self)
1102 self.__session,
1103 self.__collection.database.client)
-> 1104 self.__send_message(q)
1105 elif self.__id: # Get More
1106 if self.__limit:

C:\new\Software\Anaconda3\lib\site-packages\pymongo\cursor.py in __send_message(self, operation)
929 try:
930 response = client._send_message_with_response(
–> 931 operation, exhaust=self.__exhaust, address=self.__address)
932 self.__address = response.address
933 if self.__exhaust:

C:\new\Software\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _send_message_with_response(self, operation, exhaust, address)
1143 self.__all_credentials,
1144 self._event_listeners,
-> 1145 exhaust)
1146
1147 def _reset_on_error(self, server, func, *args, **kwargs):

C:\new\Software\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _reset_on_error(self, server, func, *args, **kwargs)
1154 “”"
1155 try:
-> 1156 return func(*args, **kwargs)
1157 except NetworkTimeout:
1158 # The socket has been closed. Don’t reset the server.

C:\new\Software\Anaconda3\lib\site-packages\pymongo\server.py in send_message_with_response(self, operation, set_slave_okay, all_credentials, listeners, exhaust)
83 It is returned along with its Pool in the Response.
84 “”"
—> 85 with self.get_socket(all_credentials, exhaust) as sock_info:
86
87 duration = None

C:\new\Software\Anaconda3\lib\contextlib.py in enter(self)
110 del self.args, self.kwds, self.func
111 try:
–> 112 return next(self.gen)
113 except StopIteration:
114 raise RuntimeError(“generator didn’t yield”) from None

C:\new\Software\Anaconda3\lib\site-packages\pymongo\pool.py in get_socket(self, all_credentials, checkout)
1004 sock_info = self._get_socket_no_auth()
1005 try:
-> 1006 sock_info.check_auth(all_credentials)
1007 yield sock_info
1008 except:

C:\new\Software\Anaconda3\lib\site-packages\pymongo\pool.py in check_auth(self, all_credentials)
675
676 for credentials in cached - authset:
–> 677 auth.authenticate(credentials, self)
678 self.authset.add(credentials)
679

C:\new\Software\Anaconda3\lib\site-packages\pymongo\auth.py in authenticate(credentials, sock_info)
540 mechanism = credentials.mechanism
541 auth_func = _AUTH_MAP.get(mechanism)
–> 542 auth_func(credentials, sock_info)
543
544

C:\new\Software\Anaconda3\lib\site-packages\pymongo\auth.py in _authenticate_default(credentials, sock_info)
515 return _authenticate_scram(credentials, sock_info, ‘SCRAM-SHA-256’)
516 else:
–> 517 return _authenticate_scram(credentials, sock_info, ‘SCRAM-SHA-1’)
518 elif sock_info.max_wire_version >= 3:
519 return _authenticate_scram(credentials, sock_info, ‘SCRAM-SHA-1’)

C:\new\Software\Anaconda3\lib\site-packages\pymongo\auth.py in _authenticate_scram(credentials, sock_info, mechanism)
243 (‘payload’, Binary(b"n," + first_bare)),
244 (‘autoAuthorize’, 1)])
–> 245 res = sock_info.command(source, cmd)
246
247 server_first = res[‘payload’]

C:\new\Software\Anaconda3\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events)
577 compression_ctx=self.compression_context,
578 use_op_msg=self.op_msg_enabled,
–> 579 unacknowledged=unacknowledged)
580 except OperationFailure:
581 raise

C:\new\Software\Anaconda3\lib\site-packages\pymongo\network.py in command(sock, dbname, spec, slave_ok, is_mongos, read_preference, codec_options, session, client, check, allowable_errors, address, check_keys, listeners, max_bson_size, read_concern, parse_write_concern_error, collation, compression_ctx, use_op_msg, unacknowledged)
148 helpers._check_command_response(
149 response_doc, None, allowable_errors,
–> 150 parse_write_concern_error=parse_write_concern_error)
151 except Exception as exc:
152 if publish:

C:\new\Software\Anaconda3\lib\site-packages\pymongo\helpers.py in _check_command_response(response, msg, allowable_errors, parse_write_concern_error)
153
154 msg = msg or “%s”
–> 155 raise OperationFailure(msg % errmsg, code, response)
156
157

OperationFailure: Authentication failed.

Not sure why this message comes when i try to do any thing with cursor or cursor printing.
OperationFailure: Authentication failed.

I appreciate any input