8

I'm trying to set up a python 3.6 environment with Django. The installation instructions say I should install mysqlclient to be able to connect to mySQL. I get this:

dennis@django:~$ sudo -H pip3 install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-1.3.10.tar.gz
    Complete output from command python setup.py egg_info:
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-4jiw3hvk/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/tmp/pip-build-4jiw3hvk/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-build-4jiw3hvk/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-4jiw3hvk/mysqlclient/
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

mySQL is properly installed. What should be in the mysql_config file? When I try to upgrade pip3 I get this:

dennis@django:~$ sudo -H pip3 install --upgrade pip3
Collecting pip3
  Could not find a version that satisfies the requirement pip3 (from versions: )
No matching distribution found for pip3
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

After installing mysqlclient with pip3, I noticed that the module was installed in the python3.5 directory instead of in the python3.6 directory so when I tried to import the MySQLdb module running python3.6, it was not found

dennis@django:~/python_db$ python
Python 3.6.0+ (default, Feb  4 2017, 11:11:46) 
[GCC 5.4.1 20161202] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MySQLdb'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MySQLdb'
>>> 

I then reran using python 3.5 and it was found. How do I install modules using pip3 into the 3.6 directory?

Dennis M. Gray
  • 332
  • 1
  • 3
  • 17

9 Answers9

33

If you're using Mac OS, try this:

brew install mysql

If you're using Ubuntu14/16, try this:

sudo apt install libmysqlclient-dev

and one more:

pip3 can be updated with sudo pip3 install -U pip

Beomi
  • 1,697
  • 16
  • 18
  • 1
    I am on Ubuntu 16.04 MySQL is installed and works. I am trying to install a python module called mysqlclient. By installing the libmysqlclient-dev I was able to successfully install the mysqlclient module Still no luck upgrading pip3. What you wrote didn't work. dennis@django:~$ sudo pip3 upgrade -U pip ERROR: unknown command "upgrade" – Dennis M. Gray Mar 07 '17 at 16:03
  • mysql being installed is not the same as mysqlclient being installed. Try upgrading your pip and/or doing an apt install of the libmysqlclient-dev packages. And read the error message.... it clearly states to install pip run "pip install --upgrade pip" in your case change it to "pip3 install --upgrade pip" – SuperTetelman Mar 07 '17 at 16:08
  • Your Q has 2 Qs... so I answered both. – Beomi Mar 07 '17 at 16:09
  • You do not have to upgrade to install mysqlclient. – Beomi Mar 07 '17 at 16:10
  • One more, do `sudo python3 -m pip install -U pip` – Beomi Mar 07 '17 at 16:10
  • I mentioned that I was able to successfully install mysqlclient after I installed libmysqlclient-dev. I'm starting to test that tonight. – Dennis M. Gray Mar 08 '17 at 03:16
  • Beomi: That worked and I got pip 9.0.1 install for python3...thanks – Dennis M. Gray Mar 08 '17 at 03:18
  • I am still not able to install mysqlclient in the python3.6 path. I have tried using virtualenv but it continues to install it in the wrong directory. In fact, it installs it in the 2.7 directory when the virtualenv was created with the path to 3.6. – Dennis M. Gray Mar 08 '17 at 05:46
  • did u activated virtualenv? – Beomi Mar 08 '17 at 05:47
  • Yes source django/bin/activate – Dennis M. Gray Mar 08 '17 at 17:01
  • Should I start this is a new question? I cannot install mysqlclient into the python3.6 path. All the details are in my edited question – Dennis M. Gray Mar 09 '17 at 17:21
10
LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysqlclient
Unheilig
  • 16,196
  • 193
  • 68
  • 98
6

In my case the issue was solved by doing the following:

sudo aptitude install libmysqlclient-dev

As I was using Python 3.x, the following was also necessary:

sudo aptitude install python3-dev
ryuzakyl
  • 511
  • 8
  • 14
3

If you're running MAMP on OSX, do this:

export PATH=$PATH:/Applications/MAMP/Library/bin
Samuurai
  • 375
  • 2
  • 13
3

In Debian Stretch the required package name seems to have changed to default-libmysqlclient-dev

https://packages.debian.org/stretch/default-libmysqlclient-dev

Paul Hayes
  • 31
  • 1
0

you can try this to install mysql to a particular python directory, for example w/ python 3.6

sudo python3.6 /usr/bin/pip3 install mysqlclient
rajiv.cla
  • 356
  • 3
  • 7
0

Looking over this old question (while looking for something else), I observe that the problem could be that the directory containing mysql_config was not in the current $PATH. Apparently that's what the install script uses to try to find it. It's possible for "mysql to be installed," but not on the $PATH – or for the $PATH to refer to the wrong version.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
0

Run sudo apt-get install python3-dev default-libmysqlclient-dev build-essential before pip3 install mysqlclient should work.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '23 at 02:35
0

MacOS, In the official documentation(https://pypi.org/project/mysqlclient/), you can install like this:

# Assume you are activating Python 3 venv
$ brew install mysql-client
$ echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile
$ export PATH="/usr/local/opt/mysql-client/bin:$PATH"
$ pip install mysqlclient

you need to pay attention to the fact that the installation directory may be inconsistent with the official path. You need to use brew list mysql-client to view the corresponding path to Replace the corresponding environment variable.