在windows环境下的pycharm中使用poetry(自己边做边写的,非搬)

1,899 阅读2分钟

1. pipx简介

  • pipx 是一个专门的python包安装程序。它只能用于安装带有 cli 入口点的软件包。
  • pipx 依赖于 pip (和 venv)

pipx官网**

2.安装pipx

参照上面官网安装

如果遇到pipx无法识别的问题(windows):

pipx : The term 'pipx' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ pipx list
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (pipx:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
pipx : 无法将“pipx”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ pipx install poetry
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (pipx:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

应先在编辑器外部cmd执行下列命令: python -m pip install --user pipx python -m pipx ensurepath python -m pipx install eth-brownie

再回到编辑器执行: python -m pipx ensurepath

3. 安装poetry

image.png

image.png

如果编辑器内无法识别poetry命令,记得重启一下

4. 使用poetry

在已有的项目中添加poetry: poetry init

而如果是新建一个项目,可以使用这个命令 poetry new my-project

创建虚拟环境: poetry install (这里执行完给出了创建的环境的路径,请注意这一点)

5. 常用的poetry 命令

安装 poetry add <pkg-name>

查看 poetry show

移除poetry remove <pkg-name>

更新【某个包】poetry update [pkg-name]

激活当前的虚拟环境,不用每次都run, 则使用:poetry shell

停用exit,不关闭停用deactivate (有无法关闭虚拟环境的问题,也许可以参考github.com/python-poet…

查看poetry安装路径 poetry env list --full-path

执行 peotry env info 查看虚拟环境的详细信息

6. 在pycharm中使用poetry

2021版pycharm之前需要插件,之后pycharm内置了。

官网说明:www.jetbrains.com/help/pychar…

但是我觉得官网说得不够清晰,我的操作是:在Settings ->Project -> Python Interpreter中,Add Python Interpreter -> Poetry Environment,base选择你的虚拟环境的python路径(就选择你的--full-path即可),Poetry executable 选择你的poetry执行目录,如下图所示:

image.png

(我这样做目前是可以满足我的需求,如果此处有异议,或者可能会发生意料之外的问题,恳请在评论区斧正)

摘录一条官网的注意事项:

External virtual environment management

Poetry will detect and respect an existing virtual environment that has been externally activated. This is a powerful mechanism that is intended to be an alternative to Poetry’s built-in, simplified environment management.

To take advantage of this, simply activate a virtual environment using your preferred method or tooling, before running any Poetry commands that expect to manipulate an environment.

7. 总结

从一个前端程序员理解的角度讲,pyproject.toml 很像package.json,管理包确实方便了很多