Helping installing a Python MySQL connection

software development

#1

Hobbyist programmer here…

I got Python installed on my account.

Assumed the mysql connector was already installed.

Yet both of these failed :

Python 2.7.10 (default, Apr 16 2017, 16:14:40)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import mysql-connector
File “”, line 1
import mysql-connector
^
SyntaxError: invalid syntax

import MySQLdb
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named MySQLdb

Tried installing :

[evergreen]$ pip install MySQL-python

and got this message:

Requirement already satisfied (use --upgrade to upgrade): MySQL-python in /usr/lib/python2.7/dist-packages
Cleaning up…

These still failed :

Python 2.7.10 (default, Apr 16 2017, 16:14:40)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import mysql-connector
File “”, line 1
import mysql-connector
^
SyntaxError: invalid syntax

import MySQLdb
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named MySQLdb

Not sure where to go from here.

Can anyone help?

Please assume I know next to nothing about Linux.


#2

Which environment are you trying to run this code on? I just tried this on a VPS account and got no error.

(py)[wp558056]$ pip install mysql-python
Downloading/unpacking mysql-python
  Downloading MySQL-python-1.2.5.zip (108Kb): 108Kb downloaded
  Running setup.py egg_info for package mysql-python
    
Installing collected packages: mysql-python
  Running setup.py install for mysql-python
    building '_mysql' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g
    In file included from _mysql.c:44:0:
    /usr/include/mysql/my_config.h:423:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
    /usr/include/python2.7/pyconfig.h:890:0: note: this is the location of the previous definition
    gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
    
Successfully installed mysql-python
Cleaning up...
(py)[wp558056]$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 
(py)[wp558056]$ 

#3

Not the VPS. I have the standard web hosting for $11 a month. I had to install Python myself to it if that means anything. I have a another thread here showing how I did it.


#4

Ah, wonderful, thanks for clarifying. That explains :slight_smile: I bet what is happening is that the paths for the python libraries are not set correctly. Here is another, much easier method to install python in your own account: setup and use a virtualenv. You should learn about virtualenv if you want to use python anyway :slight_smile:

This is what I did on a shared hosting account:

 ## setup a virtual python environment in your home dir, call it anything ('py' in this example)
[elrond]$ virtualenv py
New python executable in py/bin/python
Installing distribute.............................................................................................................................................................................................done.
Installing pip...............done.

## move to the newly created dir
[elrond]$ cd py/

## activate the new virtualenv
[elrond]$ . ./bin/activate

## install the libs you need with pip
(py)[elrond]$ pip install mysql-python
Downloading/unpacking mysql-python
  Downloading MySQL-python-1.2.5.zip (108Kb): 108Kb downloaded
  Running setup.py egg_info for package mysql-python
    
Installing collected packages: mysql-python
  Running setup.py install for mysql-python
    building '_mysql' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g
    In file included from _mysql.c:44:0:
    /usr/include/mysql/my_config.h:423:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
    /usr/include/python2.7/pyconfig.h:890:0: note: this is the location of the previous definition
    gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
    
Successfully installed mysql-python
Cleaning up...

## test things out
(py)[elrond]$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 

If all you’re doing is running experiments, you may also try DreamCompute where you can create and destroy full virtual machines of your favorite Linux distributions. You’ll also be root on those machines, meaning you won’t have to deal with the limitations of shared accounts. They’re great for learning and experimentation purposes.


#5

Thank you. Too tired to do tonight, but maybe tomorrow or Monday.


#6

Worked like a Champ! Thank you smaffulli.


#7

I found this thread while looking to solve a similar problem. I can log, create a python virtual environment, install modules, and manually invoke python. But when I visit my website and the page runs a short python script to generate some content, apparently it is not using my virtualenv for that browser processes.

How do I configure things to run the page generation process using my virtualenv instead of whatever default python environment it is using? I found a page to install a custom version of python, and maybe that is the solution, but I don’t need a new version of python … just for dreamhost to use my virtualenv.

Thanks for any tips.


#8

Thanks for the post !! Connecting to MySQL Using Connector/Python:

The connect() constructor creates a connection to the MySQL server and returns a MySQLConnection object.

Source: https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html


#9

Yes also I would recomend you to use MySQL connector Python to connect to MySQL. As it is Python 3 compatible and purly written in Python

MySQL Connector Python requires python to be in the system’s PATH. Installation fails if python cannot be located.

Unix and Unix-like systems, python is normally located in a directory included in the default PATH setting.

Refer this article for install and import python MySQL connector. Install MySQL connector Python
Refer this for Python MySQL connection