ModuleNotFoundError: No module named ‘pymysql.util‘

ModuleNotFoundError: No module named ‘pymysql.util’

先说现象,有一个项目,在自己的本地是没问题,到了线上就报错了

deploy@autotest01-test-ty-qq-bj:/czb/interface_server/hua$ cat log.log
Traceback (most recent call last):
  File "/czb/interface_server/hua/hua_start.py", line 19, in <module>
    from routers import database_router, env_router, channel_router, case_group_router, case_router
  File "/czb/interface_server/hua/routers/__init__.py", line 10, in <module>
    from routers.database_router import database_router
  File "/czb/interface_server/hua/routers/database_router.py", line 17, in <module>
    from db_moudle.database_moudle.database import get_database, get_database_s, delete_database_yes, add_database, \
  File "/czb/interface_server/hua/db_moudle/database_moudle/database.py", line 39, in <module>
    database_cls = DatabaseManage().database_cls()
  File "/czb/interface_server/hua/db_moudle/database_moudle/database.py", line 25, in __init__
    self.database = Database(DATABASE_URL)
  File "/home/deploy/.local/lib/python3.8/site-packages/databases/core.py", line 66, in __init__
    backend_cls = import_from_string(backend_str)
  File "/home/deploy/.local/lib/python3.8/site-packages/databases/importer.py", line 21, in import_from_string
    raise exc from None
  File "/home/deploy/.local/lib/python3.8/site-packages/databases/importer.py", line 18, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/deploy/.local/lib/python3.8/site-packages/databases/backends/mysql.py", line 6, in <module>
    import aiomysql
  File "/home/deploy/.local/lib/python3.8/site-packages/aiomysql/__init__.py", line 32, in <module>
    from .connection import Connection, connect
  File "/home/deploy/.local/lib/python3.8/site-packages/aiomysql/connection.py", line 19, in <module>
    from pymysql.util import byte2int, int2byte
ModuleNotFoundError: No module named 'pymysql.util'

很费解,网上也没找到答案,只能不断尝试,最后解决了,流程是这样的

一、找到最后的报错信息

  File "/home/deploy/.local/lib/python3.8/site-packages/aiomysql/connection.py", line 19, in <module>
    from pymysql.util import byte2int, int2byte
ModuleNotFoundError: No module named 'pymysql.util'

意思就是在引入pymysql.util的时候报错了,说没有这个模块,那这个pymysql.util的绝对路径就是
/home/deploy/.local/lib/python3.8/site-packages/aiomysql/connection.py
去掉aiomysql/connection.py
加上pymysql.util
再去掉util,因为util是一个文件,并不是一个目录
完整的路径就是
/home/deploy/.local/lib/python3.8/site-packages/pymysql

二、确认报错信息是否准确

deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages/pymysql$ pwd
/home/deploy/.local/lib/python3.8/site-packages/pymysql
deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages/pymysql$ ll
total 156
drwxrwxr-x   4 deploy deploy  4096 Feb 14 15:51 ./
drwx------ 202 deploy deploy 12288 Feb 14 15:51 ../
-rw-rw-r--   1 deploy deploy  7399 Feb 14 15:51 _auth.py
-rw-rw-r--   1 deploy deploy 10293 Feb 14 15:51 charset.py
-rw-rw-r--   1 deploy deploy 51251 Feb 14 15:51 connections.py
drwxrwxr-x   3 deploy deploy  4096 Feb 14 15:51 constants/
-rw-rw-r--   1 deploy deploy  9430 Feb 14 15:51 converters.py
-rw-rw-r--   1 deploy deploy 15366 Feb 14 15:51 cursors.py
-rw-rw-r--   1 deploy deploy  3773 Feb 14 15:51 err.py
-rw-rw-r--   1 deploy deploy  4391 Feb 14 15:51 __init__.py
-rw-rw-r--   1 deploy deploy   573 Feb 14 15:51 optionfile.py
-rw-rw-r--   1 deploy deploy 11859 Feb 14 15:51 protocol.py
drwxrwxr-x   2 deploy deploy  4096 Feb 14 15:51 __pycache__/
-rw-rw-r--   1 deploy deploy   360 Feb 14 15:51 times.py

发现,嗯,确实没有util文件,那这个时候我第一个想到的是重新安装

deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages/pymysql$ pip3 install PyMySQL
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: PyMySQL in /home/deploy/.local/lib/python3.8/site-packages (1.0.2)
WARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages/pymysql$ ls
_auth.py    connections.py  converters.py  err.py       optionfile.py  __pycache__
charset.py  constants       cursors.py     __init__.py  protocol.py    times.py

很奇怪,还是没有,这个时候想起会不会是版本的问题,找到了项目的requeirements.txt文件

loguru~=0.5.3
PyYAML~=6.0
SQLAlchemy~=1.4.31
databases~=0.5.5
PyMySQL==0.9.3
pydantic~=1.9.0
fastapi~=0.72.0
uvicorn~=0.17.0
ujson~=5.1.0

既然项目在本地是可以,说明本地的版本是对的,那就直接安装对应的版本即可

deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages/pymysql$ pip3 install PyMySQL==0.9.3
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting PyMySQL==0.9.3
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47 kB)
Installing collected packages: PyMySQL
  Attempting uninstall: PyMySQL
    Found existing installation: PyMySQL 1.0.2
    Uninstalling PyMySQL-1.0.2:
      Successfully uninstalled PyMySQL-1.0.2
Successfully installed PyMySQL-0.9.3
WARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.

重新进入pymysql的目录(重新进入,不要在原来的目录ls,因为对应的包已经被卸载重新安装了),发现这个文件已经有了

deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages$ cd pymysql/
deploy@autotest01-test-ty-qq-bj:~/.local/lib/python3.8/site-packages/pymysql$ ls
_auth.py    _compat.py      constants      cursors.py  __init__.py    protocol.py  _socketio.py  util.py
charset.py  connections.py  converters.py  err.py      optionfile.py  __pycache__  times.py

再次重新启动项目,success,完美

在使用Jupyter时,如果出现"ModuleNotFoundError: No module named 'pymysql'"的错误提示,可以尝试以下解决方法: 1. 确认已经安装了pymysql模块。可以使用命令`!pip install pymysql`来安装pymysql模块,确保模块已经成功安装。 2. 如果安装了pymysql模块,但仍然出现错误提示,可能是因为Jupyter无法正确导入pymysql模块。此时可以尝试通过添加以下代码来解决问题: ``` import pymysql pymysql.install_as_MySQLdb() ``` 这段代码将pymysql作为MySQLdb使用,可以解决ModuleNotFoundError错误。 3. 如果以上方法仍然不能解决问题,还可以尝试检查Jupyter的环境配置。确保Jupyter能够正确找到pymysql模块的路径,并能够导入该模块。 通过以上方法,您应该能够解决"ModuleNotFoundError: No module named 'pymysql'"的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python3.8报错ModuleNotFoundError: No module named ‘MySQLdb‘及解决方法](https://blog.csdn.net/pick_ears/article/details/122173068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ModuleNotFoundError: No module named ‘mysql‘解决方法](https://blog.csdn.net/weixin_73778478/article/details/127822215)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值