美文网首页
Python——Win10用Python3写Flask

Python——Win10用Python3写Flask

作者: 战斗力五只鹅 | 来源:发表于2020-04-23 16:05 被阅读0次

    按照Flask官网安装教程来安装Flask。

    1. 安装venv,venv是一个python的虚拟环境,可以隔离工程环境和全局环境。
    cd yourProjectPath
    py3 -m venv yourVenvFolderName
    
    1. 启动venv
    yourVenvFolderName\Scripts\activate
    

    如果你的Scripts\下没有activate文件,参照StackOverflow: venv doesn't create activate scrpt python3,你需要换一种不带pip的方式创建venv

    py3 -m venv --without-pip yourVenvFolderName
    
    1. 安装Flask
      用pip安装Flask,你的虚拟环境里可能由于某种原因没有pip,例如采用上面--without-pip的方式安装,会报错。

    (venv) F:\xxx>pip install Flasksk
    Fatal error in launcher: Unable to create process using '"xxx:\python\python3.8.2\python.exe"
    "xxx\Python3.8.2\Scripts\pip.exe" install Flask': ???????????

    那么需要先在虚拟环境里再安装一个pip。
    注意:如果你安装了多个版本的python,并且为了方便把python.exe重命名了,例如本文之前的命令都是用我自己重命名的py3代替python,那么这里执行安装pip的命令前你需要暂时重命名回原本的“python.exe”。

    否则会报错,在安装pip时依赖的wheel找不到:
    (ps:这里应该有更好的办法可以直接用重命名后的py3,但是暂时先这样解决)

    -- 错误命令:
    curl https://bootstrap.pypa.io/get-pip.py | py3
    

    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 1764k 100 1764k 0 0 17378 0 0:01:44 0:01:44 --:--:-- 40227
    Collecting pip
    Using cached pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
    ERROR: Could not find a version that satisfies the requirement wheel (from versions:
    none)
    ERROR: No matching distribution found for wheel

    -- 正确命令:
    curl https://bootstrap.pypa.io/get-pip.py | python
    

    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 1764k 100 1764k 0 0 5034 0 0:05:59 0:05:59 --:--:-- 13847
    Collecting pip
    Using cached pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
    Collecting wheel
    Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
    Installing collected packages: pip, wheel
    Attempting uninstall: pip
    Found existing installation: pip 20.0.2
    Uninstalling pip-20.0.2:
    Successfully uninstalled pip-20.0.2
    Successfully installed pip-20.0.2 wheel-0.34.2

    验证pip是否安装成功

    (venv) F:\xxx>pip --version
    pip 20.0.2 from d:\python\python3.8.2\lib\site-packages\pip (python 3.8)

    成功安装pip,然后安装Flask

    pip install Flask
    

    安装Flask的过程中会自动安装几个依赖Werkzeug、Jinja2、itsdangerous、click、MarkupSafe。
    安装依赖的过程中可能由于网络原因而失败,例如:

    Collecting Werkzeug>=0.15
    WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after
    connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")':
    ...
    Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
    ERROR: Exception:
    Traceback (most recent call last):
    ...
    socket.timeout: The read operation timed out

    这时可以换个国内的镜像。
    或者直接科学上网,几秒钟就可以安装完毕了。

    Collecting Flask
    Using cached Flask-1.1.2-py2.py3-none-any.whl (94 kB)
    Collecting Werkzeug>=0.15
    Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
    |████████████████████████████████| 298 kB 198 kB/s
    Collecting Jinja2>=2.10.1
    Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
    |████████████████████████████████| 125 kB 652 kB/s
    Collecting itsdangerous>=0.24
    Using cached itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
    Collecting click>=5.1
    Downloading click-7.1.1-py2.py3-none-any.whl (82 kB)
    |████████████████████████████████| 82 kB 260 kB/s
    Collecting MarkupSafe>=0.23
    Downloading MarkupSafe-1.1.1-cp38-cp38-win32.whl (16 kB)
    Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, click, Flask
    Successfully installed Flask-1.1.2 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.1 itsdangerous-1.1.0

    相关文章

      网友评论

          本文标题:Python——Win10用Python3写Flask

          本文链接:https://www.haomeiwen.com/subject/juqaihtx.html