getting error when iusing pip3 install mysql

adbyits
adbyits used Ask the Experts™
on
Hi all i am trying to setup my ubuntu server so i can run my python scripts and when i try to install mysql-client or mysql for python i am getting a error
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nwk7rejx/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-10shw23u-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-nwk7rejx/mysqlclient/

i have  tried good old google and all the ones i have found on there to try didnt work any help would be great
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Is this the whole error message?
I would have expected some complaints about missing C/C++ header files.

Do you try installing in a virtualenv or in the default python? (It seems to be the system python)

I'd suggest to create a brand new virtualenv and try installing there and copy the entire error message.
John TsioumprisSoftware & Systems Engineer

Commented:
Take  a look if my solution solves it for you

Author

Commented:
The directory '/home/ADBY/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ADBY/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting mysqlclient
  Downloading https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB)
    11% |███▉                            | 10kB 33.2MB/s eta 0:00:    23% |███████▋                        | 20kB 21.6MB/s eta 0:00:    35% |███████████▌                    | 30kB 25.3MB/s eta 0:00:    47% |███████████████▎                | 40kB 9.9MB/s eta 0:00:0    59% |███████████████████▏            | 51kB 11.4MB/s eta 0:00:    71% |███████████████████████         | 61kB 13.1MB/s eta 0:00:    83% |██████████████████████████▉     | 71kB 14.2MB/s eta 0:00:    95% |██████████████████████████████▋ | 81kB 15.5MB/s eta 0:00:    100% |████████████████████████████████| 92kB 8.0MB/s
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nwk7rejx/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-10shw23u-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
    creating build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/MySQLdb
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/include/mysql -I/usr/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o
    x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o -lmysqlclient -lpthread -lz -lm -lrt -latomic -lssl -lcrypto -ldl -o build/lib.linux-x86_64-3.6/MySQLdb/_mysql.cpython-36m-x86_64-linux-gnu.so
    /usr/bin/ld: cannot find -lssl
    /usr/bin/ld: cannot find -lcrypto
    collect2: error: ld returned 1 exit status
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nwk7rejx/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-10shw23u-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-nwk7rejx/mysqlclient/
ADBY@Flight-Check-Server:~/
Commented:
This ended up working
pip install mysql-connector
import mysql.connector
OK just as info. about the root cause and some recommended strategies.

In general it is not a good idea to pip install packages with sudo.  You might enter in conflict with the packages installed with apt / by your distribution and debugging strange problems will be very difficult from then on.

In general I recommend to create a virtualenv for each project and install packages in this env.

Apart from that.
The packages mysql-client / mysql are packages, that are written in C and have to be compiled during pip installation.
This compilation fails often if certain packages were not installed on your machine. This kind of error manifests itself most of the time by error messages complaining that a header file cannot be found. (e.g. ssl.h or something similiar) or during the linking phase. like "/usr/bin/ld: cannot find -lssl".

You might try installing following apt-packages before running pip install.

sudo apt-get install python-pip python-dev libmysqlclient-dev libssl-dev libcrypto++-dev 

Open in new window


If the version of your linux distribution is good enough for you, then just look at following apt packages:
python-mysqldb 
python-pymysql 
python-mysql.connector 


python3-pymysql 
python3-mysqldb 
python3-mysql.connector 

Open in new window


As general strategy I recommend to use apt install for your system python and use pip install only for virutalenvs ( https://virtualenv.pypa.io/en/stable/ )
if you want to use python versions, that don't exist for your ubuntu version, then look also at pyenv ( https://github.com/pyenv/pyenv ) which can be (if you want also combined with virtualenv

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial