pymongo - "dnspython" module must be installed to use mongodb+srv:// URIs - python

I am trying to connect MongoDB from Atlas.
My mongo uri is: mongodb+srv://abc:123#something.something.com/admin?retryWrites=True
My pymongo version is 3.6.1
I have installed dnspython and done import dns
But i still get this error:
dnspython module must be installed to use mongodb+srv:// URI

In order to use mongo+srv protocol, you need to install pymongo-srv
Launch this command to do it with python 3:
pip3 install pymongo[srv]
or this one for other versions:
pip install pymongo[srv]
And as suggested by #lukrebs, add quotes for ZSH:
pip3 install 'pymongo[srv]'

I would like to answer my own questions here. As I mentioned in the comment, the kernel of the jupyter notebook has to be restarted in order for the pymongo to take effect of the loaded dnspython.

I solved this problem with:
$ python -m pip install pymongo[srv]

In requirements.txt, replace pymongo with pymongo[tls,srv], as mentioned here.

I got stuck with the same problem and tried
pip install dnspython==2.0.0
This is the latest version from https://pypi.org/project/dnspython/
It worked :D

you can use mongo:// instead of mongodb+srv://

May be the protocol, your URI should start with:
mongo+srv instead of mongo+src
If it still not working please put a pip list with the versions of PyMongo and dnspython (and version of python that you are using)

I had the same problem on Ubuntu 18 but since I am using Anaconda
I just tried
Conda install dns python
I had an IPython running, it did not work while the same instance was open but when I restarted that instance it worked.
On a different machine using
Conda install dns python
and it worked but I had to restart my machine altogether due to a different reason before testing it

I had the same issue and found the following line.
import dns.resolver
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=['8.8.8.8']
It worked for me.

pip install dnspython
dnspython is a DNS toolkit for Python. It supports almost all record types. It can be used for queries, zone transfers, and dynamic updates. It supports TSIG authenticated messages and EDNS0.

None of the existing answers had worked for me. I had to do the following:
sudo apt-get install python3-dnspython

Related

It returnes problem with mysqldb in python. I don't understand where is the problem [duplicate]

