惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

博客园 - 稻草人.Net

Python多环境管理神器pyenv+poetry Spring REST 接口自定义404不能捕获NoHandlerFoundException问题 MacOS 安装Podman 替代Docker Prettier + ESLint + TS常用配置项 React Native V0.64.4版本 开发环境搭建及问题 Nodejs环境Eggjs加签验签示例 Mac安装compass失败相关问题 MySQL手动安装方法 Docker+Jenkins更换国内插件源 Chrome91版本 SameSite cookies 被移除后的解决方法 推荐几个文档中心搭建工具 前端开发Docker快速入门(二)制作镜像并创建容器 微信开放平台-第三方平台代小程序实现业务 微信开放平台-第三方平台授权流程及接口概述 Vuejs 3 Release:One Piece. Vuejs 3.0 正式版发布!代号:海贼王 尝试使用Nestjs搭建GraphQL服务 Mac安装Arduino搭建ESP8266 NodeMCU开发环境 用vscode进行jest单元测试并调试代码 vscode配置typescript和eslint的环境
python包管理利器poetry和conda使用简介
稻草人.Net · 2024-02-22 · via 博客园 - 稻草人.Net

conda 相对于主流的虚拟环境管理工具,在 python 开发环境中最大的特点便是 “不需要安装python”。但这并不意味着 conda 不需要 python 来运行脚本,而是 conda 不会依赖于系统中已经存在的 python 进行运行。

因此 conda 拥有较高的独立性以及强悍的跨版本支持。官方网点:https://docs.conda.io/projects/conda/en/latest/index.html

poetry作为一个传统虚拟环境的实现,poetry 凭借其强大的依赖分析能力被大量项目所推荐的虚拟环境管理工具。

poetry 作为首选的虚拟环境管理工具是最优方案,其强大的依赖分析能力、环境封装能力。

## 依赖包管理

安装requirements.txt中的所有内容

pip install -r requirements.txt

如何生成requirements.txt

pip freeze > requirements.txt

### Poetry包管理工具

poerty使用文档

<https://www.cnblogs.com/poloyy/tag/poetry/>

<https://pipx.pypa.io/stable/> <https://python-poetry.org/docs/>

<https://www.cnblogs.com/aifengqi/p/15394389.html>

- 安装poetry

pip instll poetry or pipx install poetry or pipx upgrade poetry

安装完成后可以看是否添加poetry至环境变量中

poetry --version

- 使用poetry创建虚拟环境

通常我们会设置 virtualenvs.create=true 并且直接使用 poetry install 等命令来直接自动创建虚拟环境,不过我们也可以通过 poetry env use 手动创建虚拟环境,或通过 poetry env use <解释器路径> 来手动指定一个 python 解释器。例如:

poetry env use C:\Users\Well404\AppData\Local\Programs\Python\Python310\python.exe

此命令便是使用了安装在默认路径的 Python3.10 解释器进行项目的创建。

- 手动创建虚拟环境

使用 poetry env use PYTHONPATH 命令创建虚拟环境, PYTHONPATH可以使用which python 查看python的路径。

- 激活当前虚拟环境

我们也可以通过 poetry run 命令 来直接在该项目的虚拟环境中执行命令,也可以通过 poetry shell 来显式激活这个虚拟环境。退出使用 exit 则退出当前虚拟环境

例如我们要运行这个虚拟环境中的 main.py 文件,便可使用 poetry run python main.py 或在 poetry shell 激活后直接输入 python main.py。

- 查看当前虚拟环境

poetry env list and poetry env info 查看当前虚拟环境的情况

- 虚拟环境命令

通过 poetry env -h 查看帮助文档

- 新建一个项目模板:

poetry new poetry-demo

- 转化现有的项目

已有项目中poetry 会对这个项目进行分析,并生成 pyproject.toml 文件,其过程类似于创建新项目的操作。

cd pre-existing-project

poetry init

- 调整poetry设置

通过 poetry config --list 命令,我们可以查看 poetry 的设置。

通过 poetry config 命令,我们可以调节 poetry 的设置。例如:

poetry config virtualenvs.create true

poetry config virtualenvs.in-project true

在此,我们简单了解两个配置项:virtualenvs.create 和 virtualenvs.in-project

当参数 virtualenvs.create 为 true 时,执行 poetry install 或 poetry add 时会检测当前项目是否有虚拟环境,没有就自动创建,默认为 true。

当参数 virtualenvs.in-project 为 true 时,虚拟环境的依赖将会放置于项目的文件夹内,而不是 poetry 默认的 {cache-dir}/virtualenvs,默认为 false。

通常情况下将这两项均设置为 true,这将简化我们的工作流程,以及更加方便的管理项目与其依赖。

- 使用poetry安装依赖

如果项目中已有 pyproject.toml 文件,可使用 poetry install 来直接安装其中所列出的依赖。poetry install --no-dev 参数以跳过 dev 使用的依赖,通常在部署项目时使用。

使用 poetry add <package> 可以在环境中安装新的依赖,例如 poetry add numpy,也可以一次性添加多个依赖 poetry add requests pendulum。

通过 --dev 参数可以指定为 dev 依赖,例如 poetry add pytest --dev。

通过指定版本号也可限定依赖的版本范围,例如 poetry add pendulum@^2.0.5 或 poetry add "pendulum>=2.0.5"。

- 安装开发依赖

poetry add uvicorn -D or --dev

- 其他常用命令

查看虚拟环境信息 poetry env info

显示虚拟环境所有列表 poetry env list

查看可以更新的依赖 poetry show --outdated

查看项目安装的依赖 poetry show

以树形结构查看项目安装的依赖关系 poetry show --tree

- vscode配置及调试

使用poetry env info -p 查到选择器的路径

在settings.json中添加如下代码

``` json

{

"python.defaultInterpreterPath": "/Users/webplus/Library/Caches/pypoetry/virtualenvs/fastapi-glm-demo-dEYW9svt-py3.11"

}

```

调试在launch.json中添加如下代码

``` json

{

"name": "Python 调试程序: FastAPI",

"type": "debugpy",

"request": "launch",

"module": "uvicorn",

"args": ["app.main:app", "--reload"],

"jinja": true

}

```

## conda虚拟环境管理

Python environments in VS Code <https://code.visualstudio.com/docs/python/environments>

- 创建一个名为 test 的虚拟环境。

conda create -n test

- 创建一个名为 test,且python版本为 3.8 的虚拟环境的虚拟环境。

conda create -n test python=3.8

- 从已有的虚拟环境 test 复制一份为 test_copy。

conda create -n test_copy --clone test

- 使用conda移除虚拟环境

移除一个名为 test 的虚拟环境。

conda remove -n test --all

- 查看conda虚拟环境列表

conda env list

- 使用conda激活虚拟环境

激活一个名为 test 的虚拟环境。

conda activate test

首次使用conda时:

source activate chatglm3-6b 或 conda activate chatglm3-6b

- 退出conda虚拟环境

conda deactivate

值得注意的是,此命令不包含任何额外的参数。因为一个终端仅能同时激活一个虚拟环境,因此也没有必要指定取消激活的虚拟环境的环境名。

- 返回主环境

如果想返回默认的python 2.7环境,运行

source deactivate py35 # for Linux & Mac

- 查看系统中的所有环境

用户安装的不同Python环境会放在~/anaconda/envs目录下。查看当前系统中已经安装了哪些环境,使用conda info -e。

conda env list 查询当前的conda虚拟环境 <https://blog.csdn.net/miracleoa/article/details/106115730>

## 运行

python -m sklearnex my_application.py

python -m app.main