当前位置: 首页 > backend >正文

【笔记】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/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 的特性紧密相关,以下是逐行解读:

  1. 启动 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 虚拟环境的操作方法

http://www.xdnf.cn/news/12839.html

相关文章:

  • Prompt Tuning(提示调优)到底训练优化的什么部位
  • DiscuzX3.5发帖json api
  • maven 1.0.0idea的使用说明
  • Vue3学习(watchEffect,标签的ref属性,计数器,defineExpose)
  • SpringCloud学习笔记-4
  • 实验二:数码管动态显示实验
  • 建造者模式深度解析与实战应用
  • WEB3技术重要吗,还是可有可无?
  • STM32入门学习之系统时钟配置
  • K8S认证|CKS题库+答案| 7. Dockerfile 检测
  • 五、jmeter脚本参数化
  • PHP中如何定义常量以及常量和变量的主要区别
  • Spark流水线+Gravitino+Marquez数据血缘采集
  • java综合项目开发一课一得
  • 使用 Melos 高效管理 Flutter/Dart Monorepo 项目
  • 用 Melos 解决 Flutter Monorepo 的依赖冲突:一个真实案例
  • Python 包管理器 uv 介绍
  • 基于PostGIS的各地级市路网长度统计及Echarts图表可视化实践-以湖南省为例
  • 支持selenium的chrome driver更新到137.0.7151.68
  • 时序数据库IoTDB结合SeaTunnel实现高效数据同步
  • 七、Sqoop Job:简化与自动化数据迁移任务及免密执行
  • Ubuntu20.04中 Redis 的安装和配置
  • 通过Cline使用智能体
  • webpack其余配置
  • uni-app学习笔记二十七--设置底部菜单TabBar的样式
  • AUTOSAR实战教程--标准协议栈实现DoIP转DoCAN的方法
  • 12-OPENCV ROCKX项目 人脸拍照
  • 【Blender】Blender 基础:导入导出
  • 【算法】【优选算法】优先级队列
  • Hermite 插值