最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】python调用torndb出错:import MySQLdb.constants ModuleNotFoundError: No module named MySQLdb

Python crifan 4951浏览 0评论

折腾:

【记录】Mac本地Solr中新建qa的collection再运行脚本导入数据

期间,去调试代码:

from util import database as dbcon

def mysql2solr_transfer():

    cnx = dbcon.connectTorndb()

def connectTorndb():

    dbconfig = configs.getMysqlConfigDict()

    conn = torndb.Connection(dbconfig.get(‘host’), ‘xxx’, dbconfig.get(‘user’), dbconfig.get(‘password’),

                             time_zone=’+8:00′, charset=’utf8mb4′)

    return conn

结果出错:

Traceback (most recent call last):

  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1664, in <module>

    main()

  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1658, in main

    globals = debugger.run(setup[‘file’], None, None, is_module)

  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1068, in run

    pydev_imports.execfile(file, globals, locals)  # execute the script

  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile

    exec(compile(contents+"\n", file, ‘exec’), glob, loc)

  File "/Users/crifan/dev/dev_root/xxx/utils/mysql2solr.py", line 17, in <module>

    from util import database as dbcon

  File "/Users/crifan/dev/dev_root/xxx/util/database.py", line 5, in <module>

    import torndb

  File "/Users/crifan/.virtualenvs/xxx-gXiJ4vtz/lib/python3.6/site-packages/torndb.py", line 33, in <module>

    import MySQLdb.constants

ModuleNotFoundError: No module named ‘MySQLdb’

Process finished with exit code 1

然后以为是没有安装torndb呢,去看看,发现已经安装了:

➜  xx git:(master) ✗ pipenv graph

gensim==3.5.0

  – numpy [required: >=1.11.3, installed: 1.15.0]

  – scipy [required: >=0.18.1, installed: 1.1.0]

    – numpy [required: >=1.8.2, installed: 1.15.0]

  – six [required: >=1.5.0, installed: 1.11.0]

  – smart-open [required: >=1.2.1, installed: 1.6.0]

    – boto [required: >=2.32, installed: 2.49.0]

    – boto3 [required: Any, installed: 1.7.80]

      – botocore [required: >=1.10.80,<1.11.0, installed: 1.10.80]

        – docutils [required: >=0.10, installed: 0.14]

        – jmespath [required: >=0.7.1,<1.0.0, installed: 0.9.3]

        – python-dateutil [required: >=2.1,<3.0.0, installed: 2.7.3]

          – six [required: >=1.5, installed: 1.11.0]

      – jmespath [required: >=0.7.1,<1.0.0, installed: 0.9.3]

      – s3transfer [required: >=0.1.10,<0.2.0, installed: 0.1.13]

        – botocore [required: >=1.3.0,<2.0.0, installed: 1.10.80]

          – docutils [required: >=0.10, installed: 0.14]

          – jmespath [required: >=0.7.1,<1.0.0, installed: 0.9.3]

          – python-dateutil [required: >=2.1,<3.0.0, installed: 2.7.3]

            – six [required: >=1.5, installed: 1.11.0]

    – bz2file [required: Any, installed: 0.98]

    – requests [required: Any, installed: 2.19.1]

      – certifi [required: >=2017.4.17, installed: 2018.8.13]

      – chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]

      – idna [required: >=2.5,<2.8, installed: 2.7]

      – urllib3 [required: >=1.21.1,<1.24, installed: 1.23]

jieba==0.39

openpyxl==2.5.5

  – et-xmlfile [required: Any, installed: 1.0.1]

  – jdcal [required: Any, installed: 1.4]

patchwork==1.0.1

  – fabric [required: >=2.0,<3.0, installed: 2.3.1]

    – cryptography [required: >=1.1, installed: 2.3.1]

      – asn1crypto [required: >=0.21.0, installed: 0.24.0]

      – cffi [required: >=1.7,!=1.11.3, installed: 1.11.5]

        – pycparser [required: Any, installed: 2.18]

      – idna [required: >=2.1, installed: 2.7]

      – six [required: >=1.4.1, installed: 1.11.0]

    – invoke [required: >=1.1,<2.0, installed: 1.1.1]

    – paramiko [required: >=2.4, installed: 2.4.1]

      – bcrypt [required: >=3.1.3, installed: 3.1.4]

        – cffi [required: >=1.1, installed: 1.11.5]

          – pycparser [required: Any, installed: 2.18]

        – six [required: >=1.4.1, installed: 1.11.0]

      – cryptography [required: >=1.5, installed: 2.3.1]

        – asn1crypto [required: >=0.21.0, installed: 0.24.0]

        – cffi [required: >=1.7,!=1.11.3, installed: 1.11.5]

          – pycparser [required: Any, installed: 2.18]

        – idna [required: >=2.1, installed: 2.7]

        – six [required: >=1.4.1, installed: 1.11.0]

      – pyasn1 [required: >=0.1.7, installed: 0.4.4]

      – pynacl [required: >=1.0.1, installed: 1.2.1]

        – cffi [required: >=1.4.1, installed: 1.11.5]

          – pycparser [required: Any, installed: 2.18]

        – six [required: Any, installed: 1.11.0]

