报错NameError: name ‘_mysql‘ is not defined 环境:Ubuntu 18.04.6、django3.2、python3.7

1,153 阅读3分钟

报错内容

执行python manage.py runserver

    import MySQLdb as Database
  File "/home/geology/env/lib/python3.7/site-packages/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

^C^C__vsc_prompt_cmd_original: command not found

我的是mysqlclient出现问题

pip uninstall mysqlclient
pip install mysqlclient

重新安装就失败了 遇到

(env) root@VM-0-16-ubuntu:/home/geology/geology# pip install mysqlclient
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting mysqlclient
  Downloading http://mirrors.tencentyun.com/pypi/packages/50/5f/eac919b88b9df39bbe4a855f136d58f80d191cfea34a3dcf96bf5d8ace0a/mysqlclient-2.1.1.tar.gz (88 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.1/88.1 kB 873.8 kB/s eta 0:00:00
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      /bin/sh: 1: mysql_config: not found
      /bin/sh: 1: mariadb_config: not found
      /bin/sh: 1: mysql_config: not found
      Traceback (most recent call last):
        File "<string>", line 36, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-1ck1a31j/mysqlclient_8d6b7021c9964671bd0e5d48a8c64951/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/tmp/pip-install-1ck1a31j/mysqlclient_8d6b7021c9964671bd0e5d48a8c64951/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/tmp/pip-install-1ck1a31j/mysqlclient_8d6b7021c9964671bd0e5d48a8c64951/setup_posix.py", line 31, in mysql_config
          raise OSError("{} not found".format(_mysql_config_path))
      OSError: mysql_config not found
      mysql_config --version
      mariadb_config --version
      mysql_config --libs
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

这个错误通常表示在安装mysqlclient模块时,Python解释器无法找到mysql_config程序,因此无法完成安装。mysql_config是MySQL的一个配置程序,用于确定MySQL的安装位置、库位置和头文件位置等信息,因此它对于安装mysqlclient模块非常重要。

为了解决这个问题,你需要确保安装了MySQL,并且mysql_config程序已经在你的PATH环境变量中。如果你已经安装了MySQL,但是mysql_config仍然无法找到,你可以尝试以下方法:

  1. 检查mysql_config程序是否已经在PATH环境变量中:

    在命令行中输入以下命令:

    
    mysql_config --version
    

    如果该命令返回MySQL版本信息,则mysql_config已经在你的PATH环境变量中。否则,请按照以下步骤添加mysql_config到PATH环境变量中:

  2. 确认已经安装了MySQL服务器和客户端库:

    在终端中运行以下命令:

    sudo apt-get install mysql-server libmysqlclient-dev
    

    这将安装MySQL服务器和MySQL客户端库。

  3. 确认mysql_config在PATH环境变量中:

    在终端中运行以下命令:

    mysql_config --version
    

    如果该命令返回MySQL版本信息,则mysql_config已经在你的PATH环境变量中。否则,请按照以下步骤添加mysql_config到PATH环境变量中:

    • 在终端中运行以下命令:

      sudo find / -name mysql_config
      

      该命令将在你的计算机上查找mysql_config程序的位置。通常,mysql_config位于MySQL的bin目录下。

    • 将mysql_config所在的目录添加到PATH环境变量中:

      在终端中运行以下命令:

      export PATH=$PATH:/path/to/mysql/bin
      

      其中/path/to/mysql/bin是mysql_config所在的目录。

  4. 安装mysqlclient模块:

    在终端中运行以下命令:

    pip3 install mysqlclient
    或指定版本
    pip3 install mysqlclient==2.1.1
    

    如果一切顺利,该命令应该能够成功安装mysqlclient模块。

[引 ChatGPT]