Flask开发(二)部署到ubuntu16.04


   

1 安装python3.7

  首先删除pip3,因为pip3默认是跟随3.5的版本。

sudo apt remove python3-pip

  Ubuntu 16.04中python3默认为3.5,需要安装与开发环境一致的3.7。

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7

  然后使系统中默认python3为python3.7,修改软链接

sudo ln -sf /usr/bin/python3.7 /usr/bin/python3

   安装pip3

sudo apt-get install python3-pip

  pip3 --version
  pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.7)  得到3.7对应的pip

2 安装flask

sudo python3 -m pip install flask -i https://pypi.tuna.tsinghua.edu.cn/simple

  此时可能会报错

File "/usr/local/lib/python3.7/dist-packages/flask/__init__.py", line 1, in 
    from markupsafe import escape
ModuleNotFoundError: No module named 'markupsafe'

  需要再安装如下包,参考[bug] conan doesn't work on python 3.5 anymore · Issue #9019 · conan-io/conan · GitHub,虽然是针对3.5,但是修复3.7成功。

sudo python3 -m pip install conan MarkupSafe==1.1.1  -i https://pypi.tuna.tsinghua.edu.cn/simple

3 运行

python3 server.py

备注:

  ① 如果直接在Ubuntu 16.04中python3的默认环境python3.5上部署,遇到from markupsafe import escape ImportError: cannot import name 'escape'问题,按照[bug] conan doesn't work on python 3.5 anymore · Issue #9019 · conan-io/conan · GitHub的方法没有修复成功;

  ② 如果安装python3.8后部署,会遇到Error: module 'platform' has no attribute 'linux_distribution'的问题,根据ubuntu - Error: module 'platform' has no attribute 'linux_distribution' - Stack Overflow的如下解释,后来尝试使用3.7安装成功

Python 3.8 removed the "linux_distribution()" function from the platform module. It is replaced by the equivalent in the "distro" module 
(distro is not built-in to Python and must be installed via pip). If you're unable to change the source code, you can try downgrading your Python to Python 3.7

   ③ 一些可能报错及解决:

  ImportError: cannot import name 'sysconfig' from 'distutils'     -->    sudo apt-get install python3.x-distutils

  ModuleNotFoundError: No module named 'distutils.util'     -->    sudo apt-get install python3.x-distutils

  ④ 一种可能的安装pip的方式:

sudo apt remove python3-pip
sudo python3.8 -m easy_install pip

参考链接:

Installing Python 3.8.3 on Ubuntu 16.04, change the default version of python to the new version, and why is my terminal not working. | by RL | Analytics Vidhya | Medium

(28条消息) Ubuntu 16.04下更换系统默认python版本_shawnL1的博客-CSDN博客

[bug] conan doesn't work on python 3.5 anymore · Issue #9019 · conan-io/conan · GitHub