使用pip将Python软件包从本地文件系统文件夹安装到virtualenv

本文翻译自:Installing Python packages from local file system folder to virtualenv with pip

Is it possible to install packages using pip from the local filesystem? 是否可以使用本地文件系统中的pip安装软件包?

I have run python setup.py sdist for my package, which has created the appropriate tar.gz file. 我已经为我的程序包运行了python setup.py sdist ,它已经创建了适当的tar.gz文件。 This file is stored on my system at /srv/pkg/mypackage/mypackage-0.1.0.tar.gz . 此文件存储在我的系统上的/srv/pkg/mypackage/mypackage-0.1.0.tar.gz

Now in a virtual environment I would like to install packages either coming from pypi or from the specific local location /srv/pkg . 现在,在虚拟环境中,我想安装来自pypi或来自特定本地位置/srv/pkg软件包。

Is this possible? 这可能吗?

PS I know that I can specify pip install /srv/pkg/mypackage/mypackage-0.1.0.tar.gz . PS我知道我可以指定pip install /srv/pkg/mypackage/mypackage-0.1.0.tar.gz That will work, but I am talking about using the /srv/pkg location as another place for pip to search if I typed pip install mypackage . 那将起作用,但是我正在谈论使用/srv/pkg位置作为pip搜索是否输入pip install mypackage另一个位置。


#1楼

参考:https://stackoom.com/question/114Qg/使用pip将Python软件包从本地文件系统文件夹安装到virtualenv


#2楼

I am pretty sure that what you are looking for is called --find-links option. 我很确定您要寻找的是--find-links选项。

Though you might need to generate a dummy index.html for your local package index which lists the links to all packages. 尽管您可能需要为本地软件包索引生成一个虚拟的index.html ,其中列出了所有软件包的链接。 This tool helps: 该工具有助于:

https://github.com/wolever/pip2pi https://github.com/wolever/pip2pi


#3楼

What about:: 关于什么::

pip install --help
...
  -e, --editable <path/url>   Install a project in editable mode (i.e. setuptools
                              "develop mode") from a local project path or a VCS url.

eg, pip install -e /srv/pkg 例如, pip install -e /srv/pkg

where /srv/pkg is the top-level directory where 'setup.py' can be found. / srv / pkg是可在其中找到“ setup.py”的顶级目录。


#4楼

This is the solution that I ended up using: 这是我最终使用的解决方案:

import pip


def install(package):
    # Debugging
    # pip.main(["install", "--pre", "--upgrade", "--no-index",
    #         "--find-links=.", package, "--log-file", "log.txt", "-vv"])
    pip.main(["install", "--upgrade", "--no-index", "--find-links=.", package])


if __name__ == "__main__":
    install("mypackagename")
    raw_input("Press Enter to Exit...\n")

I pieced this together from pip install examples as well as from Rikard's answer on another question . 我从pip安装示例以及Rikard另一个问题 的回答中总结了这一点。 The "--pre" argument lets you install non-production versions. “ --pre”参数使您可以安装非生产版本。 The "--no-index" argument avoids searching the PyPI indexes. “ --no-index”参数避免搜索PyPI索引。 The "--find-links=." “ --find-links =”。 argument searches in the local folder (this can be relative or absolute). 参数在本地文件夹中搜索(可以是相对的也可以是绝对的)。 I used the "--log-file", "log.txt", and "-vv" arguments for debugging. 我使用了“ --log-file”,“ log.txt”和“ -vv”参数进行调试。 The "--upgrade" argument lets you install newer versions over older ones. “ --upgrade”参数使您可以在较旧的版本上安装较新的版本。

I also found a good way to uninstall them. 我还找到了卸载它们的好方法。 This is useful when you have several different Python environments. 当您有多个不同的Python环境时,这很有用。 It's the same basic format, just using "uninstall" instead of "install", with a safety measure to prevent unintended uninstalls: 这是相同的基本格式,只是使用“卸载”而不是“安装”,并采取了安全措施来防止意外卸载:

import pip


def uninstall(package):
    response = raw_input("Uninstall '%s'? [y/n]:\n" % package)
    if "y" in response.lower():
        # Debugging
        # pip.main(["uninstall", package, "-vv"])
        pip.main(["uninstall", package])
    pass


if __name__ == "__main__":
    uninstall("mypackagename")
    raw_input("Press Enter to Exit...\n")

The local folder contains these files: install.py, uninstall.py, mypackagename-1.0.zip 本地文件夹包含以下文件:install.py,uninstall.py,mypackagename-1.0.zip


#5楼

I am installing pyfuzzy but is is not in PyPI; 我正在安装pyfuzzy但不在PyPI中; it returns the message: No matching distribution found for pyfuzzy . 它返回消息: No matching distribution found for pyfuzzy

I tried the accepted answer 我尝试了接受的答案

pip install  --no-index --find-links=file:///Users/victor/Downloads/pyfuzzy-0.1.0 pyfuzzy

But it does not work either and returns the following error: 但它也不起作用,并返回以下错误:

Ignoring indexes: https://pypi.python.org/simple Collecting pyfuzzy Could not find a version that satisfies the requirement pyfuzzy (from versions: ) No matching distribution found for pyfuzzy 忽略索引: https ://pypi.python.org/simple收集pyfuzzy找不到满足pyfuzzy要求的版本(来自版本:)找不到与pyfuzzy匹配的发行版

At last , I have found a simple good way there: https://pip.pypa.io/en/latest/reference/pip_install.html 最后,我找到了一个简单的好方法: https : //pip.pypa.io/en/latest/reference/pip_install.html

Install a particular source archive file.
$ pip install ./downloads/SomePackage-1.0.4.tar.gz
$ pip install http://my.package.repo/SomePackage-1.0.4.zip

So the following command worked for me: 所以以下命令对我有用:

pip install ../pyfuzzy-0.1.0.tar.gz.

Hope it can help you. 希望它能对您有所帮助。


#6楼

An option --find-links does the job and it works from requirements.txt file! 一个--find-links选项可以完成这项工作,并且可以从requirements.txt文件中使用!

You can put package archives in some folder and take the latest one without changing the requirements file, for example requirements : 您可以将软件包归档文件放在某个文件夹中,并在不更改需求文件的情况下获取最新的归档文件,例如requirements

.
└───requirements.txt
└───requirements
    ├───foo_bar-0.1.5-py2.py3-none-any.whl
    ├───foo_bar-0.1.6-py2.py3-none-any.whl
    ├───wiz_bang-0.7-py2.py3-none-any.whl
    ├───wiz_bang-0.8-py2.py3-none-any.whl
    ├───base.txt
    ├───local.txt
    └───production.txt

Now in requirements/base.txt put: 现在在requirements/base.txt放:

--find-links=requirements
foo_bar
wiz_bang>=0.8

A neat way to update proprietary packages, just drop new one in the folder 一种更新专有软件包的好方法,只需将新软件包放入文件夹中

In this way you can install packages from local folder AND pypi with the same single call: pip install -r requirements/production.txt 这样,您可以通过相同的一次调用从local folderpypi安装软件包: pip install -r requirements/production.txt

PS. PS。 See my cookiecutter-djangopackage fork to see how to split requirements and use folder based requirements organization. 请参阅我的cookiecutter-djangopackage分支,以了解如何拆分需求并使用基于文件夹的需求组织。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值