有關虛擬環境的相關的我之前也介紹過,python高級(4)—— 虛擬環境安裝使用
比如pipenv,還有virtualenv之類的,還有本篇文章的主角 -- poetry
到底哪個更好用呢,看你個人吧,我覺得poetry更好一點咯,不過還是看個人喜好了
一:conda創建虛擬環境
1.安裝conda
這個就省略了,網上太多教程了
2.創建虛擬環境:
conda create -n py37 python=3.7
二:poetry使用教程
簡介:
poetry是一個依賴項管理和打包工具,它運行聲明項目所依賴的庫,很方便的管理項目環境,僅支持python3.6+
安裝:
官方不推薦用pip安裝,雖然也能安裝,但是使用起來不方便
1.mac/linux:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3
2.windows:
powershell下安裝(cmd下回提示“.Content”相關錯誤)
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python
配置poetry環境變量
windows系統:
path變量里添加如下:
%USERPROFILE%\.poetry\bin
mac/linux:
安裝完即可使用,可能需要配置軟鏈接,找到安裝目錄,ln命令配置進軟鏈接即可
驗證是否安裝成功
終端命令下使用poetry --version,如果輸出版本即可
注意:
如果上面安裝時,速度較慢解決辦法:
- 將 https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py 代碼內容另存到本地,比如存儲為 get-poetry.py
- 在 https://github.com/python-poetry/poetry/releases 下載對應平台的 realse 版本,比如 1.1.4 的 windows 版本 poetry-1.1.4-win32.tar.gz
- 運行 python get-poetry.py(用絕對路徑或者進入所在目錄使用) --file poetry-1.1.4-win32.tar.gz(用絕對路徑或者進入所在目錄使用)
- 配置環境變量,就可以愉快的使用了
- 如果下載poetry包時還是下載很慢或者代理速度不給力,使用github鏡像站下載:
把域名https://github.com替換成https://github.wuyanzheshui.workers.dev即可
比如: 原鏈接:https://github.com/python-poetry/poetry/releases/download/1.1.4/poetry-1.1.4-win32.tar.gz
替換之后:https://github.wuyanzheshui.workers.dev/python-poetry/poetry/releases/download/1.1.4/poetry-1.1.4-win32.tar.gz 下載速度就飛起了
poetry常用命令
三: pip 切換安裝源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple ,記得用https
四:poetry+conda配置環境
以下步驟請在終端下執行,如果是windows的話,建議使用commder或者ConEmu,不要用cmd,cmd有中文編碼問題
0.創建目錄:
poetry new xx文件夾名
1.初始化:
這里如果沒有用第0個步驟的命令,而是進入已存在或者事先創建的目錄下,就需要使用這個命令
poetry init
結果:
This command will guide you through creating your pyproject.toml config.
Package name [baseprojectone]:
Version [0.1.0]:
Description []:
Author [xxxxxx <yh@xxxxxtech.com>, n to skip]:
License []:
Compatible Python versions [^3.7]:
Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file
[tool.poetry]
name = "baseprojectone"
version = "0.1.0"
description = ""
authors = ["xxxxxx <yh@xxxxxtech.com>"]
[tool.poetry.dependencies]
python = "^3.7"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Do you confirm generation? (yes/no) [yes]
2.指定解釋器:
poetry env use /Users/dm/.miniconda3/envs/py37/bin/python(conda創建的虛擬環境,如果是windows系統,找到python.exe文件)
比如我的路徑是C:\programe\conda\envs\py37\python.exe,那么就用 poetry env use C:\programe\conda\envs\py37\python.exe
3.安裝相關初始環境
這里前提是必須已經配置好pip安裝源,不然會很慢
poetry install
結果:
Updating dependencies
Resolving dependencies... (12.4s)
Writing lock file
Package operations: 13 installs, 0 updates, 0 removals
• Installing typing-extensions (3.7.4.3)
• Installing zipp (3.4.0)
• Installing importlib-metadata (3.4.0)
• Installing pyparsing (2.4.7)
• Installing atomicwrites (1.4.0)
• Installing attrs (20.3.0)
• Installing colorama (0.4.4)
• Installing more-itertools (8.6.0)
• Installing packaging (20.8)
• Installing pluggy (0.13.1)
• Installing py (1.10.0)
• Installing wcwidth (0.2.5)
• Installing pytest (5.4.3)
四.配置項目環境:
1.命令:
poetry add git+ssh://git@xxxxxx.git
poetry add requests
注意:
如果有提示更新pip的,復制相關命令執行:C:\Users\xxxxx\AppData\Local\pypoetry\Cache\virtualenvs\mypj1-HtrT1G92-py3.7\Scripts\python.exe -m pip install --upgrade pip
最后安裝成功:
Updating dependencies
Resolving dependencies... (3.8s)
Package operations: 1 install, 0 updates, 0 removals
• Installing xxx (0.0.2 1bd38afe2c)
五:pycharm使用poetry創建的虛擬環境創建項目
1.pycharm先安裝poetry插件
低版本的pycharm不支持,最好用最新版pycharm
2.打開項目所在目錄:
在打開時,會彈出一個創建bat文件的窗口,點【ok】
3.如果沒有解釋器環境的話,找到創建那個路徑的解釋器環境,比如:C:\Users\xxxx\AppData\Local\pypoetry\Cache\virtualenvs\mypj1-HtrT1G92-py3.7\Scripts\python.exe,添加一個解釋器環境即可,這個情況只有在第二步讓創建bat文件沒點ok才會出現
接下來就可以進行你的項目開發了