导入 PyMySQL

Importing PyMySQL

我正在 Ubuntu 使用 Jupyter Notebook。我是一个新手(编码和 ubuntu),正在尝试学习如何使用 Python 访问数据库。

有人能解释一下我遇到的问题是什么吗?

这是我经历的过程:

我以为我已经安装了 PyMySQL。

我运行这个:

import pymysql

遇到了这个错误

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

ImportError: No module named 'pymysql'

我以为我没有安装它并转到终端 运行:

sudo pip install PyMySQL

得到了这个结果

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

然后我运行:

sudo -h pip install PyMySQL

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

Jupyter Notebook 是否有您正在使用的某种形式的终端仿真器? 如果确实如此,请尝试使用 ubuntu 的终端。 Ctrl+Alt+T 是这样做的捷径。

然后在安装 pip 后尝试使用这些命令之一下载 PyMySQL 适用于您的 python 版本。

sudo apt-get install python-pip  

sudo pip install PyMySQL
sudo pip2 install PyMySQL 
sudo pip3 install PyMySQL 

最后导入 PyMySQL 包。

import pymysql

我在终端输入

sudo pip3 install PyMySQL

收到以下消息和回复。

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.

Collecting PyMySQL
  Downloading PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 1.1MB/s 
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.7.11

在 Jupyter I 中 运行

import pymysql

没有收到任何错误消息。好像有效果。