#工作記錄
【筆記】結合 Conda任意創建和配置不同 Python 版本的雙軌隔離的 Poetry 虛擬環境-CSDN博客
在PowerShell中:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindowsLoading personal and system profiles took 3890ms.
(base) PS C:\Users\love> conda activate python311
(python311) PS C:\Users\love> cd F:\PythonProjects\test3
(python311) PS F:\PythonProjects\test3> & "D:\ProgramData\anaconda3\envs\python311\Scripts\poetry.exe" config virtualenvs.create true
(python311) PS F:\PythonProjects\test3> & "D:\ProgramData\anaconda3\envs\python311\Scripts\poetry.exe" initThis command will guide you through creating your pyproject.toml config.Package name [test3]: test3
Version [0.1.0]:
Description []:
Author [love530love <love530love@qq.com>, n to skip]:
License []:
Compatible Python versions [>=3.11]:Would you like to define your main dependencies interactively? (yes/no) [yes] yesYou can specify a package in the following forms:- A single name (requests): this will search for matches on PyPI- A name and a constraint (requests@^2.23.0)- A git url (git+https://github.com/python-poetry/poetry.git)- A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)- A file path (../my-package/my-package.whl)- A directory (../my-package/)- A url (https://example.com/packages/my-package-0.1.0.tar.gz)Package to add or search for (leave blank to skip):Would you like to define your development dependencies interactively? (yes/no) [yes] yes
Package to add or search for (leave blank to skip):Generated file[project]
name = "test3"
version = "0.1.0"
description = ""
authors = [{name = "*****",email = "*****@q q.com"}
]
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
][build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"Do you confirm generation? (yes/no) [yes] yes
(python311) PS F:\PythonProjects\test3>
過程解讀
這段 PowerShell 操作記錄完整展示了在 Conda 環境下使用 Poetry 初始化項目的過程,每個步驟都與 Windows 系統下 PowerShell 的特性緊密相關,以下是逐行解讀:
-
啟動 PowerShell 并加載配置
?
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindowsLoading personal and system profiles took 3890ms.
這部分是 PowerShell 的啟動信息,提示用戶當前使用的是 Windows PowerShell,展示了版權聲明、升級提示以及加載個人和系統配置文件所花費的時間。這些信息與后續的 Python 環境管理操作無直接關聯,但提供了操作的基礎環境背景。
2.?激活 Conda 環境
?
(base) PS C:\Users\love> conda activate python311
(python311) PS C:\Users\love>
(base) PS C:\Users\love>
?表示當前處于 Conda 的基礎環境。
conda activate python311
?命令用于激活名為?python311
?的 Conda 環境,激活成功后,命令提示符前綴變為?(python311) PS C:\Users\love>
,表明后續操作將在?python311
?環境中執行。
這一步是為后續使用該環境中的 Python 和 Poetry 工具做準備,確保操作在指定的 Python 版本環境下進行 。
3.?切換到項目目錄
?
(python311) PS C:\Users\love> cd F:\PythonProjects\test3
(python311) PS F:\PythonProjects\test3>
cd F:\PythonProjects\test3
?命令將當前工作目錄切換到?F:\PythonProjects\test3
,后續的 Poetry 操作將基于此目錄進行,確保項目相關文件(如?pyproject.toml
)生成在正確的位置。
4.?配置 Poetry 虛擬環境創建
?
(python311) PS F:\PythonProjects\test3> & "D:\ProgramData\anaconda3\envs\python311\Scripts\poetry.exe" config virtualenvs.create true
在 PowerShell 中,使用?&
?符號調用可執行文件(這里是 Poetry 的可執行文件?poetry.exe
),config virtualenvs.create true
?命令配置 Poetry,使其自動創建虛擬環境。如果不使用?&
?符號,PowerShell 會將其視為普通字符串,導致語法錯誤。這一步配置是為了讓 Poetry 在后續初始化項目時,自動為項目創建獨立的虛擬環境。
5.?初始化 Poetry 項目
?
(python311) PS F:\PythonProjects\test3> & "D:\ProgramData\anaconda3\envs\python311\Scripts\poetry.exe" init
再次使用?&
?調用?poetry.exe
?執行?init
?命令,啟動 Poetry 項目初始化向導。
后續的交互過程如下:
?
This command will guide you through creating your pyproject.toml config.Package name [test3]: test3
Version [0.1.0]:
Description []:
Author [***** <*****@q q.com>, n to skip]:
License []:
Compatible Python versions [>=3.11]:
Poetry 提示用戶輸入項目相關信息,如包名、版本、描述、作者、許可證以及兼容的 Python 版本。方括號內的值為默認值,用戶直接回車則采用默認值。這里用戶保持大部分默認設置,僅確認了包名為?test3
。
6.?配置項目依賴
?
Would you like to define your main dependencies interactively? (yes/no) [yes] yesYou can specify a package in the following forms:- A single name (requests): this will search for matches on PyPI- A name and a constraint (requests@^2.23.0)- A git url (git+https://github.com/python-poetry/poetry.git)- A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)- A file path (../my-package/my-package.whl)- A directory (../my-package/)- A url (https://example.com/packages/my-package-0.1.0.tar.gz)Package to add or search for (leave blank to skip):Would you like to define your development dependencies interactively? (yes/no) [yes] yes
Package to add or search for (leave blank to skip):
Poetry 詢問用戶是否交互式定義項目的主依賴和開發依賴,并列出了多種添加依賴的方式。用戶兩次選擇?yes
?后,均直接回車跳過添加,意味著當前項目暫不添加任何依賴。
7.?確認生成項目配置文件
?
Generated file[project]
name = "test3"
version = "0.1.0"
description = ""
authors = [{name = "*****",email = "*****@q q.com"}
]
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
][build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"Do you confirm generation? (yes/no) [yes] yes
Poetry 展示了根據用戶輸入生成的?pyproject.toml
?文件內容,包括項目基本信息、依賴配置和構建系統信息。用戶輸入?yes
?確認生成該配置文件,完成項目初始化過程。
總的來說,這段操作通過 PowerShell 在 Conda 環境中使用 Poetry 完成了項目初始化,充分體現了 PowerShell 調用可執行文件的語法特點,以及 Poetry 項目初始化的交互流程。
下一篇預告
CMD 與 PowerShell 中進入 Poetry 虛擬環境的操作方法