一个简单、无依赖的库,用于通过封装命令行工具来使用PIP
项目描述
Python PIP包装器(用于pip
- 使用PyPi的Python包管理器)
一个小巧的无依赖项、单文件库,易于在Python应用程序或脚本中轻松使用pip
。
包含auto_install
辅助方法,可以检测缺少哪些指定的包,并自动安装它们。
代码库故意限制为单文件pipwrapper.py
,适用于这些用例
- 它使开发人员能够轻松地将文件的內容复制并粘贴到自包含的单文件Python脚本中,以便在最终用户安装任何Python包之前自动安装依赖项。
- 对于可以使用多个文件/文件夹的项目,它允许将
pipwrapper.py
脚本下载并存储在项目的任何文件/模块中,无需修改任何引用
官方仓库: https://github.com/Privex/pipwrapper
压缩版本
由于此库在单个文件中自包含,且不包含外部依赖项(除明显的Python和Pip外),因此可以将pipwrapper.py
压缩成更小的文件/字符/行数。
压缩版本可以复制粘贴到现有脚本中,或者保存到.py
文件中并从中导入。
请注意:脚本的压缩版本使用制表符而不是空格,作为众多节省空间的方法之一。您可能需要将制表符转换为空格,以便与您的项目兼容。
此外 - 请确保我们的许可声明作为文档字符串在Pip
类中可见,或者在将文件用作独立文件时在文件顶部可见。我们的./minify.sh
脚本在压缩后自动在文件顶部注入一个小型、基本的许可声明,并在Pip类下方作为文档字符串。
您可以在设置好开发环境(git clone; cd pipwrapper; pip3 install -r requirements.txt;
)后,通过运行./minify.sh
来生成自己的pipwrapper.py
的压缩版本。
我们偶尔也会在发布页面上发布一个压缩文件,您可以直接将其粘贴到现有的脚本中,或者保存到.py
文件中导入。
如果我们的压缩脚本出现错误导致未能添加许可证声明,或者您手动将pipwrapper.py
裁剪到适合您项目的适当大小 - 请确保在class Pip
下或文件顶部添加我们的许可证声明作为文档字符串。
以下是我们的压缩版本的小型简单许可证声明
"""
(C) Copyright 2021 - Privex Inc. https://www.privex.io
PIPWrapper is released under the X11 / MIT License
Official Repo: https://github.com/Privex/pipwrapper
"""
快速安装/使用
使用pip从PyPi安装
pip3 install -U pipwrapper
基本用法
from pipwrapper import Pip, ProcResult
########
# It's not required to initialise an instance of Pip() - however, it's recommended, as it
# will allow you to make use of functionality that's not available on bare classes, such
# as dynamically generated command wrapper methods for methods that haven't yet been defined,
# using the '.__getattr__' instance method.
#
# If you only need the basics - i.e. 'install', 'uninstall', 'auto_install', 'call', and
# any of the other pre-defined classmethod's - then you should be able to call them directly
# via the class, e.g. Pip.auto_install('privex-helpers', 'Quart', 'dnspython')
#
pip = Pip()
########
# Automatically install any of these specified packages if they aren't already installed
res = pip.auto_install('privex-helpers', 'Django', 'requests', 'httpx')
if res.retcode != 0:
print("Something went wrong while installing packages using auto_install.")
print("Output from Pip was:")
print("\n", res.stdout.decode(), "\n\n")
########
# Directly run 'pip install -U' for one or more packages
pip.install("psycopg2", "mysqlclient")
########
# Uninstall one or more packages
pip.uninstall("requests")
########
# Let's install 'requests' again, but this time, using 'output=True' to pipe pip's stdout/stderr
# directly to the console of this script, instead of capturing the output into a variable.
pip.install('requests', output=True)
########
# You can also call .freeze() to run 'pip freeze', which will capture the output, and will parse the lines of package==version
# into a standard Python list() for you to be able to process easily.
frz = pip.freeze()
print(frz)
# [
# "asgiref==3.3.1", "async-property==0.2.1", "attrs==20.3.0",
# "Django==3.1.7", "httpx==0.17.1",
# "privex-helpers==3.2.1", "privex-loghelper==1.0.6",
# "python-dateutil==2.8.1", "pytz==2021.1",
# "requests==2.25.1", "sniffio==1.2.0",
# "urllib3==1.26.4"
# ]
########
# Alternatively, instead of using .freeze(), you can use .installed_packages() (a wrapper around .freeze()) to generate a
# list of plain package names that are currently installed :)
pkgs = pip.installed_pkgs()
print(pkgs)
# [
# 'asgiref', 'async-property', 'attrs', 'certifi', 'chardet', 'Django', 'h11',
# 'httpcore', 'httpx', 'idna', 'privex-helpers', 'privex-loghelper', 'python-dateutil',
# 'pytz', 'requests', 'rfc3986', 'six', 'sniffio', 'sqlparse', 'urllib3'
# ]
########
# While only a handful of pip sub-commands have wrapper method available, you can call any arbitrary undefined attribute
# like a command method, then it will dynamically generate and return a function that will call the sub-command with that name,
# and it'll pass any positional arguments directly to the sub-command.
#
# For example, as of v0.5.0 (27 Mar 2021), the '.show()' method does not exist on the Pip class, however, if you call .show()
# with the name of an installed package like below, it will dynamically generate a function that will run 'pip show ARGS...'
# and return a ProcResult just like the defined command wrapper methods :)
res_show: ProcResult = pip.show('privex-helpers')
print(res_show.stdout.decode())
# Name: privex-helpers
# Version: 3.2.1
# Summary: A variety of helper functions and classes, useful for many different projects
# Home-page: https://github.com/Privex/python-helpers
# Author: Chris (Someguy123) @ Privex
# License: MIT
# Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
# Requires: python-dateutil, async-property, sniffio, privex-loghelper, attrs
# Required-by: privex-db
信息
此Python PIP库由@someguy123在Privex Inc.开发,旨在使在Python脚本中工作与pip
变得容易 - 尤其是在需要能够自动安装其依赖项的自包含单个文件脚本中。
+===================================================+
| © 2021 Privex Inc. |
| https://www.privex.io |
+===================================================+
| |
| Python PIP Wrapper Library |
| License: X11/MIT |
| Repo: https://github.com/Privex/pipwrapper |
| |
| Core Developer(s): |
| |
| (+) Chris (@someguy123) [Privex] |
| |
+===================================================+
Python PIP Wrapper (PyPi Wrapper) - A simple, dependency-free library for using PIP via wrapping
the CLI utility (python3.x -m pip ARGS)
Copyright (c) 2021 Privex Inc. ( https://www.privex.io )
安装
由于使用了参数和返回类型提示,我们建议您使用至少Python 3.6+。
使用pip
从PyPi安装
您可以通过pip安装此软件包
pip3 install -U pipwrapper
(替代)从Git手动安装
如果您不想使用PyPi(例如,对于尚未在PyPi上发布的开发版本),您可以直接从我们的Git仓库安装项目。
除非您有特定的理由要手动安装,否则您应该像上面所示,通常使用pip3安装。
选项1 - 使用pip直接从Github安装
pip3 install git+https://github.com/Privex/pipwrapper
选项2 - 克隆并手动安装
# Clone the repository from Github
git clone https://github.com/Privex/pipwrapper.git
cd pipwrapper
# RECOMMENDED MANUAL INSTALL METHOD
# Use pip to install the source code
pip3 install .
# ALTERNATIVE INSTALL METHOD
# If you don't have pip, or have issues with installing using it, then you can use setuptools instead.
python3 setup.py install
贡献
我们非常愿意接受pull请求,并处理向我们报告的任何问题。
以下是一些重要信息
报告问题
- 对于错误报告,您应包括以下信息
pipwrapper
版本 - 使用pip3 freeze
- 如果未通过PyPi版本安装,则包括测试该问题的git修订号 -
git log -n1
- 如果未通过PyPi版本安装,则包括测试该问题的git修订号 -
- 您的python3版本 -
python3 -V
- 您的操作系统和操作系统版本(例如,Ubuntu 20.04,Debian 10)
- 对于功能请求/更改
- 请避免需要第三方依赖的建议。此工具旨在轻量级,不包含外部依赖。
- 清楚地说明您希望添加的功能/更改
- 解释为什么该功能/更改对我们或该工具的其他用户有用
- 请注意,添加复杂或我们认为对我们使用该工具不必要的功能/更改可能不会被添加(但我们可以接受PR)
拉取请求
- 我们将愉快地接受仅添加代码注释或README更改的PR
- 在贡献代码时请使用4个空格,而不是制表符
- 您可以使用Python 3.4+的功能(我们为我们的项目运行Python 3.7+)
- 需要尚未发布给最新稳定版本Ubuntu Server LTS(目前为Ubuntu 18.04 Bionic)的Python版本的特性将不予接受。
- 在标题和描述中清楚地说明您的pull请求的目的
- 您都做了哪些更改?
- 为什么您要做出这些更改?
- 请确保代码贡献有适当的注释 - 我们不会接受涉及未注释、高度简洁的单行代码的更改。
贡献的法律免责声明
没有人愿意阅读一个充满法律文本的长文档,所以我们在这里总结了重要部分。
如果您将您创建/拥有的内容贡献给Privex创建/拥有的项目,例如代码或文档,那么您可能会自动授予我们无限制使用您内容的使用权,无论适用于我们项目的开源许可协议。
如果您不想授予我们无限制使用您内容的使用权,您应确保将您的内容放入单独的文件中,并确保在文件开头(例如代码注释)或其包含的文件夹内(例如命名为LICENSE的文件)清楚地显示您内容的许可协议。
您应在您的pull请求或issue中让我们知道您已包含受单独许可协议约束的文件,以便我们确保没有可能导致我们无法接受您的贡献的许可协议冲突。
如果您想阅读整个法律文本,它应包含为privex_contribution_agreement.txt
。
许可
本项目采用X11 / MIT
许可协议。有关详细信息,请参阅LICENSE
文件。
以下是重要内容
- 如果您修改/分发/复制本项目的一部分或全部,必须包含/显示许可协议和版权声明(
LICENSE
)。 - 未经我们许可,您不得使用我们的名称来推广/支持您的产品。然而,您可以声明您的产品使用了本项目的一部分或全部。
感谢您阅读!
如果本项目对您有帮助,请考虑从Privex购买VPS或专用服务器 - 价格低至每月仅US$0.99(我们接受加密货币!)
项目详情
下载文件
下载适合您平台文件的版本。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
pipwrapper-0.5.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cae5e319e07422e0606dee4346f0d313d78a62e3a52b0de975d9ccad7e4112f2 |
|
MD5 | 95cfe100736c27ea5c758b1bb73ae328 |
|
BLAKE2b-256 | 97709eff85b418db762befa7fedcd2eac443db4ac06618720cb77d103f3e4352 |
pipwrapper-0.5.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 56f453b39676ef4ec06f34e8fc929cff9ebe1d2acbf33aac9784a15a80d210f5 |
|
MD5 | 257895497758f5feaf3fe85658ac8181 |
|
BLAKE2b-256 | 694d76984b62d07e0fdead30ff776d5a16482c3f1a1f15d95ed84ec69c694996 |