This question already has answers here:
Python MySQLdb not importing
(7 answers)
Closed 3 years ago.
I'm going to connect to mysql database using python. My python version is 2.7.12 and I've installed MySQL using sudo yum install MySQL-python
Package MySQL-python26-1.2.3-11.14.amzn1.x86_64 already installed and latest version
Nothing to do
Linux version:
$ cat /etc/*-release
NAME="Amazon Linux AMI"
VERSION="2016.09"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2016.09"
But when I try to import MySQLdb package it gives an error:
$python
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
I've tried different ways as long as I found on the internet but it didn't work.
Any help would be appreciated.
Updated:
Required dependency library:
sudo yum install mysql-devel
Try running:
pip install MySQL-python
Make sure you have depending libraries installed.
For that you may run:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
As in your case sudo yum install mysql-devel did the job for you.
install pip and upgrade to the latest version.
apt-get install python-pip
pip install -U pip
Next, install the required development packages.
apt-get install python-dev libmysqlclient-dev
Then...
pip install MySQL-python
This should complete installation. After running these commands successfully, try importing MySQLdb in python interpreter.
>>> import MySQLdb
Related
I tried to install a python package through pip, which raises a SyntaxError within setup.py:
$ /usr/bin/pip install dash --user
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Collecting dash
Using cached https://files.pythonhosted.org/packages/ad/94/c591c1ff695c32e7fc5138525f3a792d289160008f1a7f517860cc744a85/dash-0.26.5.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-ysGuJO/dash/setup.py", line 5, in <module>
exec(open('dash/version.py').read(), main_ns) # pylint: disable=exec-used
File "<string>", line 1
__version__ = '0.26.5'
^
SyntaxError: invalid syntax
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ysGuJO/dash/
You are using pip version 7.1.0, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
What am I doing wrong here? I suppose it has something todo with a too old python version:
$ python --version
Python 2.6.6
since the response is similar to this, more specific question on stackoverflow and
pip install urllib3
worked fine, which only requires >= Python 2.6.
This is a problem because of an older version of python. you might have to upgrade your python. After upgrading you won't be facing any issue.below are the step to upgrade. write the steps in your terminal.
# refreshing the repositories
sudo apt update
# its wise to keep the system up to date!
# you can skip the following line if you not
# want to update all your software
sudo apt upgrade
# installing python 2.7 and pip for it
sudo apt install python2.7 python-pip
# installing python-pip for 3.6
sudo apt install python3-pip
apt-get update
apt-get install python python-pip
python -m pip install --no-cache-dir setuptools==20.7.0
python -m pip install xxx
from flask_mysqldb import MySQL
File "/Library/Python/2.7/site-packages/flask_mysqldb/__init__.py", line 1, in <module>
import MySQLdb
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: #rpath/libmysqlclient.21.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: image not found
this is the error I receive when trying to run my flask app on a local server.
You may have to install the python connector for mysql.
> brew install mysql-connector-c
> pip install mysql-python`
After installation, your import should work fine.
NB: I assume that you have already installed homebrew
It's easy to do, but hard to remember the correct spelling:
pip install mysqlclient
If you need 1.2.x versions (legacy Python only), use pip install MySQL-python
Note: Some dependencies might have to be in place when running the above command. Some hints on how to install these on various platforms:
Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)
sudo apt-get install python-pip python-dev libmysqlclient-dev
Fedora 24:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
Mac OS
brew install mysql-connector-c
if that fails, try this
brew install mysql
Traceback (most recent call last):
File "C:\AppServ\www\pythonWeb\MySqlConn.py", line 1, in <module>
import mysql.connector
ImportError: No module named 'mysql'
I got the above error message while trying to import mysql.connector
I'm using a Window with python 3.4.2 and Appserv 2.5.10 for Window
I've saved the python file under the folder of AppServ.
Please help.
Error clearly shows you do 'not have the module "Mysql" which you are importing in order to process further.
ImportError: No module named 'mysql'
What you can do depending on OS you are using :
Windows
pip install mysqlclient
If you need 1.2.x versions (legacy Python only), use pip install MySQL-python
Note: Some dependencies might have to be in place when running the above command.
Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)
sudo apt-get install python-pip python-dev libmysqlclient-dev
Fedora 24:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
Mac OS
brew install mysql-connector-c
if that fails, try
brew install mysql
once you install the module successfully re-run your script and it should work
Does Python version 2.7 have bcrypt in its library?
I tried import bcrypt and got the following error:
Traceback (most recent call last):
File "register.py", line 7, in <module>
import bcrypt
ImportError: No module named bcrypt
bcrypt is available as a pip package for python 2.7, so yes, it supports python 2.7.
It does not come by default with your python installation though, you need to pip install bcrypt in your python environment to import bcrypt successfully
As jonsharpe mentioned, bcrypt is not in standard python library, but it can be easily installed and used in python 2.7
From terminal, just try
sudo pip install bcrypt
And you should be able to do
import bcrypt
without issue.
The following error for me was caused by an upgrade in the version of python.
There are two ways to fix, you can downgrade to the old version of python or you can purge that old version by running the following commands.
sudo apt-get clean
&&
sudo apt-get autoremove --purge
&&
sudo apt-get remove python
&&
sudo apt-get autoremove
Then create a virtualenv with the new python version and install your dependencies. All the best!
I'm using a clean instance of Ubuntu server and would like to install some python packages in my virtualenv.
I receive the following output from the command 'pip install -r requirements.txt'
Downloading/unpacking pymongo==2.5.2 (from -r requirements.txt (line 7))
Downloading pymongo-2.5.2.tar.gz (303kB): 303kB downloaded
Running setup.py egg_info for package pymongo
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named setuptools.command
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named setuptools.command
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/redacted/env/build/pymongo
Storing complete log in /home/redacted/.pip/pip.log
Any Idea what's going on?
python version 2.7.3
pip version pip 1.4 from /home/redacted/env/lib/python2.7/site-packages (python 2.7)
Try installing:
sudo apt-get install python-setuptools
if this doesn't work try:
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
Edit: If you have several (possible conflicting) python installations or environments, the following commands can be useful to debug which executables are being used:
which python
which pip
which easy_install
They should "match". It can happen for example that you have pip installing packages for an EPD or global distribution while the current python that is being used corresponds to a local environment (or something different), in which case it might not be able to see the installed packages.
had the same problem, solved it with
pip install -U setuptools
Elaborating #elyase's Answer.
First check for which python version you want to install setuptools.
Normally both python versions comes default with debian or any linux distro.
So, as per your requirement install setup tools using apt package manager
For python 2.x
sudo apt-get install python-setuptools
For python 3.x
sudo apt-get install python3-setuptools
These instructions solved the problem for me:
first enter these commands
pip install --upgrade pip
pip install --upgrade wheel
pip install setuptools
and then try to install the package that requires setuptools.