记录一次python多版本和软连接指向不对导致的尴尬报错:import pymysql ModuleNotFoundError: No module named ‘pymysql‘

前提条件

之前为了测试一键安装脚本,安装了python3.7,却忘记了服务器上已经有了pythton3.6,然后正常添加了软连接到/usr/bin目录,其实测试并没有替换成功,因为我没有先删除软连接。

然后后面执行python send.py文件时,一直报错,百思不得其解:

Traceback (most recent call last):
  File "src/send.py", line 6, in <module>
    from src.fetchData import get_day_list, get_today_hours
  File "/project/python/email/src/fetchData.py", line 1, in <module>
    import pymysql
ModuleNotFoundError: No module named 'pymysql'

出现这个问题,肯定是pip默认安装错了为止,但是每次pip list检查的时候,都会发现PyMYSQL存在列表中

然后执行whereis命令发现竟然有两个版本的python,随后想到是不是pip安装包指向为止错了

[root@AlexWong email]# whereis python
python: /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python /usr/lib/python3.6 /usr/lib64/python3.6 /usr/local/lib/python3.6 /usr/include/python3.6m /opt/python/bin/python3.7m /opt/python/bin/python3.7m-config /opt/python/bin/python3.7-config /opt/python/bin/python3.7 /usr/share/man/man1/python.1.gz
[root@AlexWong email]# whereis pip
pip: /usr/bin/pip /usr/lib/python3.6/bin/pip3.6

# 查看新安装版本下的pip3安装的包,发现不存在
[root@AlexWong email]# /opt/python/bin/pip3 list
Package    Version
---------- -------
pip        10.0.1 
setuptools 39.0.1 

# 查看软连接
[root@AlexWong email]# ll /usr/bin/
lrwxrwxrwx  1 root root           10 Feb  5 19:00  pip -> //bin/pip3
lrwxrwxrwx. 1 root root           23 Dec 12 19:54  pip-3 -> /etc/alternatives/pip-3
lrwxrwxrwx. 1 root root           22 Dec 12 19:54  pip3 -> /etc/alternatives/pip3
lrwxrwxrwx  1 root root            8 Aug 25 03:31  pip-3.6 -> ./pip3.6
-rwxr-xr-x  1 root root          209 Aug 25 03:31  pip3.6
lrwxrwxrwx  1 root root           23 Feb  5 18:51  python -> /opt/python/bin/python3
lrwxrwxrwx. 1 root root           25 Dec 12 19:54  python3 -> /etc/alternatives/python3
lrwxrwxrwx  1 root root           31 Nov  4 12:04  python3.6 -> /usr/libexec/platform-python3.6
lrwxrwxrwx  1 root root           32 Nov  4 12:04  python3.6m -> /usr/libexec/platform-python3.6m
lrwxrwxrwx. 1 root root           26 Dec 12 19:54  pyvenv-3 -> /etc/alternatives/pyvenv-3
-rwxr-xr-x  1 root root          446 Aug 25 01:59  pyvenv-3.6

pip -> //bin/pip3 是错误的,应该是/opt/python/bin/pip3
说明脚本中自动执行软链接没有替换成功;

为了保险起见,我需要清理下服务器的环境,只保留一个python和pip版本:

删除3.6的所有软连接

[root@AlexWong email]# rm -rf /usr/bin/python3.6m /usr/bin/python3.6 /usr/lib/python3.6
[root@AlexWong email]# rm -rf /usr/lib64/python3.6 /usr/local/lib/python3.6 /usr/include/python3.6m

新增软链接
记得一定要先删除老的软连接

# python
[root@AlexWong email]# rm -rf /usr/bin/python
[root@AlexWong email]# ln -s /opt/python/bin/python3 /usr/bin/python

# pip
[root@AlexWong email]# rm -rf /usr/bin/pip
[root@AlexWong email]# ln -s /opt/python/bin/pip3 /usr/bin/pip

测试

[root@AlexWong email]# whereis python
python: /usr/bin/python /opt/python/bin/python3.7m /opt/python/bin/python3.7m-config /opt/python/bin/python3.7-config /opt/python/bin/python3.7 /usr/share/man/man1/python.1.gz
[root@AlexWong email]# whereis pip
pip: /usr/bin/pip /opt/python/bin/pip3.7 /opt/python/bin/pip

再次执行python也没有报错了

上一篇:数据库之存储过程


下一篇:数据可视化