python3 -V
2.安裝 venv 環境
sudo apt install python3-venv -y
3.建立虛擬環境
cd ~
mkdir my_flask_app
cd my_flask_app
python3 -m venv venv
4.啟動venv
source venv/bin/activate
5.安裝Flask
pip install Flask
6.檢查 Flask 版本
python -m Flask --version
7.建立 hello.py
cd ~/my_flask_app/
touch hello.py
echo "from flask import Flask" > hello.py
echo "app = Flask(__name__)" >> hello.py
echo -e "\n" >> hello.py
echo "@app.route('/')" >> hello.py
echo "def hello_world():" >> hello.py
echo " return 'Hello World!'" >> hello.py
8.測試
export FLASK_APP=hello
flask run
那我們將上述步驟1~步驟8 寫成 shell,檔名為flaskInstall.sh,其內容為
#!/bin/sh
python3 -V
sudo apt install python3-venv -y
cd ~
mkdir my_flask_app
cd my_flask_app
python3 -m venv venv
source venv/bin/activate
pip install Flask
python -m Flask --version
cd ~/my_flask_app/
touch hello.py
echo "from flask import Flask" > hello.py
echo "app = Flask(__name__)" >> hello.py
echo "" >> hello.py
echo "@app.route('/')" >> hello.py
echo "def hello_world():" >> hello.py
echo " return 'Hello World!'" >> hello.py
export FLASK_APP=hello
flask run
接下來以一般帳號(有sudo 權限)登入
新增一個檔案 flaskInstall.sh,並將上面紅色文字複製貼到flaskInstall.sh
touch flaskinstall.sh
pico flaskInstall.sh
對 flaskInstall.sh 變更權限
啟動 flaskinstall.sh
source flaskinstall.sh
9.停用虛擬環境
deactivate
資料來源:
1.如何在 Ubuntu 18.04 上安装 Flask
沒有留言:
張貼留言