I am trying to connect to a MySQL server with python connector. I created a new user lcherukuri with the authentication plugin mysql_native_password.
But I got the error
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
Can someone help me?
import mysql.connector
cnx = mysql.connector.connect(user='lcherukuri', password='password',
host='127.0.0.1',
database='test')
cnx.close()
I had the same problem and passing auth_plugin='mysql_native_password' did not work, because I accidentally installed mysql-connector instead of mysql-connector-python (via pip3). Just leaving this here in case it helps someone.
Per Caching SHA-2 Pluggable Authentication
In MySQL 8.0, caching_sha2_password is the default authentication plugin rather than mysql_native_password.
You're using mysql_native_password, which is no longer the default. Assuming you're using the correct connector for your version you need to specify the auth_plugin argument when instantiating your connection object
cnx = mysql.connector.connect(user='lcherukuri', password='password',
host='127.0.0.1', database='test',
auth_plugin='mysql_native_password')
From those same docs:
The connect() method supports an auth_plugin argument that can be used to force use of a particular plugin. For example, if the server is configured to use sha256_password by default and you want to connect to an account that authenticates using mysql_native_password, either connect using SSL or specify auth_plugin='mysql_native_password'.
This question is already answered here and this solution works.
caching sha2 password is not supported mysql
Just try this command :
pip install mysql-connector-python
None of the above solution work for me. I tried and very frustrated until I watched the following video:
https://www.youtube.com/watch?v=tGinfzlp0fE
pip uninstall mysql-connector work on some computer and it might not work for other computer.
I did the followings:
The mysql-connector causes problem.
pip uninstall mysql-connector
The following may not need but I removed both connector completely.
pip uninstall mysql-connector-python
re-install mysql-conenct-python connector.
pip install mysql-connector-python
I also got a similar error
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
You have probably installed mysql-connector instead of mysql-connector-python. So you need to install it again for python3:
pip3 install mysql-connector-python
I had this same issue but my resolution was different because this didn't completely work.
I found this on a GitHub forum - copy and paste this into your terminal. You don't have to change your password; it can be the exact same.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '{NewPassword}';
check your settings using this
select Host,User,plugin from mysql.user;
pip3 install mysql-connector-python did solve my problem as well. Ignore using mysql-connector module.
Modify Mysql encryption
ALTER USER 'lcherukuri'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Use pip install mysql-connector-python
Then connect like this:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost", #hostname
user="Harish", # the user who has privilege to the db
passwd="Harish96", #password for user
database="Factdb", #database name
auth_plugin = 'mysql_native_password',
)
pip install -U mysql-connector-python this worked for me, if you already have installed mysql-connector-python and then follow https://stackoverflow.com/a/50557297/6202853 this answer
I realized that I install mysql-connector instead of mysql-connector-python so run these commands in the terminal
pip3 uninstall mysql-connector
pip3 install mysql-connector-python
You can go to Settings->Project->Project Interpreter and here install latest version of mysql-connector-python package. In my case it was mysql-connector-python 8.0.15.
To have a more permanent solution without going through your code and modifying whatever needs to be modified:
Per MySQL 8 documentation, easiest way to fix this is to add the following to your MySQL d file -> restart MySQL server.
This worked for me!
If your MySQL installation must serve pre-8.0 clients and you encounter compatibility issues after upgrading to MySQL 8.0 or higher, the simplest way to address those issues and restore pre-8.0 compatibility is to reconfigure the server to revert to the previous default authentication plugin (mysql_native_password). For example, use these lines in the server option file:
[mysqld]
#add the following file to your MySQLd file
default_authentication_plugin=mysql_native_password
Please install below command using command prompt.
pip install mysql-connector-python
I was facing the same error for 2 days, then finally I found a solution. I checked for all the installed connectors using pip list and uninstalled all the connectors. In my case they were:
mysql-connector
mysql-connector-python
mysql-connector-python-rf
Uninstalled them using pip uninstall mysql-connector and finally downloaded and installed the mysql-connector-python from MySQL official website and it works well.
For those who couldn't work out because they installed mysql-connector first, I did the following:
1.First on CMD go to the path of 'pip'
2.Use 'pip list' command
3.There would be three packages installed namely six, protobuf and mysql-connector
4.Uninstall each of them separately
5.Now freshly install the mysql-connector-python module
This worked out for me
Install mysql connector using the below command.
pip install mysql-connector-python-rf
Use the command to set the privileges.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY
'very_strong_password';
FLUSH PRIVILEGES;
Use the python command to connect to mysql database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="very_strong_password",
auth_plugin='mysql_native_password')
If you are looking for the solution of following error
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Acces
s is denied: 'D:\softwares\spider\Lib\site-packages\libmysql.dll'
Consider using the --user option or check the permissions.
The solution:
You should add --user if you find an access-denied error.
pip install --user mysql-connector-python
paste this command into cmd and solve your problem
This seems to be a problem with the mysql-connector package. Uninstall and install the mysql-connector-python package instead.
sudo pip uninstall mysql-connector
sudo pip install mysql-connector-python
Alternatively, you could use the mysql-connector package with auth_plugin variable shown the following python code
mysql.connector.connect(host='localhost',port="3306",user='user',password='pass',database='dbname',auth_plugin='myql_native_password')
I think in either case, you also need to setup your SQL database user with the mysql_native_password
alter user 'user'#'localhost' identified with mysql_native_password by 'password';
I ran into the same problem as well.
My problem was, that I accidentally installed the wrong connector version.
Delete your currently installed version from your file system (my path looks like this: C:\Program Files\Python36\Lib\site-packages) and then execute
"pip install mysql-connector-python".
This should solve your problem
i try to resolve this error and finally install PyMySQL instead of mysql library
and it's working properly.
thanks.
I had an almost identical error:
Error while connecting to MySQL: Authentication plugin 'caching_sha2_password' is not supported
The solution for me was simple:
My db username/password creds were incorrect. The error was not descriptive of the problem, so I thought I would share this in case someone else runs into this.
I uninstalled mysql-connector (I installed following tutorial on w3schools tutorial) and installed mysql-connector-python instead. It worked.
Using MySql 8 I got the same error when connecting my code to the DB, using the pip install mysql-connector-python did solve this error.
This did the trick for me:
pip install cryptography
if you are looking for connection url or connection string with correct plugin ; then following worked for me
url = 'mysql+mysqlconnector://%s:%s#%s:%s/%s?auth_plugin=mysql_native_password' % (settings['user'], settings['password'], settings['host'], settings['port'], settings['database'])
It failed with MySQL server version 8.0, but worked with the version 5.7.33
The solution is to use MySQL version 5.7.33
pip install mysql-connector-python , i hope it work

Module exist in 'pip list' but i cannot import it

Here
is screenshot of my problem, this server is in my uni so I don't have all the access, however I can see the package i want to install but get an error that says no module, i searched the web for a while but couldn't find a solution, how can I get it to work?
Try:
pip --version
Probably this is the Python 2.7 pip, and you are trying to use Python 3.
Check if requests is listed by:
pip3 list

scrapy fail to cooperate with python3.7

I failed to install twisted by pip command, so I manually downloaded the .whl file and got it installed( version 18.7.0). Only after i did that, my laptop could install scrapy; however, it seems that the twisted package is not compatible with python 3.7 and it keeps saying "syntax error"
I have tried some method posted on the Github about this issue(https://github.com/scrapy/scrapy/issues/3143), but none of them solve it. I wonder whether I need to shift to python 3.6 or not? cause my python spider can only setup it's downloader and cannot parse webpages
Could anyone please give me some advise?
Yes. Twisted does not yet support Python 3.7. Try Python 3.6 or earlier.
One of the comments from the issue helped me:
pip install git+https://github.com/scrapy/scrapy#master --no-dependencies --upgrade

"ImportError: No module named twilio.rest"

I have installed python 2.7.10 with PATH access and correctly installed twilio. However, when I try to execute a code I get this error message
Traceback (most recent call last):
File "C:\Users\tmslvo\Google Drive\Desktop\send text.py", line 1, in <module>
from twilio.rest import TwilioRestClient
ImportError: No module named twilio.rest
Now I read that a reason might be that python can't find the twilio package so I tried the
which -a python
which -a twilio
commands (in my Windows command prompt) in which case I get
'which' is not recognized as an internal or external command,
operable program or batch file.
Does anybody have an idea of what I'm doing wrong?
Thank you!
Twilio developer evangelist here.
I think your problem will be that somehow when you installed the library, it failed silently(?). A few things to keep in mind:
When installing Python libraries, always make sure you use pip.
Also, check that none of your files within the project are actually called twilio.py as this will conflict with the actual library.
Check that you're using the version of Python you think you're using by running python --version
All that failing, run the installation again, and all going well (without errors), you should be able to test it quickly with the following code.
import twilio
import twilio.rest
try:
client = twilio.rest.TwilioRestClient(account_sid, auth_token)
message = client.messages.create(
body="Hello World",
to="+14159352345",
from_="+14158141829"
)
except twilio.TwilioRestException as e:
print e
try this: sudo pip3 install twilio --upgrade
I had this problem as well.
In my case, I had named my file twilio.py and that is what caused the error.
Renaming the file to send_sms.py ( or any other name of your choice) will resolve the issue!
Close and then relunch all IDLE instances.
This sounds obvious but it worked for me, since the installations of components were successful
I ran into this same issue. I had used easy_install instead of pip to install twilio which was the problem. To fix this I ran pip uninstall twilio and reinstalled using pip.
rename file name other than twilio.py
EX:send_sms.py
A bit late to the party here but I also ran into this issue.
After some trial and error, it looks like it was due to the pip version I was using. I originally used -
pip3 install twilio.
Now I'm unsure of the underlying reason why this did not work, but it seems that pip3 does not encompass all versions of python 3.x? Using
pip3 list and
pip3.8 list
I noticed I had the twilio module for pip3 but not for pip 3.8.
I used the following and was able to solve the issue
pip3.8 install twilio.
I used pip3.8 because that matched the python3.8 version that I am using.
Pycharm user:
Macs (mid 2017) come with python 2.6 and 2.7 installed. PyCharm uses by default 2.6. When you install twilio (Pip install) the module is installed in python version 2.7. So, when you try to run twilio from PyCharm you get
ImportError: No module named twilio.rest
Solution: change the python interpreter in PyCharm. Go to preferences > project interpreter and from the drop menu Project Interpreter choose python 2.7
I think your pip is not configured properly . You may be getting succefuuly installed message but it is not install where it should be. try pip install --user i am sure it will work for you. pip install may work fine only in virtualenvironment without any config.Try pip install --user package name
#iosCurator
I had first installed twilio with the easy_intall tool
I followed the steps below:
Uninstall twilio with the command pip uninstall twillo
Install twilio with the command pip install twilio
Close the python IDLE and relaunch it.
For the windows user,
I have suggested, pip3 install twilio
Follow these steps (on mac):
Shift + Command + P
search: Configure Language Specific Setting
search: Python
add: "code-runner.runInTerminal": true
That's it!
Ask me any question about it by:
My LinkedIn
I ran into this issue when using poetry for my dependency management. Poetry doesn't recognise it as an existing package yet, hence it won't run your code unless you try the poetry+pip way.
there will be 2 reasons for this
1.make sure you kept right path for python files in environment location
2.install twilio
commands:
1.pip3 install twilio
(or)
pip install twilio
2.python otpv.py

ImportError: No module named sqlalchemy

I'm unable to find a module in python ,though easy_install says its already installed.
Any idea how to resolve this isseue?
$ python -c "from flaskext.sqlalchemy import SQLAlchemy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named sqlalchemy
$ python -V
Python 2.7
$ sudo easy_install sqlalchemy
Searching for sqlalchemy
Best match: SQLAlchemy 0.7.7
Adding SQLAlchemy 0.7.7 to easy-install.pth file
Using /usr/lib/python2.7/site-packages
Processing dependencies for sqlalchemy
Finished processing dependencies for sqlalchemy
$ sudo pip install SQLAlchemy --upgrade Requirement already
up-to-date: SQLAlchemy in /usr/lib/python2.7/site-packages Cleaning
up...
Though pip says it's installed.But I can't find them in sys.path output.
$ sudo python -c "import sys;print sys.path" ['',
'/usr/lib/python2.7/site-packages/Flask_SQLAlchemy-0.15-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask-0.8-py2.7.egg',
'/usr/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask_WTF-0.5.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/WTForms-0.6.3-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask_Mail-0.6.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/blinker-1.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/lamson-1.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/python_daemon-1.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/mock-0.8.0-py2.7.egg',
'/usr/lib/python2.7/site-packages/chardet-1.0.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/lockfile-0.9.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/Flask_FlatPages-0.2-py2.7.egg',
'/usr/lib/python2.7/site-packages/Markdown-2.1.1-py2.7.egg',
'/usr/lib/python2.7/site-packages/PyYAML-3.10-py2.7-linux-i686.egg',
'/usr/lib/python2.7/site-packages/uWSGI-1.0.3-py2.7.egg',
'/usr/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg',
'/usr/lib/python27.zip', '/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Did you install flask-sqlalchemy? It looks like you have SQLAlchemy installed but not the Flask extension. Try pip install Flask-SQLAlchemy in your project's virtualenv to install it from PyPI.
I just experienced the same problem. Apparently, there is a new distribution method, the extension code is no longer stored under flaskext.
Source: Flask CHANGELOG
This worked for me:
from flask_sqlalchemy import SQLAlchemy
Install Flask-SQLAlchemy with pip in your virtualenv:
pip install flask_sqlalchemy
Then import flask_sqlalchemy in your code:
from flask_sqlalchemy import SQLAlchemy
Okay,I have re-installed the package via pip even that didn't help. And then I rsync'ed the entire /usr/lib/python-2.7 directory from other working machine with similar configuration to
the current machine.It started working. I don't have any idea ,what was wrong with my setup. I see some difference "print sys.path" output earlier and now. but now my issue is resolved by this work around.
EDIT:Found the real solution for my setup. upgrading "sqlalchemy only doesn't solve the issue" I also need to upgrade flask-sqlalchemy that resolved the issue.
try this:
from flask.ext.sqlalchemy import SQLAlchemy
first install the library
pip install flask_sqlalchemy
after that
from flask_sqlalchemy import SQLAlchemy
put this in app.py file to get the access of database through SQLAlchemy
So here is an idea!
Since it seemed to work somewhere else.
install python-virtualenv
and optionally you can install virtualenv-wrapper (which is pretty cool to create projects and so on)
In each env, you might have different versions of eggs. In other word, you could have sqlalchemy 1 and sqlaclhemy 1.5 in two different envs and they won't conflict with each others. It seems that you have a problem with your currently installed eggs.
So here we go:
virtualenv --no-site-packages foo
source foo/bin/activate
The parameter --no-site-packages will create a virtualenv and not use the packages already installed on your computer. It's pretty much like a bare python install.
source foo/bin/activate loads the virtualenv.
It's not that really userfriendly. And that's why http://www.doughellmann.com/projects/virtualenvwrapper/ exists.
That said, you should see somthing like thant in your terminal "(foo)user#domain$:" once your virtualenv is activated. It means that you can go on!
Then you have to do.
python setup.py develop of your project. It should download and install dependencies of your project in the virtualenv located in foo. If you need to install anything else, please use pip or easy_install without using sudo. When using virtualenv, you almost never need to use sudo. Sudo will install package in your global python install while it's not required and not really desirable.
If something happens in your virtualenv, you can always delete it and create a new one. This is no big deal. No need to mess with anything. Doesn't work? start over, do pip install -U if needed, define the versions if needed and so on.
Last but not least, in the other answers, it seems that the import changed. If the new versions for flask-sqlalchemy is located somewhere else, you should update your import or install the version you used to use.
This code works perfectly:
import sqlalchemy
Maybe you installed the package in another version of the interpreter?
Also, like Shawley pointed out, you need to have the flask extension installed in order for it to be accessible.
I'm new comer, use python 3.8 and met the same problem. I installed with pip instead of pip3 because i thought the pip installer is the same for python2 and python3
so this is proper installation
pip3 install flask_sqlalchemy
Probably a stupid mistake; but, I experienced this problem and the issue turned out to be that "pip3 install sqlalchemy" installs libraries in user specific directories.
On my Linux machine, I was logged in as user1 executing a python script in user2's directory. I installed sqlalchemy as user1 and it by default placed the files in user1's directory. After installing sqlalchemy in user2's directory the problem went away.
Solution for me was to use:
from flask_sqlalchemy import SQLAlchemy
instead of
from flask.ext.sqlalchemy import SQLAlchemy
Very late to the party but hopefully this will help someone, was in the same situation for about a hour without any of the solutions mentioned above working. (On a Windows 10 machine).
In the Settings/Preferences dialog (Ctrl+Alt+S), from the side menu select Project: | Project Interpreter.
Check which packages you currently have installed (You need SQLAlchemy and Flask-SQLAlchemy). Double click on any package name, an 'Available Packages' menu will open.
Search for the missing package(s) and click install.
On Windows 10 # 2019
I faced the same problem. Turns out I forgot to install the following package:
pip install flask_sqlalchemy
After installing the package, everything worked perfectly. Hope, it helped some other noob like me.
I just experienced the same problem using the virtual environment.
For me installing the package using python from the venv worked:
.\venv\environment\Scripts\python.exe -m pip install flask-sqlalchemy
I am not sure if it is still relevant but try uninstalling sqlalchemy and then installing flask-sqlalchemy.
I guess if you have sqlalchemy, flask-sqlalchemy wont work.
Tried in Python 3.8
I also faced the same problem by a silly mistake. I have created a separate conda environment for flask from scratch. However, I tried to test the import of FlaskSQLAlchemy using IPython. Finally, I realized that it was the system-based python, not the one coming from conda env.
Always make sure that your sys.path contains the directory with flask_sqlalchemy.
I was using virtualenv, even if the flask_sqlalchemy was installed, I was still getting it, what I did was to deactivate virtual environment, delete the flask from global packages, and delete virtual environment, create fresh one, install requirements again. And it worked. Hope this helps.

Categories