ImportError: No module named 'MySQL' - python

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.

Related

python: pip fails to install matplotlib v.3.1.1 [duplicate]

On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.
For example:
pip install beautifulsoup4
by default installs BeautifulSoup for Python 2.6
When I do:
import bs4
in Python 2.6 it works, but in Python 2.7 it says:
No module named bs4
Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:
python2.7 -m pip install foo
Use a version of pip installed against the Python instance you want to install new packages to.
In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).
pip's website includes installation instructions, if you can't find anything within your distribution.
You can execute pip module for a specific python version using the corresponding python:
Python 2.6:
python2.6 -m pip install beautifulsoup4
Python 2.7
python2.7 -m pip install beautifulsoup4
In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )
py -2 -m pip install pyfora
You can use this syntax
python_version -m pip install your_package
For example. If you're running python3.5, you named it as "python3", and want to install numpy package
python3 -m pip install numpy
Have tried this on a Windows machine and it works
If you wanna install opencv for python version 3.7, heres how you do it!
py -3.7 -m pip install opencv-python
Alternatively, if you want to install specific version of the package with the specific version of python, this is the way
sudo python2.7 -m pip install pyudev=0.16
if the "=" doesnt work, use ==
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16
Invalid requirement: 'pyudev=0.16'
= is not a valid operator. Did you mean == ?
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16
works fine
If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".
Python 2
sudo pip2 install johnbonjovi
Python 3
sudo pip3 install johnbonjovi
For Python 3
sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4
For Python 2
sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4
On Debian/Ubuntu, pip is the command to use when installing packages
for Python 2, while pip3 is the command to use when installing
packages for Python 3.
for python2 use:
py -2 -m pip install beautifulsoup4
I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).
Making a simple change worked for me.
When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.
If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only
As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.
# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"
I'm using Ubuntu 22.04, which comes with python 3.10.4.
Some packages do not have recent pip packages, so I needed install from an older pip. This sequence worked for me.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
sudo apt install python3.9-distutils
python3.9 -m pip install onnxruntime-gpu
Folder location: /usr/local/lib/python3.8
Package: python3.8 -m pip install <package_name>
I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.
Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).
I think the best practice here is not to use the system python or install any system python package (no apt install). That is just the way to trouble.
Instead, build the required Python version from source, get it installed in /usr/local/... . Then use pip to install packages for that. It is really not that hard to build Python from source on Ubuntu.
sudo apt install build-essential
download the source from https://www.python.org/downloads/source/
unpack the file downloaded: tar xf <filename>
cd <directory> - change into the directory created.
./configure
make
sudo make install
Then check /usr/local/bin for a pip script tied to that version. Use that to pip install whatever you need. Also find the particular executable for the python version in that directory. You might have to shuffle things a bit if you get lots of versions.
Again, do not mess with system python.

How to fix "module 'platform' has no attribute 'linux_distribution'" when installing new packages with Python3.8?

