error to import MySQLdb in python - python

I have error to connect existing mysql database in python and i unable to do
pip install mysql command
import MySQLdb
db = MySQLdb.connect(host='10.10', port=3306, user='system', passwd='xyz' db='Reporting')
print(db)
I want to retrieve the data from mysql database in python.

Use pip install MySQL-python instead

You need to install the dependencies before installation with pip.
apt-get install python-dev libmysqlclient-dev
And than, the installation with pip like the answer of Zart should work.
pip install MySQL-python
The above answer is only for Linux System.
For Windows you can download the .whl file from here mysqlclient and install it. Please download the right version. win32 for 32 bit. amd64 for 64 bit. cp27 for python 2.7, cp34 for python 3.4, cp35 for python 3.5

Related

Problem installing MariaDB Python bindings on Raspberry Pi OS--how to get the latest Connector/C?

I am trying to use MariaDB with Python on a Raspberry Pi 4 with the 64 bit OS. After much trial and error I cannot solve the issue of a newer Connector/C version being required. The steps I took so far:
sudo apt update
sudo apt upgrade
sudo apt install mariadb-server
This installed MariaDB just fine and I can use it in a terminal. In order to install Python bindings I first installed
sudo apt install libmariadbclient-dev
but then when I try to install the Python module with
pip3 install mariadb
There is an error:
Collecting mariadb
Using cached https://files.pythonhosted.org/packages/71/a4/2d73d007571a5df9369eed9166150c7e067eb883cbd9ec3f97c7a48be660/mariadb-1.1.0.tar.gz
Complete output from command python setup.py egg_info:
MariaDB Connector/Python requires MariaDB Connector/C >= 3.2.4, found version 3.1.13
I wonder why version 3.2.4 is required in the first place. According to the documentation, it was just released 10 days ago: https://mariadb.com/kb/en/about-mariadb-connector-c/
There is no repository for Raspberry Pi OS. How can I install the newest Connector/C?
The answer for me was not to get the newest Connector/C, but to install an older version of the mariadb module. In my case going back to 1.0.7 was sufficient. It can be installed with
pip3 install -Iv mariadb==1.0.7
However, it seems like the newest pip version automatically falls back to the latest compatible module, so try upgrading pip first:
python3 -m pip install --user --upgrade pip
And then run the simpler command (without specific version number):
python3 -m pip install mariadb

update PostgreSQL client library to 9.1

I am installing psycopg2 using this command:
pip3 install psycopg2
But the output is:
./psycopg/psycopg.h:30:2: error: #error "Psycopg requires PostgreSQL
client library (libpq) >= 9.1"
My pg_config version: pg_config --version is PostgreSQL 8.4.20. How to upgrade PostgreSQL client library? I am searching from Google, but no method to guide me. Python version: 3.5.0.
I guess you need to install it according to these instructions - https://www.postgresql.org/download/linux/redhat/.
Upgrade pip version 7.1.2 to version 18.1 solve this problem:
pip3 install --upgrade pip
Execute this command to install psycopg2:
pip3 install psycopg2

psycopg2 import error when ubuntu upgraded to 17.10 (from 17.04)

Everything was working great until I upgraded the OS to Ubuntu 17.10. Now my Django project won't run (python manage.py runserver) because psycopg2 won't import. psycopg2 is already installed with pip (nothing has changed there). To be exact this is the error:
lib/python3.5/site-packages/psycopg2/.libs/libresolv-2-c4c53def.5.so:
symbol __res_maybe_init, version GLIBC_PRIVATE not defined in file
libc.so.6 with link time reference
Reinstall psycopg2 and use the binary.
sudo pip uninstall psycopg2
pip install psycopg2-binary
It was a problem of the wheel build tool fixed with the release of a new binary pacakge in psycopg2 2.7.3.1
Try reinstalling psycopg2. It looks like a dynamically linked dependency changed. The database adapters as a rule have compiled components and those are compiled against system files that can change on updates, so on major OS upgrades, you'll almost certainly have to recompile a pip installed version.
I've had the same issue. Apparently, there is a compatibility issue with glibc binaries. The following worked for me:
pip uninstall psycopg2
sudo apt-get install postgresql-server-dev-X.Y (if not already installed)
pip install --no-binary :all: psycopg2
Source: Problem loading psycopg2 with glibc 2.26

Upgraded to Ubuntu 16.04 now MySQL-python dependencies are broken

