ImportError: No module named _mysql
Question: I'm trying to use the Python module MySQL-python to connect to an external
MySQL database from an AWS EC2 instance running amazon linux.

This is the code I'm trying to run:

    
    
    db=_mysql.connect(host="hostname",user="dbuser",passwd="dbpassword",db="database")
    db.query("""SELECT id, field1 FROM test""")
    r=db.store_result()
    row = r.fetch_row()
    print row
    

I have installed the python module with pip:

    
    
    sudo pip install MySQL-python
    

When I run the script I get the following error message:

    
    
    Traceback (most recent call last):
      File "script.py", line 2, in <module>
        import _mysql
    ImportError: No module named _mysql
    

When I research this I keep on digging up a lot of solutions for Ubuntu/Debian
linux that don't work for amazon linux.

How can I fix this error on amazon linux and run the script?

Also, from any experienced linux users observing/answering: Is there any
advantage to using amazon linux as I try to learn more linux and pick up AWS
or would I be better off using an Ubuntu/Debian image? I'm not an experienced
linux user as probably shows from the question.

**Update**

I've realised that the installation of the package was unsuccessful on the
amazon linux server. Here's the full output when I try to run the install via
pip:

    
    
    $ sudo pip install MySQL-Python
    Collecting MySQL-Python
      Using cached MySQL-python-1.2.5.zip
    Installing collected packages: MySQL-Python
      Running setup.py install for MySQL-Python ... error
        Complete output from command /usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-B1IkvH/MySQL-Python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-RNgtpa-record/install-record.txt --single-version-externally-managed --compile:
        running install
        running build
        running build_py
        creating build
        creating build/lib.linux-x86_64-2.7
        copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
        creating build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
        creating build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        running build_ext
        building '_mysql' extension
        creating build/temp.linux-x86_64-2.7
        gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql55 -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
        unable to execute 'gcc': No such file or directory
        error: command 'gcc' failed with exit status 1
    
        ----------------------------------------
    Command "/usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-B1IkvH/MySQL-Python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-RNgtpa-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-B1IkvH/MySQL-Python/
    


Answer: Only a workaround, but one that worked for me in situations where I could not
easily call "sudo pip install".

What you (often, not always) can do:

  1. Turn to a system where that python module you are looking for works
  2. Identify its "location", for example, after installing enum34 on my ubuntu, the installation would put files under _/usr/lib/python2.7/dist-packages/enum_
  3. Put that directory in an archive
  4. On your "target" system, extract that archive locally
  5. Manipulate the python path to include the locally extracted archive

As said, this isn't beautiful; but if no better answers come in; you have at
least something to try ...

