记录在Ubuntu上的一次模块缺失的摸排检查工作
#python
ftp123@ubuntutest-virtual-machine:~/Recv/123$ cd ~ ftp123@ubuntutest-virtual-machine:~$ ls bin Data examples.desktop FtpSend.py Recv Snd src XY_send_file.sh ftp123@ubuntutest-virtual-machine:~$ cd bin/ ftp123@ubuntutest-virtual-machine:~/bin$ ls clear_file.sh ftp123@ubuntutest-virtual-machine:~/bin$ cd $xy_src ftp123@ubuntutest-virtual-machine:~/src$ ls fileRedis.py FtpSend.py GetRedis.py ftp123@ubuntutest-virtual-machine:~/src$ ll total 28 drwxrwxr-x 2 ftp123 ftp123 4096 12月 22 09:51 ./ drwxr-xr-x 10 ftp123 ftp123 4096 12月 24 09:07 ../ -rw-rw-r-- 1 ftp123 ftp123 2130 12月 22 09:39 fileRedis.py -rw-rw-r-- 1 ftp123 ftp123 5404 12月 22 09:39 FtpSend.py -rw-rw-r-- 1 ftp123 ftp123 6277 12月 22 09:39 GetRedis.py # --------------------------------------------------------------------- # 这里开始作为使用python并且发现没有找到redis模块的问题的开始 ftp123@ubuntutest-virtual-machine:~/src$ python fileRedis.py File "fileRedis.py", line 7 SyntaxError: Non-ASCII character '\xe7' in file fileRedis.py on line 8, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details ftp123@ubuntutest-virtual-machine:~/src$ ^C ftp123@ubuntutest-virtual-machine:~/src$ python fileRedis.py Traceback (most recent call last): File "fileRedis.py", line 3, in <module> import GetRedis File "/home/ftp123/src/GetRedis.py", line 4, in <module> import redis ImportError: No module named redis # 问题现象描述结束 # --------------------------------------------------------------------- # --------------------------------------------------------------------- # 第一次尝试使用pip安装redis ftp123@ubuntutest-virtual-machine:~/src$ pip install redis Command 'pip' not found, but can be installed with: apt install python-pip Please ask your administrator. # 显示系统并没有安装pip命令,所以转而进行pip的安装 ftp123@ubuntutest-virtual-machine:~/src$ apt install python-pip # 以下为系统安装 pip 过程 E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root? ftp123@ubuntutest-virtual-machine:~/src$ su - Password: root@ubuntutest-virtual-machine:~# apt install python-pip Reading package lists... Done Building dependency tree Reading state information... Done ... # pip 安装完毕,开始使用pip安装redis root@ubuntutest-virtual-machine:~# pi pi1toppm pico pinentry pinentry-x11 ping6 pivot_root pi3topbm piconv pinentry-curses ping pinky pic pidof pinentry-gnome3 ping4 pip3 # 这里其实已经有隐形提示,在一般情况下pip命令是只有pip的。 # 当出现 pip3 的情况下,就说明了此时系统上装有Python2 和 Python3 两个版本。 # 使用pip3 安装redis root@ubuntutest-virtual-machine:~# pip3 install redis Collecting redis Downloading https://files.pythonhosted.org/packages/b1/9c/838dbabd16f7dad05d3b83abad11560a2c6cc72fe913a02fa487fc915b9d/redis-4.0.2-py3-none-any.whl (119kB) 25% |████████▏ | 30kB 1.5kB/s eta 0:01:01Exception: ... error_catcher raise ReadTimeoutError(self._pool, None, 'Read timed out.') urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. # 因为网络问题第一次安装失败。 # 这里开始确认系统上有没有redis环境 root@ubuntutest-virtual-machine:~# find / -name redis* /var/log/redis /var/log/redis/redis-server.log.4.gz /var/log/redis/redis-server.log ... ^C # 这里通过查找确认了本地有redis 库文件。所以退出再次重试 root@ubuntutest-virtual-machine:~# exit logout ftp123@ubuntutest-virtual-machine:~/src$ python fileRedis.py Traceback (most recent call last): File "fileRedis.py", line 3, in <module> import GetRedis File "/home/ftp123/src/GetRedis.py", line 4, in <module> import redis ImportError: No module named redis # 依旧显示库缺失,和可以使用的用户确认本地环境变量的差异性。无误 # 选择再次安装Python的 redis库。 ftp123@ubuntutest-virtual-machine:~/src$ vi ~/.profile ftp123@ubuntutest-virtual-machine:~/src$ su - Password: # 使用root用户 第二次安装 root@ubuntutest-virtual-machine:~# pip3 install redis Collecting redis Downloading ... Successfully installed deprecated-1.2.13 redis-4.0.2 wrapt-1.13.3 # 这里安装完毕,不再急于启动程序,首先通过python 尝试调用库。 root@ubuntutest-virtual-machine:~# python Python 2.7.17 (default, Feb 27 2021, 15:10:58) # 其实这里就已经有答案了,我通过find查找到的redis库是通过pip3安装的,路径在Python3.6下 # 但是这里的版本是2.7 。存在多个版本的时候特别是2和3都有的情况下,一定要使用Python2或Python3来指定版本。 [GCC 7.5.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import redis Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named redis >>> q Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'q' is not defined >>> KeyboardInterrupt >>> KeyboardInterrupt >>> import redis Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named redis >>> [1]+ Stopped python # 想要看看在这个安装路径下调用可不可以,因为python的特性是先在当前目录下寻找。 root@ubuntutest-virtual-machine:~# cd anaconda3/lib/python3.7/site-packages/redis-3.5.3.dist-info/METADATA -su: cd: anaconda3/lib/python3.7/site-packages/redis-3.5.3.dist-info/METADATA: No such file or directory # 成功使用的正确方式 # 前提安装完成redis ftp123@ubuntutest-virtual-machine:~/src$ python3 fileRedis.py {'type': 'message', 'pattern': None, 'channel': b'second_product', 'data': b'20210606000000'} send 20210606000000.bz2 end
再附赠解决国内链接GitHub网络不稳的解决方式:
怎么解决Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection
遇到该问题的表象: 原因是连接超时,所以需要自己设定安装源,
root@ubuntutest-virtual-machine:~# pip install requests -i http://pypi.douban.com/simple --trusted-host pypi.douban.com(通过豆瓣) root@ubuntutest-virtual-machine:~# pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple(使用清华镜像)
成功表现: