由于Poetry中的requiremtns.txt个文件很难阅读,所以我try 编写一个简单的脚本来创建requiremtns.txt个文件,同时在它旁边创建一个更易于阅读的文件.一个Fitting Porey命令是:

~$ poetry show

好的……所以我启动我的子流程来执行以下代码:

if __name__ == '__main__':
    PROJECT_DIR = Path(__file__).parent
    # CMD
    package_list = subprocess.run(['poetry', 'show'], cwd=PROJECT_DIR, encoding='utf_8', capture_output=True)
    # Powershell
    # package_list = subprocess.run(['powershell.exe', '-Command', 'poetry', 'show'], cwd=PROJECT_DIR, encoding='utf_8', capture_output=True)
    print(package_list.stdout)

唯一的问题是,我得到了这个"删减"的内容:

alabaster                       0.7.16          A light, configurable Sphin...
argon2-cffi                     23.1.0          Argon2 for Python
argon2-cffi-bindings            21.2.0          Low-level CFFI bindings for...
asgiref                         3.7.2           ASGI specs, helper code, an...
attrs                           23.2.0          Classes Without Boilerplate
autopep8                        2.0.4           A tool that automatically f...
babel                           2.14.0          Internationalization utilities
certifi                         2023.11.17      Python package for providin...
cffi                            1.16.0          Foreign Function Interface ...
charset-normalizer              3.3.2           The Real First Universal Ch...
colorama                        0.4.6           Cross-platform colored term...
concurrent-log-handler          0.9.25          RotatingFileHandler replace...
cryptography                    41.0.7          cryptography is a package w...
django                          5.0.1           A high-level Python web fra...
django-cors-headers             4.3.1           django-cors-headers is a Dj...
django-cprofile-middleware      1.0.5           Easily add cProfile profili...
django-rest-knox                4.2.0           Authentication for django r...
django-silk                     5.1.0           Silky smooth profiling for ...
django-stubs                    4.2.7           Mypy stubs for Django
django-stubs-ext                4.2.7           Monkey-patching and extensi...
djangorestframework             3.14.0          Web APIs for Django, made e...
djangorestframework-dataclasses 1.3.1           A dataclasses serializer fo...
djangorestframework-stubs       3.14.5          PEP-484 stubs for django-re...
docutils                        0.20.1          Docutils -- Python Document...
drf-spectacular                 0.27.1          Sane and flexible OpenAPI 3...

正如您所看到的,我已经try 使用PowerShell,因为我认为这是一个CMD问题,但结果保持不变.我还try 将stdout直接重新路由到一个文件中.还是老样子.我也try 了无缓冲版本(bufsize=0),仍然是一样的…

Now the question:我需要做什么,才能获得"完整"文本,就像你在终端输入poetry show得到的一样?

更新

我现在已经在Ubuntu操作系统上try 过了.结果是相同的,因此这是特定于NOT个Windows的.

推荐答案

通过判断源代码,我可以看到POLITE unconditionally limits the output of poetry show to the current terminal's width,所以即使手动运行命令,您也不会总是获得每个包的完整描述:

width = shutil.get_terminal_size().columns

...

if len(locked.description) > remaining:
    description = description[: remaining - 3] + "..."

shutil.get_terminal_size的文档说,如果没有连接到终端,它默认为80列(当您使用subprocess.run设置capture_output=True时就是这种情况),这解释了为什么您的输出被截断.文档中说,如果设置了环境变量COLUMNS,它将使用环境变量COLUMNS的值,因此一种解决方案是在子流程调用中将其设置为一个非常大的值:

subprocess.run(['poetry', 'show'], capture_output=True, env={'COLUMNS': '9999'})

这并不理想,因为无法保证任何给定的输出行有多长(尽管9999个字符似乎是一个安全数字);更可靠的方法是解析poetry.lock(或者根据您的需要解析pyproject.toml),因为看起来poetry show的输出并不是机器可读的:

import tomllib

with open('poetry.lock', 'rb') as lockf:
    lock = tomllib.load(lockf)

for package in lock['package']:
    print(package['name'], package['version'], package['description'])

如果您想使用Pory自己计算的信息(如poetry show --outdated的输出),这将不起作用,但如果您不需要比常规poetry show提供的任何信息更多的信息,则通常比运行子进程更好.

Python相关问答推荐

在for循环中仅执行一次此操作

Pandas 除以一列中出现的每个值

在应用循环中间保存pandas DataFrame

Python -根据另一个数据框中的列编辑和替换数据框中的列值

在Pandas框架中截短至固定数量的列

用gekko解决的ADE方程系统突然不再工作,错误消息异常:@错误:模型文件未找到.& &

如何在BeautifulSoup中链接Find()方法并处理无?

如何使用Python将工作表从一个Excel工作簿复制粘贴到另一个工作簿?

数据抓取失败:寻求帮助

如何在Python脚本中附加一个Google tab(已经打开)

OR—Tools中CP—SAT求解器的IntVar设置值

当点击tkinter菜单而不是菜单选项时,如何执行命令?

不允许访问非IPM文件夹

删除marplotlib条形图上的底边

python—telegraph—bot send_voice发送空文件

找到相对于列表索引的当前最大值列表""

如何将数据帧中的timedelta转换为datetime

统计numpy. ndarray中的项目列表出现次数的最快方法

如何求相邻对序列中元素 Select 的最小代价

如何训练每一个pandaprame行的线性回归并生成斜率