I just upgraded my Ubuntu install to 16.04 and this seems to have broken my mysql dependencies in the MySQL-python package.
Here is my error message:
File "/opt/monitorenv/local/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 386, in create_engine
return strategy.create(*args, **kwargs)
File "/opt/monitorenv/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 75, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/opt/monitorenv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 92, in dbapi
return __import__('MySQLdb')
File "/opt/monitorenv/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
So basically the import_mysql is looking for an so file that doesn't exist because in Ubuntu 16.04, I have libmysqlclient20 installed.
And libmysqlclient18 is not available.
As far as I am aware (or at least I believe) my python libraries are up to date with the latest versions.
(I tried running pip install --upgrade mysql-python which indicated it was up to date).
Do you guys have any suggestions ?
Thank for Largaroth. If you use mysqlclient on Ubuntu 16.04 and have error:
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
You can fix:
sudo -H pip uninstall mysqlclient
sudo -H pip install --no-binary mysqlclient mysqlclient
I ended up finding the solution to my problems with pip install --no-binary MySQL-python MySQL-python
as stated in this thread : Python's MySQLdb can’t find libmysqlclient.dylib with Homebrewed MySQL
I had the same issue. I uninstalled and reinstalled MySQL-python:
pip uninstall MySQL-python
pip install MySQL-python
I had this issue on updating to stretch. To fix it I updated my requirements.txt:
mysqlclient==1.4.2.post1
So either update that manually or pip install --upgrade mysqlclient
My problem was that I was using wheelhouse from old OS.
The problem was solved when I uninstalled/installed the package or updated wheelhouse...
From docs:
http://mysql-python.sourceforge.net/FAQ.html#importerror
This means you have a version of MySQLdb compiled against one version of MySQL, and are now trying to run it against a different version. The shared library version tends to change between major releases.
Solution: Rebuilt MySQLdb, or get the matching version of MySQL.
Steps:
search mysql path
which mysql
O/p : /opt/mysql/
create Symbolic Links to usr/lib
sudo ln -s /opt/mysql/lib/mysqlclient.so.20 /usr/lib
Note: mysqlclient.so.20 will be as per your version
I had this issue with python 3.6... when I used an environment with Python 3.5 it worked just fine.
I solved this on my virtual environment using django 2.2.7 and Ubuntu 19.10 by doing:
pip3 uninstall mysqlclient
pip3 install mysqlclient

ImportError: No module named 'MySQL'

