我需要直接在脚本中安装来自 PyPi 的包。 Maybe there’s some module or distutils
( distribute
, pip
etc.) feature which allows me to just execute something like pypi.install('requests')
and requests will安装到我的 virtualenv 中。
原文由 chuwy 发布,翻译遵循 CC BY-SA 4.0 许可协议
我需要直接在脚本中安装来自 PyPi 的包。 Maybe there’s some module or distutils
( distribute
, pip
etc.) feature which allows me to just execute something like pypi.install('requests')
and requests will安装到我的 virtualenv 中。
原文由 chuwy 发布,翻译遵循 CC BY-SA 4.0 许可协议
您还可以使用类似的东西:
import pip
def install(package):
if hasattr(pip, 'main'):
pip.main(['install', package])
else:
pip._internal.main(['install', package])
# Example
if __name__ == '__main__':
install('argh')
原文由 Rikard Anglerud 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5k 阅读✓ 已解决
2 回答1k 阅读✓ 已解决
4 回答936 阅读✓ 已解决
3 回答1.1k 阅读✓ 已解决
3 回答1.1k 阅读✓ 已解决
1 回答1.7k 阅读✓ 已解决
1 回答1.2k 阅读✓ 已解决
官方推荐的从脚本安装包的方法是通过子进程调用 pip 的命令行界面。 pip 不支持此处提供的大多数其他答案。此外,自 pip v10 以来,所有代码都已移至
pip._internal
正是为了向用户明确表示不允许以编程方式使用 pip。使用
sys.executable
确保您将调用相同的pip
与当前运行时关联。