Search code examples
pythonubuntupymysql

Importing PyMySQL


I'm on Ubuntu using Jupyter Notebook. I'm a newbie (to coding and ubuntu) trying to learn how to access databases with Python.

Can some explain what the problem I'm having is?

This is the process I went through:

I thought I had installed PyMySQL.

I ran this:

import pymysql

and got this error

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-3dee6b071588> in <module>()
----> 1 import pymysql.cursor

ImportError: No module named 'pymysql'

I assumed that I hadn't installed it and went to terminal and ran:

sudo pip install PyMySQL

and got this result

The directory '/home/username/.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. Requirement already satisfied: PyMySQL in /usr/local/lib/python2.7/dist-packages

I then ran:

sudo -h pip install PyMySQL

install: missing destination file operand after 'PyMySQL'
Try 'install --help' for more information.

Solution

  • Does Jupyter Notebook have some form of terminal emulator that you're using? If it does try using ubuntu's terminal. Ctrl+Alt+T is a shortcut to do so.

    Then try either one of these commands to download PyMySQL for your version of python, after installing pip.

    sudo apt-get install python-pip  
    
    sudo pip install PyMySQL
    sudo pip2 install PyMySQL 
    sudo pip3 install PyMySQL 
    

    Finally import the PyMySQL package.

    import pymysql