pycnnum==1.0.1

pylru==1.1.0

pymongo==3.7.1

PyMySQL==0.9.2

  – cryptography [required: Any, installed: 2.3.1]

    – asn1crypto [required: >=0.21.0, installed: 0.24.0]

    – cffi [required: >=1.7,!=1.11.3, installed: 1.11.5]

      – pycparser [required: Any, installed: 2.18]

    – idna [required: >=2.1, installed: 2.7]

    – six [required: >=1.4.1, installed: 1.11.0]

schedule==0.5.0

SolrClient==0.2.1

  – kazoo [required: ==2.2.1, installed: 2.2.1]

    – six [required: Any, installed: 1.11.0]

  – requests [required: >=2.2.1, installed: 2.19.1]

    – certifi [required: >=2017.4.17, installed: 2018.8.13]

    – chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]

    – idna [required: >=2.5,<2.8, installed: 2.7]

    – urllib3 [required: >=1.21.1,<1.24, installed: 1.23]

torndb==0.3

直接去试试安装MySQLdb,不行:

➜  xx git:(master) ✗ pipenv install MySQLdb

Installing MySQLdb…

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple

Collecting MySQLdb

Error:  An error occurred while installing MySQLdb!

  Could not find a version that satisfies the requirement MySQLdb (from versions: )

No matching distribution found for MySQLdb

You are using pip version 10.0.1, however version 18.0 is available.

You should consider upgrading via the ‘pip install –upgrade pip’ command.

网上搜了:

torndb ModuleNotFoundError: No module named ‘MySQLdb’

torndb安装小记

去安装mysql-python:

➜  xx git:(master) ✗ pipenv install mysql-python

Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead.

Installing mysql-python…

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple

Collecting mysql-python

  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108kB)

    Complete output from command python setup.py egg_info:

    Traceback (most recent call last):

      File "<string>", line 1, in <module>

      File "/private/var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/pip-install-52c2wkph/mysql-python/setup.py", line 13, in <module>

        from setup_posix import get_config

      File "/private/var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/pip-install-52c2wkph/mysql-python/setup_posix.py", line 2, in <module>

        from ConfigParser import SafeConfigParser

    ModuleNotFoundError: No module named ‘ConfigParser’

    

    —————————————-

Error:  An error occurred while installing mysql-python!

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/pip-install-52c2wkph/mysql-python/

You are using pip version 10.0.1, however version 18.0 is available.

You should consider upgrading via the ‘pip install –upgrade pip’ command.

This is likely caused by a bug in mysql-python. Report this to its maintainers.

对于ConfigParser到处出错,感觉显示Python 2和3的问题

注:我此处的环境是:

Mac本地,pipenv的虚拟环境,Python 3.6

➜  x git:(master) ✗ which python

/Users/crifan/.local/share/virtualenvs/x-gXiJ4vtz/bin/python

➜  x git:(master) ✗ python –version

Python 3.6.4

python – Getting "Error loading MySQLdb module: No module named MySQLdb" – have tried previously posted solutions – Stack Overflow

已经装了MySQL-python,但在引用时报错:ImportError: No module named MySQLdb-一直在路上-51CTO博客

flask中连接MySQL出现ModuleNotFoundError: No module named ‘MySQLdb’错误 – CSDN博客

所以再去搜:

mysql-python ModuleNotFoundError: No module named ‘ConfigParser’

Python 安装 MySQL-python ImportError: No module named ‘ConfigParser’ – CSDN博客

Python3 安装 mysql-python 与ImportError: No module named ‘ConfigParser’报错解决方法 – CSDN博客

MySQL-python installation not working within virtualenv – Stack Overflow

virtualenv

pip-3.3 install MySQL-python – Stack Overflow

Python3安装mysql-python包报错ModuleNotFoundError: No module named ‘ConfigParser’之解决方案 – 伟哥的技术博客

【总结】

python3不再支持mysqldb

所以:

Python3中,用MySQL的话,避免

o module named ‘ConfigParser’

办法:

(1)临时

改名ConfigParser.py为configparser.py

(2)终极

换用pymysql

然后再:

import pymysql

此处还用用终极的办法,换用pymysql

然后代码就可以正常运行了:

转载请注明:在路上 » 【已解决】python调用torndb出错:import MySQLdb.constants ModuleNotFoundError: No module named MySQLdb

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.186 seconds, using 22.14MB memory