【问题标题】:Python - No module named 'PyMySQL'Python - 没有名为“PyMySQL”的模块
【发布时间】:2017-03-05 13:53:05
【问题描述】:

我正在尝试添加 'PyMySQL,但它一直在抛出

Traceback (most recent call last):
  File "/home/john/workspace/Python/sql/sql.py", line 1, in <module>
    import PyMySQL
ImportError: No module named 'PyMySQL'

我尝试通过 pip 安装它并再次运行它,但错误并没有消失。关于什么是错的任何想法?

john@john-PC:$ sudo pip install PyMySQL
The directory '/home/john/.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/john/.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
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading PyMySQL-0.7.9-py2-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 1.3MB/s 
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.7.9

john@john-PC:$ pip install PyMySQL
Requirement already satisfied (use --upgrade to upgrade): PyMySQL in /usr/local/lib/python2.7/dist-packages

john@john-PC:$ sudo apt-get install python3-pymysql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pymysql is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 56 not upgraded.

sql.py

import PyMySQL
print("Here")

解决方案。 这是以下两个答案的组合,以及在线错误报告。

apt-get install software-properties-common
add-apt-repository cloud-archive:mitaka
apt-get update && apt-get dist-upgrade 
sudo apt-get install python3-pymysql
import pymysql

【问题讨论】:

  • 如何运行你的脚本?
  • @Loïc "/usr/bin/python3 sql.py"
  • Python 3.4.3(默认,2016 年 9 月 14 日,12:36:27)[GCC 4.8.4] 在 linux 上键入“帮助”、“版权”、“信用”或“许可证”更多信息。 >>> pymysql Traceback(最近一次调用最后):文件“”,第 1 行,在 NameError: name 'pymysql' is not defined >>>

标签: python mysql debian pymysql


【解决方案1】:

尝试运行

sudo apt-get install python3-pymysql

对于python3。

已经在stackoverflow.com回答了

【讨论】:

  • sudo apt-get install python3-pymysql 正在读取包列表...完成 构建依赖树 正在读取状态信息...完成 E: 无法定位包 python3-pymysql
  • 我不得不运行一些升级/和其他库。然后把名字改成pymysql
  • 谢谢@John,我的问题也是使用大写字母
【解决方案2】:

安装好“PyMySQL”后,还需要更新the _init__.py,添加import pymysql
pymysql.install_as_MySQLdb()
,并将base.py中的MySQLdb替换为pymysql。

【讨论】:

    【解决方案3】:

    我最近遇到了同样的问题。我用这个解决了它:

    sudo pip-3.2 install pymysql**3**
    

    pymysql3 中的那 3 个使一切变得不同。

    它在树莓派上,喘不过气来

    【讨论】:

      【解决方案4】:

      即使在安装了正确的软件包之后,它仍然抛出错误的原因是因为正在使用的名称。它应该是“import pymysql”而不是“import PyMySQL”。这对我有用。

      【讨论】:

        【解决方案5】:

        使用 Debian 9 并从 BASH 调用 python3。

        import pymysql
        

        为我工作

        import PyMySql
        

        失败

        Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 NameError: 名称 'PyMySQL' 未定义

        【讨论】:

          【解决方案6】:

          这对我有用:

          #!/usr/bin/env python3
          
          #Importing needed modules of PyMySQL
          from pymysql import connect, err, sys, cursors
          
          #Doing our connection
          conn = connect( host = 'localhost',
          
          cursor = conn.cursor( cursors.DictCursor );
          

          【讨论】:

            【解决方案7】:

            我发现如果我使用 PyCharm 的解释器添加包 GUI,那么我的计算机可以找到 pymysql。

            我只在使用 VSCode 时遇到过这个问题。

            【讨论】:

              猜你喜欢
              • 2016-01-31
              • 2021-03-03
              • 1970-01-01
              • 2020-11-08
              • 2019-04-29
              • 1970-01-01
              • 2017-03-09
              • 1970-01-01
              • 2022-10-01
              相关资源
              最近更新 更多