I had Python versions of 2.7 and 3.5. I wanted the install a newer version of Python which is python 3.8. I am using Ubuntu 16.04 and I can not just uninstall Python 3.5 due to the dependencies. So in order to run my scripts, I use python3.8 app.py. No problem so far. But when I want to install new packages via pip:
python3.8 -m pip install pylint
It throws an error:
AttributeError: module 'platform' has no attribute 'linux_distribution'
So far, I tried:
sudo update-alternatives --config python3
and chose python3.8 and run command by starting with python3 but no luck.
Then:
sudo ln -sf /usr/bin/python3.5 /usr/bin/python3
I also tried running the command by starting with python3 but it did not work either.
How can I fix it so that I can install new packages to my new version of Python?
It looks like at least on my Ubuntu 16.04, pip is shared for all Python versions in /usr/lib/python3/dist-packages/pip.
This is what I did to get it working again:
sudo apt remove python3-pip
sudo python3.8 -m easy_install pip
You might want to install the python 3.5 version of pip again with sudo python3.5 -m easy_install pip.
Python 3.8 removed some stuff. I solved my problems with pip (specifically pip install) by installing pip with curl.
What worked for me was downloading get-pip.py and run it with Python 3.8:
cd ~/Downloads
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py
Source: https://pip.pypa.io/en/stable/installing/
The problem is that package.linux_distribution was deprecated starting with Python 3.5(?). and removed altogether for Python 3.8.
Use the distro package instead. This package only works on Linux however.
I ran into this problem after installing OpenCobolIDE on Linux Mint 20, having upgraded Python to the latest level. have submitted a code fix to the OpenCobolIDE author to review and test. I was able to get the IDE to start up and run with this fix.
Essentially the fix uses the distro package if available, otherwise it uses the old platform package. For example:
This code imports distro if available:
import platform
using_distro = False
try:
import distro
using_distro = True
except ImportError:
pass
Then you can test the value of using_distro to determine whether to get the linux distro type from package or distro, for example:
if using_distro:
linux_distro = distro.like()
else:
linux_distro = platform.linux_distribution()[0]
In my case, removing python-pip-whl package helped:
apt-get remove python-pip-whl
It removed also pip and virtualenv, so I had to install them again:
curl https://bootstrap.pypa.io/get-pip.py | python3
pip install virtualenv
Check if your wheels installation is old. I was getting this same error and fixed it with
python3.8 -m pip install --upgrade pip setuptools wheel
Pylint seems to work on python3.8
I recently had this error and it turns out that I had a package called platform at a folder on my path ahead of the standard library and so the interpreter imported that instead. Check your path to what it is that you're actually importing.
If you have this issue when running a docker-compose up command. The solutions above do not work. You should install docker ce (https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)

pip install modules to 2.7.10 rather than 2.7.13 causing default python2.7.13 can't import those modules on MAC terminal window

As u can see from the question, I run sudo pip install pymysql successfully.
But it seems to be installed in 2.7.10 rather 2.7.13. I find it when tried to get python version before as well as after using sudo -i.
Then I listed all modules install using help('modules') and suprisingly to find 2.7.10 has but 2.7.13 don't have pymsql .
It failed to run pip2.7.13 install pymysql .Only pip2.7 install pymysql works .
I wonder how to config pip to install modules to 2.7.x or change my default python version from 2.7.13 to 2.7.10 ?
You may try to install python packages without root permission:
pip install --user pymysql
The option --user allow pip to install packages in your \home directory instead. That means it doesn't need any special privileges.
I finally find the cause:
My computer has an software named MAMP which has python2.7.13 env itself and pushes the python to the global.But pip will install modules into the computer's own python2.7.10, which causes now global python2.7.13 cannot import the installed modules.
Solution:
1,Find out the location of modules installed by pip and copy them to the
python directory in mamp. Commands as below:
cp -r /Library/Python/2.7/site-packages/pymysql/* /Applications/MAMP/Library/lib/python2.7/pymysql/
2, Then I can feely import modules installed by pip in global python now ~

No module named google.protobuf

I am trying to run Google's deep dream. For some odd reason I keep getting
ImportError: No module named google.protobuf
after trying to import protobuf. I have installed protobuf using sudo install protobuf. I am running python 2.7 OSX Yosemite 10.10.3.
I think it may be a deployment location issue but i cant find anything on the web about it. Currently deploying to /usr/local/lib/python2.7/site-packages.
There is another possibility, if you are running a python 2.7.11 or other similar versions,
sudo pip install protobuf
is ok.
But if you are in a anaconda environment, you should use
conda install protobuf
Locating the google directory in the site-packages directory (for the proper latter directory, of course) and manually creating an (empty) __init__.py resolved this issue for me.
(Note that within this directory is the protobuf directory but my installation of Python 2.7 did not accept the new-style packages so the __init__.py was required, even if empty, to identify the folder as a package folder.)
...In case this helps anyone in the future.
In my case I
downloaded the source code, compiled and installed:
$ ./configure
$ make
$ make check
$ sudo make install`
for python I located its folder(python) under source code, and ran commands:
$ python setup.py build
$ python setup.py install'
Not sure if this could help you..
I got the same error message when I tried to use Tensor Flow. The solution was simply to uninstall Tensor Flow and protobuf:
$ sudo pip uninstall protobuf
$ sudo pip uninstall tensorflow
And reinstall it again: pip installation of Tensorflow. Currently, this is:
# Ubuntu/Linux 64-bit, CPU only:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py2-none-any.whl
when I command pip install protobuf, I get the error:
Cannot uninstall 'six'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
If you have the same problem as me, you should do the following commands.
pip install --ignore-installed six
sudo pip install protobuf
According to your comments, you have multiply versions of python
what could happend is that you install the package with pip of anthor python
pip is actually link to script that donwload and install your package.
two possible solutions:
go to $(PYTHONPATH)/Scripts and run pip from that folder that way you insure
you use the correct pip
create alias to pip which points to $(PYTHONPATH)/Scripts/pip and then run pip install
how will you know it worked?
Simple if the new pip is used the package will be install successfully, otherwise the package is already installed
I installed the protobuf with this command:
conda install -c anaconda protobuf=2.6.1
(you should check the version of protobuf)
In my case, MacOS has the permission control.
sudo -H pip3 install protobuf
I had this problem to when I had a google.py file in my project files.
It is quite easy to reproduce.
main.py: import tensorflow as tf
google.py: print("Protobuf error due to google.py")
Not sure if this is a bug and where to report it.

Install a module using pip for specific python version

On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.
For example:
pip install beautifulsoup4
by default installs BeautifulSoup for Python 2.6
When I do:
import bs4
in Python 2.6 it works, but in Python 2.7 it says:
No module named bs4
Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:
python2.7 -m pip install foo
Use a version of pip installed against the Python instance you want to install new packages to.
In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).
pip's website includes installation instructions, if you can't find anything within your distribution.
You can execute pip module for a specific python version using the corresponding python:
Python 2.6:
python2.6 -m pip install beautifulsoup4
Python 2.7
python2.7 -m pip install beautifulsoup4
In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )
py -2 -m pip install pyfora
You can use this syntax
python_version -m pip install your_package
For example. If you're running python3.5, you named it as "python3", and want to install numpy package
python3 -m pip install numpy
Have tried this on a Windows machine and it works
If you wanna install opencv for python version 3.7, heres how you do it!
py -3.7 -m pip install opencv-python
Alternatively, if you want to install specific version of the package with the specific version of python, this is the way
sudo python2.7 -m pip install pyudev=0.16
if the "=" doesnt work, use ==
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16
Invalid requirement: 'pyudev=0.16'
= is not a valid operator. Did you mean == ?
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16
works fine
If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".
Python 2
sudo pip2 install johnbonjovi
Python 3
sudo pip3 install johnbonjovi
For Python 3
sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4
For Python 2
sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4
On Debian/Ubuntu, pip is the command to use when installing packages
for Python 2, while pip3 is the command to use when installing
packages for Python 3.
for python2 use:
py -2 -m pip install beautifulsoup4
I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).
Making a simple change worked for me.
When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.
If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only
As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.
# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"
I'm using Ubuntu 22.04, which comes with python 3.10.4.
Some packages do not have recent pip packages, so I needed install from an older pip. This sequence worked for me.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
sudo apt install python3.9-distutils
python3.9 -m pip install onnxruntime-gpu
Folder location: /usr/local/lib/python3.8
Package: python3.8 -m pip install <package_name>
I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.
Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).
I think the best practice here is not to use the system python or install any system python package (no apt install). That is just the way to trouble.
Instead, build the required Python version from source, get it installed in /usr/local/... . Then use pip to install packages for that. It is really not that hard to build Python from source on Ubuntu.
sudo apt install build-essential
download the source from https://www.python.org/downloads/source/
unpack the file downloaded: tar xf <filename>
cd <directory> - change into the directory created.
./configure
make
sudo make install
Then check /usr/local/bin for a pip script tied to that version. Use that to pip install whatever you need. Also find the particular executable for the python version in that directory. You might have to shuffle things a bit if you get lots of versions.
Again, do not mess with system python.

Categories