【问题标题】:MySQL is not being recognized in PythonMySQL 没有在 Python 中被识别
【发布时间】:2021-04-09 04:00:38
【问题描述】:

我已经成功下载并安装了 MySQL 连接器/Python:

pip install mysql-connector-python

但是当我在终端(Visual Studio Code)中运行我的 Python 文件时,会显示这个错误:

ModuleNotFoundError: No module named 'mysql'

这是我的代码:

import mysql.connector

database = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd=""
    #database="master_python"
)

我尝试使用以下方式安装 mysql-connector-python-rf:

pip install mysql-connector-python-rf

但向我显示了一个大的、红色的和可怕的错误:

ERROR: Command errored out with exit status 1:
     command: 'c:\program files\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Enrique Rubio\\AppData\\Local\\Temp\\pip-install-w1fo5muv\\mysql-connector-python-rf_a462ab4d49084651a288ddeacc8db2f3\\setup.py'"'"'; __file__='"'"'C:\\Users\\Enrique Rubio\\AppData\\Local\\Temp\\pip-install-w1fo5muv\\mysql-connector-python-rf_a462ab4d49084651a288ddeacc8db2f3\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Enrique Rubio\AppData\Local\Temp\pip-record-5uwbo9pj\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\Enrique Rubio\AppData\Roaming\Python\Python38\Include\mysql-connector-python-rf'
         cwd: C:\Users\Enrique Rubio\AppData\Local\Temp\pip-install-w1fo5muv\mysql-connector-python-rf_a462ab4d49084651a288ddeacc8db2f3\
    Complete output (6 lines):
    usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or: setup.py --help [cmd1 cmd2 ...]
       or: setup.py --help-commands
       or: setup.py cmd --help

    error: option --single-version-externally-managed not recognized
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\program files\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Enrique Rubio\\AppData\\Local\\Temp\\pip-install-w1fo5muv\\mysql-connector-python-rf_a462ab4d49084651a288ddeacc8db2f3\\setup.py'"'"'; __file__='"'"'C:\\Users\\Enrique Rubio\\AppData\\Local\\Temp\\pip-install-w1fo5muv\\mysql-connector-python-rf_a462ab4d49084651a288ddeacc8db2f3\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Enrique Rubio\AppData\Local\Temp\pip-record-5uwbo9pj\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\Enrique Rubio\AppData\Roaming\Python\Python38\Include\mysql-connector-python-rf' Check the logs for full command output.

我不明白为什么 MySQL 没有被识别。请有人可以帮助我。

【问题讨论】:

标签: python mysql pip


【解决方案1】:

您能否确认您的系统上只安装了一个 python 版本?

如果您确实安装了多个版本,请尝试pip3 或相应的子版本。

如果没有帮助,请尝试从此处下载并安装二进制文件:

https://dev.mysql.com/downloads/connector/python/

【讨论】:

  • 当我从该链接安装二进制文件时显示错误:访问此目录的权限不足...
  • 请不要推荐 pip3 脚本之类的东西,相反,最好建议为特定的 Python 解释器调用 pip 的可执行模块:@987654325 @ -- snarky.ca/why-you-should-use-python-m-pip
猜你喜欢
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多