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,完美