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
Related
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
So I'm trying to follow a tutorial to connect to an SQL database, using Connector/Python.
I have to import it, obviously:
import mysql.connector
So I tried pip install mysql in the terminal.
This is what I got:
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/g6/yrxcmygn3ls375rm9rh3p86r0000gn/T/pip-build-a0d0rzdl/MySQL-python/
What I've tried from looking up on Google:
pip install --upgrade setuptools
pip install ez_setup
pip install unroll
easy_install -U setuptools
Nothing worked
Thanks
MySQL-Python is not available for Python 3, you may consider using MySqlClient which is a fork of the MySQL-python interface for the MySQL database.
Before installing this connector, make sure MySQL (or MariaDB) is installed.
On Linux, you also need the "devel" packages. See: https://stackoverflow.com/a/7461662/1513933
On Windows, you may be interested in the unofficial Windows binaries.
To install, an application, the best practices is to use a virtualenv.
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
I'm attempting to make a website with a few others for the first time, and have run into a weird error when trying to use Django/Python/VirtualEnv. I've found solutions to this problem for other operating systems, such as Ubuntu, but can't find any good solutions for Mac.
This is the relevant code being run:
virtualenv -p python3 venv
source venv/bin/activate
pip install -r requirements.txt
After running that block, I get the following errors:
AssertionError
Failed building wheel for django-toolbelt
Running setup.py bdist_wheel for psycopg2
...
AssertionError
Failed building wheel for psycopg2
Failed to build django-toolbelt psycopg2
I believe I've installed the "django-toolbelt" and "psycopg2", so I'm not sure why it would be failing.
The only difference I can think of is that I did not use the command
sudo apt-get install libpq-dev
as was instructed for Ubuntu usage as I believe that installing postgresql with brew took care of the header.
Thanks for any help or insight!
For MacOS users
After trying all the above methods (which did not work for me on MacOS 10.14), that one worked :
Install openssl with brew install openssl if you don't have it already.
add openssl path to LIBRARY_PATH :
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
install psycopg2 with pip pip3 install psycopg2
I had the same problem on Arch linux. I think that it's not an OS dependant problem. Anyway, I fixed this by finding the outdated packages and updating then.
pip uninstall psycopg2
pip list --outdated
pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2
I was also getting same error.
Using Python 3.7.3 and pip 19.1.1.
I used following command.
pip install psycopg2-binary==2.8.3
TDLR
If you aren't used to installing Python C-extensions, and psycopg2 isn't a core part of your work, try
pip install psycopg2-binary
Building Locally
psycopg2 is a C-extension, so it requires compilation when being installed by pip. The Build Prerequisites section of the docs explain what must be done to make installation via pip possible. In summary (for psycopg 2.8.5):
a C compiler must be installed on the machine
the Python header files must be installed
the libpq header files must be installed
the pg_config program must be installed (it usually comes with the libpq headers) and on $PATH.
With these prerequisites satisfied, pip install psycopg2 ought to succeed.
Installing pre-compiled wheels
Alternatively, pip can install pre-compiled binaries so that compilation (and the associated setup) is not required. They can be installed like this:
pip install psycopg2-binary
The docs note that
The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements.
but I would suggest that psycopg2-binary is often good enough for local development work if you are not using psycopg2 directly, but just as a dependency.
Concluding advice
Read the informative installation documentation, not only to overcome installation issues but also to understand the impact of using the pre-compiled binaries in some scenarios.
I had same problem and this appears to be a Mojave Issue, I was able to resolve with:
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
For Mac OS X users:
1. First check your postgresql path by running this command in terminal:
pg_config
If this fails lookup how to add pg_config to your path.
2. Next install Xcode Tools by running this command in terminal:
xcode-select --install
If you have both those sorted out now try to install psycopg2 again
For MacOS users, this question has the correct solution:
install command line tools if necessary:
xcode-select --install
then
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
I was also facing the same after running all the above commands, but the following two commands worked for me:
Instead of pip, use this:
sudo apt-get install libpq-dev
then run this command:
pip install psycopg2
On OS X, I was able to solve this by simply upgrading wheel before installing psycopg2:
pip install --upgrade wheel
For OSX Sierra users, it seems that an xcode update is the solution: Can't install psycopg2 package through pip install... Is this because of Sierra?
I tried all the above solutions but they did not work for me. What I did was change the psycopg2 version in my requirements.txt file from psycopg2==2.7.4 to psycopg2==2.7.6
Is your error message complete? the most encountered reason for failing to install psycopg2 on mac from pip is pg_config is not in path.
by the way, using macports or fink to install psycopg2 is more recommended way, so you don't have to worry about pg_config, libpq-dev and python-dev.
plus, are using Python 3.5? then upgrage your wheel to > 0.25.0 using pip.
I faced the same issue, but the answers above didn't work for me.
So this is what I did in my requirements.txt
psycopg2-binary==2.7.6.1 and it worked fine
I had this issue on several packages, including psycopg2, numpy, and pandas. I simply removed the version from the requirements.txt file, and it worked.
So instead of psycopg2-binary==2.7.6.1 I just had psycopg2-binary.
I know you are asking for development environment but if you are deploying on server say, Heroku. Just add below line in the requirements.txt of your project.
django-heroku==0.3.1
As this package itself will install the required packages like psycopg2 on server deployment.So let the server(heroku) should take care of it.
sudo apt install libpq-dev python3.X-dev
where X is the sub version,
these should be followed by :
pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2
Enjoy !!!
I solved my problem by updating/installing vs_BuildTools. The link to the software was given in the error itself.
Error Image
Fixed by installing python3.7-dev: sudo apt install python3.7-dev, based on the link.
Python: 3.7
Ubuntu: 20.04.3 LTS
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.