I have downloaded the Connector/Python for MySQL successfully. I used the following code in Python's shell to test my connection:
import mysql.connector
I received the following error message:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import mysql.connector
ImportError: No module named 'mysql'
I can't figure out why MySQL is not being recognized.
I was facing the similar issue. My env details -
Python 2.7.11
pip 9.0.1
CentOS release 5.11 (Final)
Error on python interpreter -
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>
Use pip to search the available module -
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf -
$ pip install mysql-connector-python-rf
Verify
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
Thanks =)
For python3 and later use the next command:
$ pip3 install mysql-connector-python-rf
Use
pip3 install mysql-connector
to install the python packaged (if you are using Python 3. For Python 2 you can use pip).
The silly mistake I had done was keeping mysql.py in same dir. Try renaming mysql.py to another name so python don't consider that as module.
May be simple install from cli?
pip3 install mysql-connector-python-rf
Package name differs from import library name
Or my universal variant in code:
import pip
pip.main(['install','mysql-connector-python-rf'])
For new version of pip:
from pip._internal import main
main(['install','mysql-connector-python-rf'])
It's better - install needed modules in running python installation (if many)
I tried all the answers but not worked for me.
It is a python version problem, in the end, I realized that python 3 scripts need explicit pip command for python 3, at least on ubuntu 18.
python3 -m pip install mysql-connector
Try that out bud
sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
gunzip mysql-connector-python-2.1.3.tar.gz
tar xf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
You need to use anaconda to manage python environment dependencies. MySQL connector can be installed using conda installer
conda install -c anaconda mysql-connector-python
run
pip list
to see list of packages you have installed. If it has
mysql-connector-python then that is fine.
Remember not to name your python script file as mysql.py
just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:
using the package from repository sudo apt-get install python-mysqldb: Was installed correctly, but unfortunatelly python returned ImportError: No module named 'mysql'
using the ubuntu package from Mysql: wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.4-1ubuntu16.04_all.deb. Installed correctly with sudo dpckg -i package_name, but python returned the same error.
using tar.gz file, installing with python, worked ok.
wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.tar.gz (used 2.1.4 at that time)
tar -xf mysql-connector-python-2.1.6.tar.gz
cd mysql-connector-python-2.1.6/
sudo python3 setup.py install
Did not investigate why the first two failed, might try later.
Hope this helps someone.
sudo python3 -m pip install mysql-connector-python
This problem was a plague to me!!! The 100% solution is to forget using the mysql module: import mysql.connector, instead use pymysql via import pymysql. I installed it via the instructions: python3 -m pip install PyMySQL
made a change to the:
Import statement
The connector
The cursor
After that everything worked like a charm. Hope this helps!
I found that #gdxn96 solution worked for me, but with 1 change.
sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
tar -zxvf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
I was facing the same issue on mac and linux both despite installing the connector using the command as mentioned in the other answers.
What worked for me the following commands.
sudo python3 -m ensurepip --default-pip
python3 -m pip --version
sudo python3 -m pip install mysql-connector-python
I don't know what difference do they make since I am not a python programmer but would like to know if someone can enlighten.
πŸ‘‡οΈ in a virtual environment or using Python 2
pip install mysql-connector-python
πŸ‘‡οΈ for python 3 (could also be pip3.10 depending on your version)
pip3 install mysql-connector-python
πŸ‘‡οΈ if you get permissions error
sudo pip3 install mysql-connector-python
pip install mysql-connector-python --user
πŸ‘‡οΈ if you don't have pip in your PATH environment variable
python -m pip install mysql-connector-python
πŸ‘‡οΈ for python 3 (could also be pip3.10 depending on your version)
python3 -m pip install mysql-connector-python
πŸ‘‡οΈ using py alias (Windows)
py -m pip install mysql-connector-python
πŸ‘‡οΈ for Anaconda
conda install -c anaconda mysql-connector-python
πŸ‘‡οΈ for Jupyter Notebook
!pip install mysql-connector-python
Source: https://bobbyhadz.com/blog/python-no-module-named-mysql
I have found another reason:
If your program file's path contains space or special characters, then you'd get this error.
#nembokid
Thanks, I wanted to build on this answer. If you're using anaconda or any other form of environment management, be sure to select the python from that environment.
sudo /home/joseph/anaconda3/envs/stocks/bin/python -m pip install mysql-connector-python
I had the same issue I resolved it using the following steps:
Step 1 - in terminal type echo %path% look for a file that's similar "C:\Users\AppData\Local\Programs\Python\Python39"
Step 2 - in vs code type "ctrl+shift+p" select python interpreter
Step 3 - make sure you select the interpreter with the correct path, you found yours when you used echo %path%
Now run the program !
You need to use one of the following commands. Which one depends on different-2 OS and software you have and use.
sudo easy_install mysql-python (mix os)
sudo pip install mysql-python (mix os)
sudo apt-get install python-mysqldb (Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
yum install MySQL-python (Linux Fedora, CentOS ...)
I just moved source folder connector from folder mysql to site-packages.
And run import connector
For 64-bit windows
install using wheel
pip install wheel download from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
For python 3.x:
pip install mysqlclient-xxxxxxxxx-win_amd64.whl
For python 2.7:
pip install mysqlclient-xxxxxxxxx-win_amd64.whl
My project was using pipenv. The following command worked for me:
pipenv install mysql-connector-python
I did try re-install with apt and pip.
But the only thing that worked was install from source.
On Ubuntu 18.04 x64
Here is how I fixed the issue on my end.
Issue
I couldn't neither compile nor have the package available on VSCode.
Context
System: Ubuntu 18.04
Python version: 3.6.*
Installed Packages
mysql
mysql-connector-python
mysql-connector-python-rf
How I solved:
Nothing really worked until I see this article:
https://dev.mysql.com/doc/connector-python/en/connector-python-cext-development.html
On python console, just set
use_pure = True
With that, I could use python based connector instead.
import mysql.connector
However, if something goes wrong, the C Extension can be used through the _mysql_connector module.
References:
https://dev.mysql.com/doc/connector-python/en/connector-python-cext-module.html
https://www.reddit.com/r/learnpython/comments/a0q4bq/mysqlconnector_how_to_use_use_puretrue/
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
https://overiq.com/mysql-connector-python-101/installing-mysql-connector-python/
in my case - for python 3.x works
pip3 install mysql-connector-python
I am facing the same issue, search a lot but didn't found any useful answer. Finally I found a mistake that first you should check your script name. Your script name shouldn't be mysql.py.

Categories