centos安裝Python外掛後找不到對應的外掛ModuleNotFoundError: No module named*

weixin_34402408發表於2018-10-10

題記

在之前的文章中,我在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的位置,並且建立了軟鏈,


1091540-5874475aac4963a4
python版本

專案中引入flask的時候提示報錯,並且執行中執行不同的python命令提示報錯還不一樣:


1091540-af23988e972133b0
報錯

安裝flask環境以後還是不行,

猜想應該是裝python3 並且建立軟鏈的過程中,哪個地方弄錯了。一時不知道該怎麼辦。想起來 flask文件 有這麼一句段:

1091540-14c2393fac93d441
文件

於是想到用 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能夠找到對應的模組。能夠執行。

1091540-7bbd00161aff8a0d
HELLO WORLD

親情連結:
簡書
部落格園

相關文章