5

I'm trying out Python 3 with Flask and I'm stuck with the following error while working with databases.

I'm doing this on macOS High Sierra v. 10.13.6

My import code is as follows:

from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordFeild, validators
from passlib.hash import sha256_crypt

The error I get when trying to run the app, is the following:

Traceback (most recent call last):
    File "app.py", line 3, in <module>
        from flask_mysqldb import MySQL  
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
          packages/flask_mysqldb/__init__.py", line 1, in <module> import MySQLdb  
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
          packages/MySQLdb/__init__.py", line 18, in <module> import _mysql  
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
             packages/_mysql.cpython-37m-darwin.so, 2): Library not loaded: 
             libssl.1.0.0.dylib  
    Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
                     packages/_mysql.cpython-37m-darwin.so  
    Reason: image not found

I've looked around in multiple other questions related to mine, but couldn't seem to find anything to solve my problem with. Or at least I didn't know how. I really hope someone can help me out.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
M. Quist
  • 211
  • 1
  • 2
  • 8
  • brew install mysql ? – jtlz2 Aug 22 '18 at 12:53
  • Already done, same error :/ – M. Quist Aug 22 '18 at 12:55
  • `pip install mysql`. @M.Quist hope it works – jalazbe Aug 22 '18 at 12:58
  • @jalazbe that gives me the following error: `Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-install-juotsd7s/MySQL-python/` in the terminal – M. Quist Aug 22 '18 at 12:59
  • have you installed mysql on your computer? – jalazbe Aug 22 '18 at 13:23
  • @jalazbe I have indeed – M. Quist Aug 22 '18 at 13:24
  • try doing `pip install flask_mysqldb`. Then open a new terminal. Type `python`, then type `import flask_mysqldb`. If no errors are prompted you should be ok – jalazbe Aug 22 '18 at 13:28
  • Bad I know but you can also do `sudo pip install mysql` followed by `python -c 'import flask_mysqldb'` - no output means you are good to go. – jtlz2 Aug 22 '18 at 13:30
  • @jalazbe Having tried that already by reading other questions, I can say that it gives me the error written in my question once again – M. Quist Aug 22 '18 at 13:30
  • Also try (sudo) `pip install --upgrade setuptools` before you do pip install mysql – jtlz2 Aug 22 '18 at 13:31
  • @jtlz2 Installing mysql with pip/pip3 gives me the error in comment three – M. Quist Aug 22 '18 at 13:33
  • 1
    Read this. It might help https://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib – jalazbe Aug 22 '18 at 13:33
  • Can you try it with `sudo pip install --upgrade setuptools; sudo pip install mysql` first? – jtlz2 Aug 22 '18 at 13:34
  • @jtlz2 Gives my this error when running: `Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-install-cUPsLz/MySQL-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/tmp/pip-record-E8aY4Y/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-install-cUPsLz/MySQL-python/` – M. Quist Aug 22 '18 at 13:35
  • I'm so sorry - going to keep quiet now.. – jtlz2 Aug 22 '18 at 13:41
  • 1
    @jtlz2 No worries, thanks for helping out! – M. Quist Aug 22 '18 at 13:42
  • Except possibly: Try link/unlink mysql with brew – jtlz2 Aug 22 '18 at 13:42

6 Answers6

16

Solved the Issue

After doing some more search I found a fix that worked for me:

Step 1: Install openssl using brew

brew install openssl

Step 2: Copy libssl.1.0.0.dylib and libcrypto.1.0.0.dylib

cd /usr/local/Cellar/openssl/1.0.1f/lib
sudo cp libssl.1.0.0.dylib libcrypto.1.0.0.dylib /usr/lib/

Note the folder name (1.0.1f). There will be change in that depending on your openssl version

Step 3: Remove the existing links

sudo rm libssl.dylib libcrypto.dylib
sudo ln -s libssl.1.0.0.dylib libssl.dylib
sudo ln -s libcrypto.1.0.0.dylib libcrypto.dylib

That's it.

M. Quist
  • 211
  • 1
  • 2
  • 8
6

Following worked for me with mysql Ver 14.14 Distrib 5.7.23, for osx10.14 (x86_64) & Django 2.2.3.

  1. Install version 1.1
   brew install openssl@1.1
  1. Copy required library to /usr/lib location. Refer to article at this url if you have trouble copying to /usr/lib location. System Integrity Change
    cd /usr/local/Cellar/openssl@1.1/1.1.1c/lib
    sudo cp libssl.1.1.dylib libcrypto.1.1.dylib /usr/lib/
  1. Remove and update link
    sudo rm libssl.dylib libcrypto.dylib
    sudo ln -s libcrypto.1.1.dylib libcrypto.dylib
    sudo ln -s libssl.1.1.dylib libssl.dylib
Baskaya
  • 7,651
  • 6
  • 29
  • 27
sukumarpsurya
  • 61
  • 1
  • 4
  • Thank you for your solution! This was the one that did it for me. I had to disable the Mac's SIP in order to copy the files to /usr/lib. Perhaps there could have been a non SIP directory that it could have been copied without going into recovery mode – Mark D Sep 17 '19 at 22:29
  • 5
    You can also get around this problem without disabling MacOS SIP by simply copying the files to `/usr/local/lib`. That worked for me. – Doug Couvillion Feb 05 '20 at 16:25
  • 1
    Not a good idea to copy those files into /usr/lib: https://apple.stackexchange.com/questions/238828/root-user-cant-create-a-file-in-usr-lib – Baskaya May 26 '20 at 00:26
  • 1
    It says "Operation not permitted." for `sudo cp libssl.1.1.dylib libcrypto.1.1.dylib /usr/lib/` – Shahaf Shavit Feb 22 '21 at 15:30
0

Earlier I was getting the following error.

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/osgeo/_gdal.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
  Referenced from: /usr/local/opt/gdal/lib/libgdal.20.dylib
  Reason: image not found

for the following import

import gdal

Just create the directory in the desired location

My installation was here...

/usr/local/Cellar/openssl/1.0.2s/

I created a directory at their desired location. openssl directory was not there, I mkdir ed it.

/usr/local/opt/openssl/

Then copied the folder as required. Now

import gdal

works.

tor9ado
  • 361
  • 2
  • 12
0

My approach to this problem was this:

Instead of:

from flask_mysqldb import MySQL

I used:

from flaskext.mysql import MySQL

So this means that I pip install flask-mysql instead of pip install flask-mysqldb. Note: if you want to obtain a cursor, with this lib you can do cursor = mysql.get_db().cursor()

0

In case of Mac, in the ~/.bash_profile where you are updating the openssl path. Escape the openssl@1.1 as openssl\@1.1.

export PATH="/usr/local/opt/openssl\@1.1/bin:$PATH" export LDFLAGS="-L/usr/local/opt/openssl\@1.1/lib" export CPPFLAGS="-I/usr/local/opt/openssl\@1.1/include"

0

For me, I already had openssl installed and the solution was a simple one -- re-installing openssl:

brew reinstall openssl@1.1

After that, the imports in python worked smoothly.

josliber
  • 43,891
  • 12
  • 98
  • 133