I am referring the following tutorial to make a login page for my web application.
http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982
I am having issue with the database.
I am getting an
ImportError: No module named MySQLdb
when I execute
http://127.0.0.1:5000/testdb
I have tried all possible ways to install python mysql, the one mentioned in the tutorial, easy_install, sudo apt-get install.
I have installed mysql in my virtual env. My directory structure is just the same as whats explained in the tutorial. The module is sucessfully installed in my system and still I am getting this error.
Please help. What could be causing this.
If you're having issues compiling the binary extension, or on a platform where you cant, you can try using the pure python PyMySQL bindings.
Simply pip install pymysql and switch your SQLAlchemy URI to start like this:
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'
There are some other drivers you could also try.
Or try this:
apt-get install python-mysqldb
may you try
pip install mysqlclient
My issue is :
return __import__('MySQLdb')
ImportError: No module named MySQLdb
and my resolution :
pip install MySQL-python
yum install mysql-devel.x86_64
at the very beginning, i just installed MySQL-python, but the issue still existed. So i think if this issue happened, you should also take mysql-devel into consideration.
Hope this helps.
I got this issue when I was working on SQLAlchemy. The default dialect used by SQLAlchemy for MySQL is mysql+mysqldb.
engine = create_engine('mysql+mysqldb://scott:tiger#localhost/foo')
I got the "No module named MySQLdb" error when the above command was executed. To fix it I installed the mysql-python module and the issue was fixed.
sudo pip install mysql-python
It depends on Python Version as well in my experience.
If you are using Python 3, #DazWorrall answer worked fine for me.
However, if you are using Python 2, you should
sudo pip install mysql-python
which would install 'MySQLdb' module without having to change the SQLAlchemy URI.
So I spent about 5 hours trying to figure out how to deal with this issue when trying to run
./manage.py makemigrations
With Ubuntu Server LTS 16.1, a full LAMP stack, Apache2 MySql 5.7 PHP 7 Python 3 and Django 1.10.2 I really struggled to find a good answer to this. In fact, I am still not satisfied, but the ONLY solution that worked for me is this...
sudo apt-get install build-essential python-dev libapache2-mod-wsgi-py3 libmysqlclient-dev
followed by (from inside the virtual environment)
pip install mysqlclient
I really dislike having to use dev installs when I am trying to set up a new web server, but unfortunately this configuration was the only mostly comfortable path I could take.
In ubuntu 20 , you can try this :
sudo apt-get install libmysqlclient-dev
sudo apt-get install gcc
pip install mysqlclient
While #Edward van Kuik's answer is correct, it doesn't take into account an issue with virtualenv v1.7 and above.
In particular installing python-mysqldb via apt on Ubuntu put it under /usr/lib/pythonX.Y/dist-packages, but this path isn't included by default in the virtualenv's sys.path.
So to resolve this, you should create your virtualenv with system packages by running something like:
virtualenv --system-site-packages .venv
yum install MySQL-python.x86_64
worked for me.
for Ubuntu 20.04 with python3
sudo apt-get install python3-mysqldb
by default, this work for me
Create a sqlite engine instance
engine = create_engine('mysql://username:password#your_host/your_dbname')
OR
pip install pymysql
Create a sqlite engine instance
create_engine('mysql://...
I have the same issue the install mySQL connector and resolved
pip install mysqlclient
more details ->
SQLAlchemy official docs
Got so many errors related to permissions and what not. You may wanna try this :
xcode-select --install
sudo apt-get install python-mysqldb
Here's what I did:
First, install a python package:
pip3 install PyMySQL
Then add the following lines of code to your Project:
import pymysql
pymysql.install_as_MySQLdb()
this should fix your issue by now, the following one is optional and for anyone who is using SqlAlchemy to connect to mysql server:
Replace engine = create_engine("mysql://YOURMYSQLURL")
to:
engine = create_engine("mysql+pymysql://YOURMYSQLURL")
I have faced this type of error when I tray to build python flask eccomerce web-application
ModuleNotFoundError: No module named 'MySQLdb'?
I tray to see the version of python installed on my window 10
it was python 3.10
so MYSQL-PYTHON is compatable with the other version of python 3.8
I delet python 3.10 and I nstalled the version python 3.8
It become ok!
python --version
3.8
pip --version
python 3.8
so install the connector
pip install MYSQL-PYTHON
successfuly installed
Related
I am using windows 10 command line for a django project using python34 however, I am facing difficulties with SQL.
I have already installed mysqlclient using pip install mysqlclient==1.3.5 and located the file to make sure I was not delusional. I then ran python manage.py migrate to migrate the tables to the SQL database (I am using phpmyadmin). However when the command returned with...
File "C:\Users\user\env\lib\site-packages\django\db\backends\mysql\base.py", line 30, in <module>
'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?
I am aware that questions like these already exist, but not one solution seems to have made any impact on attempts.
pip install pymysql
Then, edit the __init__.py file in your project origin dir(the same as settings.py)
add:
import pymysql
pymysql.install_as_MySQLdb()
Faced same problem after migrating to python 3. Apparently, MySQL-python is incompatible, so as per official django docs, installed mysqlclient using pip install mysqlclient on Mac. Note that there are some OS specific issues mentioned in docs.
Quoting from docs:
Prerequisites
You may need to install the Python and MySQL development headers and libraries like so:
sudo apt-get install python-dev default-libmysqlclient-dev # Debian / Ubuntu
sudo yum install python-devel mysql-devel # Red Hat / CentOS
brew install mysql-connector-c # macOS (Homebrew) (Currently, it has bug. See below)
On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC.
Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
sudo apt-get install python3-dev # debian / Ubuntu
sudo yum install python3-devel # Red Hat / CentOS
Note about bug of MySQL Connector/C on macOS
See also: https://bugs.mysql.com/bug.php?id=86971
Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when mysqlclient-python is installed. (As of November 2017, this is known to be true for homebrew's mysql-connector-c and official package)
Modification of mysql_config resolves these issues as follows.
Change
# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
to
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
An improper ssl configuration may also create issues; see, e.g, brew info openssl for details on macOS.
Install from PyPI
pip install mysqlclient
NOTE: Wheels for Windows may be not released with source package. You should pin version in your requirements.txt to avoid trying to install newest source package.
Install from source
Download source by git clone or zipfile.
Customize site.cfg
python setup.py install
For pip
pip install pymysql
For pip3 you should use
python3 -m pip install PyMySQL
Then, edit the init.py file in your project origin directory (the same as settings.py). Add:
import pymysql
pymysql.install_as_MySQLdb()
This command did the trick from #Aniket Sinha's answer above:
pip install mysqlclient
Use the below command to solve your issue,
pip install mysql-python
apt-get install python3-mysqldb libmysqlclient-dev python-dev
Works on Debian
If you already have mysqlclient installed (i.e. you see Requirement already satisfied) and are getting Error loading MySQLdb Module, the following worked for me:
pip uninstall mysqlclient
export LDFLAGS="-L/usr/local/opt/openssl/lib" and export CPPFLAGS="-I/usr/local/opt/openssl/include" as explained here
pip install mysqlclient
That reinstalls mysqlclient and for whatever reason solved my problem.
For MAC os user:
I have faced this issue many times. The key here is to set these environment variables before installing mysqlclient by pip command. For my case, they are like below:
export LDFLAGS="-L/usr/local/opt/protobuf#3.7/lib"
export CPPFLAGS="-I/usr/local/opt/protobuf#3.7/include"
edit the init.py file in your project origin directory
import pymysql
pymysql.install_as_MySQLdb()
I am using python 3 in windows.
I also faced this issue. I just uninstalled 'mysqlclient' and then installed it again.
It worked somehow
Installing mysqlclient using anaconda is pretty simple and straight forward.
conda install -c bioconda mysqlclient
and then, install pymysql using pip.
pip install pymysql
I had this issue just recently even with using the python 3 compatible mysqlclient library and managed to solve my issue albeit in a bit of an unorthodox manner. If you are using MySQL 8, give this a try and see if it helps! :)
I simply made a copy of the libmysqlclient.21.dylib file located in my up-to-date installation of MySQL 8.0.13 which is was in /usr/local/mysql/lib and moved that copy under the same name to /usr/lib.
You will need to temporarily disable security integrity protection on your mac however to do this since you won't have or be able to change permissions to anything in /usr/lib without disabling it. You can do this by booting up into the recovery system, click Utilities on the menu at the top, and open up the terminal and enter csrutil disable into the terminal. Just remember to turn security integrity protection back on when you're done doing this! The only difference from the above process will be that you run csrutil enable instead.
You can find out more about how to disable and enable macOS's security integrity protection here.
I simply needed to update my project's dependencies and then restart the server.
To solve the problem run: pip install mysql-connector-python==8.0.23
Faced this issue with mysql.connector version 8.0.24 on mac(if code base is same then the issue should happen in windows as well). This file on line 51 imports "from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper". The imported file has following code 14-20(exact code and error that you received is part of code
try:
import MySQLdb as Database
except ImportError as err:
raise ImproperlyConfigured(
'Error loading MySQLdb module.\n'
'Did you install mysqlclient?'
) from err
The error is formed here. Not sure why this import keeps coming back in different version of mysql connector but 8.0.23 does not have the import, so I reverted to that version and error was gone... This is incase you wish to continue to work with mysql.connector.python. Not sure of logic and why this error is open for last 5 years...
Run this command in CLI.
pip install mysqlclient
[NEW]
Try pip install mysqlclient
if not working, and if you are getting below error
MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
Then visit this site and download the supported wheel.
Now how to know the supported wheel. For example there are multiple wheel files like below:
mysqlclient-1.4.6-cp36-cp36m-win_amd64.whl
See the bold version cp36 which indicates CPython version 3.6
Just install it and this will resolve your issue.
Oh! now if you come across the another problem how to install the wheel file then just refer below line.
python -m pip install "C:\Users\Abhijit\Downloads\mysqlclient-1.4.6-cp36-cp36m-win_amd64.whl"
Inspired by #elad silver's answer (thanks).
For OSx (Monterey) this worked for me:
Install protobuf
brew install protobuf
edit your .bashrc, or .zshenv file with these lines:
export LDFLAGS="${LDFLAGS} -L/usr/local/opt/protobuf/lib"
export CPPFLAGS="${CPPFLAGS} -I/usr/local/opt/protobuf/include"
Reinstall mysqlclient
pip uninstall mysqlclient
pip install mysqlclient
For now because default mysql server on Debian is MariaDB.
user#cl1-preprod ~ % mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 39
apt-get install mariadb doesn't exist. To install it you need to install this package apt-get install mariadb-server but this is only the mysql server the client is also required apt-get install mariadb-client.
Then pip install mysqlclient
user#cl1-preprod ~ % apt-get remove libmysqlclient-dev python-dev
Lecture des listes de paquets... Fait
Construction de l'arbre des dépendances
Lecture des informations d'état... Fait
Le paquet « libmysqlclient-dev » n'est pas installé, et ne peut donc être supprimé
Le paquet « python-dev » n'est pas installé, et ne peut donc être supprimé
You have to install mysqlclient. Just ran
pip install mysqlclient
I'm running Python 3.5 (on Windows) and I have installed python-ldap from https://pypi.python.org/pypi/python-ldap/
I also tried using ldap3 but I keep getting an error saying
"ImportError: No module named 'ldap'
I looked around and saw some people saying there's no python-ldap for 3.5 so I installed 2.6 still getting the same error.
Is there a way to import ldap and make it work for Python 3.5?
Try the command below:
sudo apt-get install libsasl2-dev python-dev libldap2-dev libssl-dev
sudo pip3 install pyldap
Open a command line(cmd, powershell, git bash)
Check you python version
$ pyhton --version
Go to https://www.lfd.uci.edu/~gohlke/pythonlibs/
Download the library according with your python version and windows system
And install it on a command line using
pip install file_downloaded.whl
For example:
If you have python 3.5x and windows x64,
download the file python_ldap‑3.2.0‑cp35‑cp35m‑win_amd64.whl
pip install python_ldap‑3.2.0‑cp35‑cp35m‑win_amd64.whl
I'm running Apache Airflow on an Amazon EC2-Instance and I was getting "ImportError: No module named 'ldap3'. I used these two sites https://www.python-ldap.org/en/latest/installing.html and http://ldap3.readthedocs.io/installation.html to run the commands sudo python -m pip install python-ldap and sudo pip install ldap3 but my pip wasn't working for the last command so after some investigation I found out in my /usr/bin/ directory I had pip, pip-2.7, pip-3.6, and pip-python so I changed the command to pip-3.6 install ldap3 and then everything worked! Hope this helps someone.
I tried multiple approaches but finally, PyPI official documentation fixed this.
I was trying to execute on VS Code and did pip3 install python-ldap, but it didn't solve the issue. So I did the below from VS Code
# %% - This runs the below code as a Jupyter notebook cell
!pip3 install python-ldap
Now, I am able to import ldap and use it
Inside you folder, you can use virtualenv for python 3, example:
/opt/python-ldap-test
virtualenv -p /usr/bin/python3.5 venv
source venv/bin/activate
and then
pip install ldap3
It's extremely complicated to make things from 2.X to work in 3.X. Have you tried using it in a separate, 2.X only script and using it from there? It's not so unusual to combine python 2.X with 3.X in that manner or so I've heard.
I'm trying to use PyMySQL on Ubuntu.
I've installed pymysql using both pip and pip3 but every time I use import pymysql, it returns ImportError: No module named 'pymysql'
I'm using Ubuntu 15.10 64-bit and Python 3.5.
The same .py works on Windows with Python 3.5, but not on Ubuntu.
Sort of already answered this in the comments, but just so this question has an answer, the problem was resolved through running:
sudo apt-get install python3-pymysql
Use:
import pymysql
Not:
import PyMySQL
That works for me.
After trying a few things, and coming across PyMySQL Github, this worked:
sudo pip install PyMySQL
And to import use:
import pymysql
If even sudo apt-get install python3-pymysql does not work for you try this:
Go to the PyMySQL page and download the zip file.
Then, via the terminal, cd to your Downloads folder and extract
the folder
cd into the newly extracted folder
Install the setup.py file with: sudo python3 setup.py install
Make sure that you're working with the version of Python that think you are. Within Python run import sys and print(sys.version).
Select the correct package manager to install pymysql with:
For Python 2.x sudo pip install pymysql.
For Python 3.x sudo pip3 install pymysql.
For either running on Anaconda: sudo conda install pymysql.
If that didn't work try APT: sudo apt-get install pymysql.
If all else fails, install the package directly:
Go to the PyMySQL page and download the zip file.
Then, via the terminal, cd to your Downloads folder and extract the folder.
cd into the newly extracted folder.
Install the setup.py file with: sudo python3 setup.py install.
This answer is a compilation of suggestions. Apart from the other ones proposed here, thanks to the comment by #cmaher on this related thread.
To get around the problem, find out where pymysql is installed.
If for example it is installed in /usr/lib/python3.7/site-packages, add the following code above the import pymysql command:
import sys
sys.path.insert(0,"/usr/lib/python3.7/site-packages")
import pymysql
This ensures that your Python program can find where pymysql is installed.
fwiw, for a conda env:
conda install -c anaconda pymysql
For windows or one using google colab, you can try this
!pip install pymysql
import pymysql
sudo apt-get install python3-pymysql
This command also works for me to install the package required for Flask app to tun on ubuntu 16x with WISG module on APACHE2 server.
BY default on WSGI uses python 3 installation of UBUNTU.
Anaconda custom installation won't work.
I had this same problem just now, and found the reason was my editor (Visual Studio Code) was running against the wrong instance of python; I had it set to run again python bundled with tensorflow, I changed it to my Anaconda python and it worked.
Just a note:
for Anaconda install packages command:
python setup.py install
if you are using SPYDER IDE , just try to restart the console or restart the IDE, it works
I also got this error recently when using Anaconda on a Mac machine.
Here is what I found:
After running python3 -m pip install PyMySql, pymysql module is under /Library/Python/3.7/site-packages
Anaconda wants this module to be under /opt/anaconda3/lib/python3.8/site-packages
Therefore, after copying pymysql module to the designated path, it runs correctly.
Another common issue causing the error message to appear is related to conda environments in jupyter notebook and jupyter lab.
After successfully installing a module (pymysql in this case) in one environment, import may seem to fail because the environment has not been correctly registered. To solve this it is necessary to manually add kernels for each environment.
Solution and more details can be found here.
I tried installing pymysql on command prompt by typing
pip install pymysql
But it still dont work on my case, so I decided to try using the terminal IDE and it works.
I ran into the same problem earlier, but solved it in a way slightly different from what we have here. So, I thought I'd add my way as well. Hopefully, it will help someone!
sudo apt-get install mysql-client didn't work for me. However, I have Homebrew already installed. So, instead, I tried:
brew install mysql-client
Now, I don't get the error any more.
You can also visit this lien pip install PyMySQL
You just need to install:
python3 -m pip install PyMySQL
The following pymysql version worked for me:
pip install pymysql==1.0.2
For anaconda user,
I got this error message.
ModuleNotFoundError: No module named 'pymysql'
So tried 'pip install pymysql' but got below.
Requirement already satisfied: pymysql
What worked for me is,
python file.py (NOT py file.py)
I am having trouble installing a Django app (Mezzanine) on Ubuntu 14.04. I've installed most necessities using apt-get (except for django-compressor and south -used pip), including psycopg2 for Postgres. However when I go to run python manage.py createdb it gives this error:
Error loading psycopg2 module: No module named psycopg2
This is the command I'm using to install psycopg2:
sudo apt-get install python-psycopg2
What am I doing wrong? Should I use pip to install psycopg2. I went to the website and it recommends installing through your OS package manager instead of pip.
I am working in a virtualenv except for when I am installing the psycopg2 elements....
If you need psycopg2 for a system installed program, then install it with the system package manager. If you need it for a program in a virtualenv, install it in that virtualenv.
. env/bin/activate
pip install psycopg2
Note that on many distros, the development headers needed for compiling against libraries are not installed by default. For psycopg2 on Ubuntu you'll need the python and postgresql headers.
sudo apt-get install python-dev libpq-dev
psycopg 2.7 now issues a warning that it will stop providing binary releases due to compatibility issues.
The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: http://initd.org/psycopg/docs/install.html#binary-install-from-pypi.
See the release announcement for a thorough explanation. To handle the warning, tell pip not to download the pre-built wheel for psycopg2.
pip install --no-binary psycopg2 psycopg2
For me, to resolve this issue on Ubuntu 14.04 with virtualenv I had to
sudo apt-get install python3.5-dev
then I could run
pip install psycopg2
The psycopg you installed through apt-get is not visible from inside the virtual env.
You should install it through pip
pip install psycopg2
after sourcing your environment.
I have installed Python version 3.4.0 and I would like to do a project with MySQL database. I downloaded and tried installing MySQLdb, but it wasn't successful for this version of Python.
Any suggestions how could I fix this problem and install it properly?
MySQLdb does not support Python 3 but it is not the only MySQL driver for Python.
mysqlclient is essentially just a fork of MySQLdb with Python 3 support merged in (and a few other improvements).
PyMySQL is a pure python MySQL driver, which means it is slower, but it does not require a compiled C component or MySQL libraries and header files to be installed on client machines. It has Python 3 support.
Another option is simply to use another database system like PostgreSQL.
Use mysql-connector-python. I prefer to install it with pip from PyPI:
pip install --allow-external mysql-connector-python mysql-connector-python
Have a look at its documentation and examples.
If you are going to use pooling make sure your database has enough connections available
as the default settings may not be enough.
Install pip:
apt-get install pip
For acess MySQL from Python, install:
pip3 install mysqlclient
There is a Ubuntu solution available either through the Ubuntu Software Center or through the Synaptic Package Manager. This will connect Python version 3.4.0 to MySQL. Download "python3-mysql.connector" version 1.1.6-1.
Note that the connection syntax does not use "MySQLdb".
Instead read: Connecting to MySQL Using Connector/Python
sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install zlib1g-dev
sudo pip3 install mysqlclient
that worked for me!
I solved it this way:
download the zipped package from here and follow this set of instructions:
unzip /path/to/downloads/folder/mysql-connector-python-VER.zip
In case u got a .gz u can use ->
tar xzf mysql-connector-python-VER.tar.gz
And then:
cd mysql-connector-python-VER # move into the directory
sudo python3 setup.py install # NOTICE I USED PYTHON3 INSTEAD OF PYTHON
You can read about it here
It seems that at the moment Ubuntu 15.10 has a but with python3 and pip3.
As elaborated in this article.
The problem makes pip3 install to python3.5 while python3 is actually running python3.4 :(
Until a proper solution will be available via the updates you can do one of the following:
run
python3 -m pip install pymysql
instead of
pip3 install pymysql
(or any other package)
Now
import pymysql
should work in python3 and in idle3.
Alternatively, if you explicitly need 3.5 you can use explicit python3.5 instead of python3. but idle3 will still point to 3.4...
mysqlclient is a fork of MySQLdb and can serve as a drop-in replacement with Python 3.4 support. If you have trouble building it on Windows, you can download it from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages
Alternatively, you can use mysqlclient or oursql. For oursql, use the oursql py3k series as my link points to.
Maybe you can use a work around and try something like:
import datetime
#import mysql
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1',user = 'someUser', passwd = 'foobar',db = 'foobardb')
cursor = conn.cursor()
for fedora and python3 use: dnf install mysql-connector-python3