【笔记】Poetry虚拟环境创建示例

#工作记录

【笔记】结合 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/PSWindows

Loading 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" init

This 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] yes
        You 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 的特性紧密相关,以下是逐行解读:

  1. 启动 PowerShell 并加载配置

 

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

Loading 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] yes
        You 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 虚拟环境的操作方法

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AITechLab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值