pycharm 开发flask 环境搭建
首先安装pyenv
macname@MacdeMacBook-Pro Desktop % mkdir fish macname@MacdeMacBook-Pro Desktop % cd fish macname@MacdeMacBook-Pro fish % pip3 install pipenv Collecting pipenv Downloading https://files.pythonhosted.org/packages/13/b4/3ffa55f77161cff9a5220f162670f7c5eb00df52e00939e203f601b0f579/pipenv-2018.11.26-py3-none-any.whl (5.2MB) |████████████████████████████████| 5.2MB 10kB/s Requirement already satisfied: setuptools>=36.2.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pipenv) (40.8.0) Collecting virtualenv-clone>=0.2.5 Downloading https://files.pythonhosted.org/packages/83/b8/cd931487d250565392c39409117436d910232c8a3ac09ea2fb62a6c47bff/virtualenv_clone-0.5.4-py2.py3-none-any.whl Requirement already satisfied: pip>=9.0.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pipenv) (19.3.1) Requirement already satisfied: virtualenv in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pipenv) (16.7.4) Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pipenv) (2019.11.28) Installing collected packages: virtualenv-clone, pipenv Successfully installed pipenv-2018.11.26 virtualenv-clone-0.5.4 WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command. macname@MacdeMacBook-Pro fish % pipenv install Creating a virtualenv for this project… Pipfile: /Users/macname/Desktop/fish/Pipfile Using /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 (3.7.4) to create virtualenv… ⠙ Creating virtual environment...Already using interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7' New python executable in /Users/macname/.local/share/virtualenvs/fish-TU8Iv3va/bin/python3.7 Also creating executable in /Users/macname/.local/share/virtualenvs/fish-TU8Iv3va/bin/python Installing setuptools, pip, wheel... done. ✔ Successfully created virtual environment! Virtualenv location: /Users/macname/.local/share/virtualenvs/fish-TU8Iv3va Creating a Pipfile for this project… Pipfile.lock not found, creating… Locking [dev-packages] dependencies… Locking [packages] dependencies… Updated Pipfile.lock (a65489)! Installing dependencies from Pipfile.lock (a65489)… 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00 To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. macname@MacdeMacBook-Pro fish % pipenv shell Launching subshell in virtual environment… . /Users/macname/.local/share/virtualenvs/fish-TU8Iv3va/bin/activate macname@MacdeMBP fish % . /Users/macname/.local/share/virtualenvs/fish-TU8Iv3va/bin/activate (fish) macname@MacdeMBP fish % (fish) macname@MacdeMBP fish % pip3 list Package Version ---------- ------- pip 20.0.2 setuptools 46.1.3 wheel 0.34.2 (fish) macname@MacdeMBP fish % pip list Package Version ---------- ------- pip 20.0.2 setuptools 46.1.3 wheel 0.34.2 (fish) macname@MacdeMBP fish % (fish) macname@MacdeMBP fish % pip -V pip 20.0.2 from /Users/macname/.local/share/virtualenvs/fish-TU8Iv3va/lib/python3.7/site-packages/pip (python 3.7) (fish) macname@MacdeMBP fish %
安装flask,flask_restful
(fish) macname@MacdeMBP fish % pipenv install flask Installing flask… Adding flask to Pipfile's [packages]… ✔ Installation Succeeded Pipfile.lock (662286) out of date, updating to (a65489)… Locking [dev-packages] dependencies… Locking [packages] dependencies… ✔ Success! Updated Pipfile.lock (662286)! Installing dependencies from Pipfile.lock (662286)… 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 6/6 — 00:00:01 (fish) macname@MacdeMBP fish % (fish) macname@MacdeMBP fish % pipenv install flask_restful Installing flask_restful… Adding flask_restful to Pipfile's [packages]… ✔ Installation Succeeded Pipfile.lock (4d9cf9) out of date, updating to (662286)… Locking [dev-packages] dependencies… Locking [packages] dependencies… ✔ Success! Updated Pipfile.lock (4d9cf9)! Installing dependencies from Pipfile.lock (4d9cf9)… 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 10/10 — 00:00:02 (fish) macname@MacdeMBP fish %
使用pycharm打开fish文件夹,新建一个fisher.py文件
from flask import Flask from flask_restful import Api,Resource app = Flask(__name__) api = Api(app) class HelloWorld(Resource): def get(self): return {'hello': 'world'} api.add_resource(HelloWorld, '/') if __name__ == '__main__': app.run(debug=True)
命令行运行
(fish) macname@MacdeMBP fish % python3 fisher.py * Serving Flask app "fisher" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 315-121-294
打开浏览器
测试
from flask import Flask app = Flask(__name__) app.run()