centos安装Python插件后找不到对应的插件

题记

在之前的文章中,我在centos服务器装了python3并且给python与pip建立了软链,利用git上传代码到centos服务器之后利用终端运行app.py的时候会报错。

报错:

[root@izm5e0qcppq4b6rgy4d5vfz flaskServer]# python app.py
Traceback (most recent call last):
  File "app.py", line 2, in <module>
    from flask import Flask
  File "/usr/local/python3/lib/python3.7/site-packages/flask/__init__.py", line 21, in <module>
    from .app import Flask, Request, Response
  File "/usr/local/python3/lib/python3.7/site-packages/flask/app.py", line 25, in <module>
    from . import cli, json
  File "/usr/local/python3/lib/python3.7/site-packages/flask/cli.py", line 18, in <module>
    import ssl
  File "/usr/local/python3/lib/python3.7/ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
[root@izm5e0qcppq4b6rgy4d5vfz flaskServer]# python3 app.py
Traceback (most recent call last):
  File "app.py", line 2, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'

问题还原:

环境:

1. 自定义安装了python3的位置,并且建立了软链,
python版本

项目中引入flask的时候提示报错,并且运行中执行不同的python命令提示报错还不一样:
报错

安装flask环境以后还是不行,

猜想应该是装python3 并且建立软链的过程中,哪个地方弄错了。一时不知道该怎么办。想起来 flask文档 有这么一句段:

文档

于是想到用 virtualenv 建立一个虚拟环境,安装flask,再运行
代码:

[root]# sudo pip install virtualenv
[root]# virtualenv venv
New python executable in /var/www/flaskServer/venv/bin/python2
Also creating executable in /var/www/flaskServer/venv/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...done.
[rootr]# chmod 755 venv/bin/activate
[rootr]# venv/bin/activate
[rootr]# pip3 install Flask
...
[rootr]# pip3 install requests
...
[rootr]# python3 app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

重启服务器之后,利用python3执行命令,照样可以运行,不知道为什么但是就是这么解决了。。。

猜想

应该是和系统自带的python2.7有关,而我给项目创建了一个虚拟环境,应该我是隔绝了python3对python2.7模块的引用,之后python3找不到对应的模块,重新安装,pip3重新创建新的路径,python3能够找到对应的模块。能够运行。

本站所有文章除特殊声明外均为原创,未经允许禁止转载!
换个声音
停止播放