Try   HackMD

Python環境設定

tags: python venv

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
設定Python虛擬環境-建議一專案一個虛擬環境

請先確定 apt 已更新至最新
sudo apt update -y
sudo apt upgrade - y

  1. 新增虛擬環境 : python3 -m venv myproject (myproject:專案名稱可換成你要的名稱)

  2. cd myproject

  3. 啟動虛擬環境: source bin/activate 離開虛擬環境: deactivate

  4. 查看已安裝模組: pip list (這時候環境很單純沒有不必要的模組)

Package    Version
---------- -------
pip        22.0.2
setuptools 59.6.0
  1. pip升級到最新版: pip install upgrade pip
(myproject) kevin@NB:~$ pip list
Package    Version
---------- -------
pip        22.0.2
setuptools 59.6.0
(myproject) kevin@NB:~$ python -m pip install --upgrade pip
Requirement already satisfied: pip in ./env/lib/python3.10/site-packages (22.0.2)
Collecting pip
  Using cached pip-22.3.1-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.0.2
    Uninstalling pip-22.0.2:
      Successfully uninstalled pip-22.0.2
Successfully installed pip-22.3.1
(myproject) kevin@NB:~$ pip list
Package    Version
---------- -------
pip        22.3.1
setuptools 59.6.0
(myproject) kevin@NB:~$
  1. mkdir app :把程式.py放在這個目錄裡並在這個目錄中新增requirements.txt
    填入你會用到的模組,例如 fastapi,line-bit-sdk,
    pip install -r requirements.txt (會自動安裝相依的模組)
(myproject1) kevin@CH-LAPTOP:~/myproject1/app$ cat requirements.txt
fastapi
line-bot-sdk
(myproject1) kevin@CH-LAPTOP:~/myproject1/app$ pip install -r requirements.txt
Collecting fastapi
  Downloading fastapi-0.88.0-py3-none-any.whl (55 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 55.5/55.5 kB 605.2 kB/s eta 0:00:00
Collecting line-bot-sdk
  Downloading line_bot_sdk-2.3.0-py2.py3-none-any.whl (88 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.5/88.5 kB 958.3 kB/s eta 0:00:00
...
Successfully installed aiohttp-3.8.3 aiosignal-1.3.1 anyio-3.6.2 async-timeout-4.0.2 attrs-22.2.0 certifi-2022.12.7 charset-normalizer-2.1.1 fastapi-0.88.0 frozenlist-1.3.3 future-0.18.2 idna-3.4 line-bot-sdk-2.3.0 multidict-6.0.4 pydantic-1.10.4 requests-2.28.1 sniffio-1.3.0 starlette-0.22.0 typing-extensions-4.4.0 urllib3-1.26.13 yarl-1.8.2

(myproject1) kevin@CH-LAPTOP:~/myproject1/app$ pip list
Package            Version
------------------ ---------
aiohttp            3.8.3
aiosignal          1.3.1
anyio              3.6.2
async-timeout      4.0.2
attrs              22.2.0
certifi            2022.12.7
charset-normalizer 2.1.1
fastapi            0.88.0
frozenlist         1.3.3
future             0.18.2
idna               3.4
line-bot-sdk       2.3.0
multidict          6.0.4
pip                22.3.1
pydantic           1.10.4
requests           2.28.1
setuptools         59.6.0
sniffio            1.3.0
starlette          0.22.0
typing_extensions  4.4.0
urllib3            1.26.13
yarl               1.8.2

也可以用指令 pip freeze > requirements.txt 將你會用到的模組寫進 requirements.txt中 ,未來移植專案時就能直接下指令
pip install -r requirements.txt 一鍵安裝所有的模組且不會有版本不合的問題。

或者可以安裝package,以安裝jupyter notebook為例

例如:pip install notebook

執行 jupyter notebook

Python 入門教學

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Python 入門教學

Author

  • 林奇賢