配置Flask开发环境
开发环境:Python3.11+pycharm
一、升级pip至最新版本
在终端中,使用
python.exe -m pip install --upgrade pip
,升级pip至最新版本
PS C:\Users\bytetoy> python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\users\bytetoy\appdata\local\packages\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\localcache\local-packages\python311\site-packages (23.3.1)
Collecting pip
Using cached pip-24.2-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.2-py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 1.3 MB/s eta 0:00:00
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 23.3.1
Uninstalling pip-23.3.1:
Successfully uninstalled pip-23.3.1
WARNING: The scripts pip.exe, pip3.11.exe and pip3.exe are installed in 'C:\Users\bytetoy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-24.2
二、安装pipenv
使用
pip install pipenv
安装pipenv
PS C:\Users\bytetoy> pip install pipenv
Collecting pipenv
Using cached pipenv-2024.0.1-py3-none-any.whl.metadata (19 kB)
Collecting certifi (from pipenv)
Using cached certifi-2024.7.4-py3-none-any.whl.metadata (2.2 kB)
Collecting setuptools>=67 (from pipenv)
Downloading setuptools-72.1.0-py3-none-any.whl.metadata (6.6 kB)
Collecting virtualenv>=20.24.2 (from pipenv)
Downloading virtualenv-20.26.3-py3-none-any.whl.metadata (4.5 kB)
Collecting distlib<1,>=0.3.7 (from virtualenv>=20.24.2->pipenv)
Downloading distlib-0.3.8-py2.py3-none-any.whl.metadata (5.1 kB)
Collecting filelock<4,>=3.12.2 (from virtualenv>=20.24.2->pipenv)
Downloading filelock-3.15.4-py3-none-any.whl.metadata (2.9 kB)
Collecting platformdirs<5,>=3.9.1 (from virtualenv>=20.24.2->pipenv)
Downloading platformdirs-4.2.2-py3-none-any.whl.metadata (11 kB)
Downloading pipenv-2024.0.1-py3-none-any.whl (3.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2/3.2 MB 54.1 kB/s eta 0:00:00
Downloading setuptools-72.1.0-py3-none-any.whl (2.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.3/2.3 MB 43.3 kB/s eta 0:00:00
Downloading virtualenv-20.26.3-py3-none-any.whl (5.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.7/5.7 MB 22.1 kB/s eta 0:00:00
Downloading certifi-2024.7.4-py3-none-any.whl (162 kB)
Downloading distlib-0.3.8-py2.py3-none-any.whl (468 kB)
Downloading filelock-3.15.4-py3-none-any.whl (16 kB)
Downloading platformdirs-4.2.2-py3-none-any.whl (18 kB)
Installing collected packages: distlib, setuptools, platformdirs, filelock, certifi, virtualenv, pipenv
WARNING: The script virtualenv.exe is installed in 'C:\Users\bytetoy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts pipenv-resolver.exe and pipenv.exe are installed in 'C:\Users\bytetoy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed certifi-2024.7.4 distlib-0.3.8 filelock-3.15.4 pipenv-2024.0.1 platformdirs-4.2.2 setuptools-72.1.0 virtualenv-20.26.3
三、配置环境变量
根据上面步骤的WARNING提示,将
C:\Users\bytetoy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts
路径添加至系统用户变量,否则pycharm创建项目时,找不到pipenv虚拟环境管理工具
四、安装flask
- 在pycharm中创建项目,选择pipenv作为虚拟环境管理工具
- 在pycharm的Terminal中启用虚拟环境,使用
pipenv shell
pipenv shell
Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for
any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY
Launching subshell in virtual environment...
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
- 使用
pipenv install flask
安装Flask模块pipenv install flask Installing flask... Resolving flask... Added flask to Pipfile's [packages] ... Installation Succeeded Pipfile.lock not found, creating... Locking [packages] dependencies... Building requirements... Resolving dependencies... Success! Locking [dev-packages] dependencies... Updated Pipfile.lock (4444e3889bcf386ac1ef6bcf8c824ac77e3b32298e8e766e2ec3ed38aa4acaff)! Installing dependencies from Pipfile.lock (4acaff)...
- 安装成功后,打开pycharm的settings->project->python interpreter,可以看到flask及相关依赖已经安装成功。
五、启动服务器
- 在虚拟环境中pipenv shell使用
flask run
启动服务器,即可通过浏览器访问http://127.0.0.1:5000页面
flask run
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [10/Aug/2024 22:44:09] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [10/Aug/2024 22:44:09] "GET /favicon.ico HTTP/1.1" 404 -
- 当然也可以在Pycharm中配置启动选项
六、代码
在项目根目录下创建一段python代码,命名为app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello,Bytetoy</h1>'