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

是否可以使用本地文件系统中的pip安装软件包?

我已经python setup.py sdist为我的程序包运行了,该程序包已经创建了相应的tar.gz文件。该文件存储在我的系统上,位置为/srv/pkg/mypackage/mypackage-0.1.0.tar.gz

现在,在虚拟环境中,我想安装来自pypi或来自特定本地位置的软件包/srv/pkg

这可能吗?

PS 我知道我可以指定pip install /srv/pkg/mypackage/mypackage-0.1.0.tar.gz。可以,但是我正在谈论使用该/srv/pkg位置作为我输入时pip搜索的另一个位置pip install mypackage

Is it possible to install packages using pip from the local filesystem?

I have run python setup.py sdist for my package, which has created the appropriate tar.gz file. This file is stored on my system at /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.

Is this possible?

PS I know that I can specify 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.


回答 0

我很确定您正在寻找的东西称为--find-links选项。

虽然您可能需要index.html为本地软件包索引生成一个虚拟对象,该虚拟对象列出了所有软件包的链接。该工具有助于:

https://github.com/wolever/pip2pi

I am pretty sure that what you are looking for is called --find-links option.

Though you might need to generate a dummy index.html for your local package index which lists the links to all packages. This tool helps:

https://github.com/wolever/pip2pi


回答 1

关于什么::

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.

例如, pip install -e /srv/pkg

/ srv / pkg是可在其中找到“ setup.py”的顶级目录。

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

where /srv/pkg is the top-level directory where ‘setup.py’ can be found.


回答 2

我正在安装,pyfuzzy但不在PyPI中;它返回消息:No matching distribution found for pyfuzzy

我尝试了接受的答案

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

但它也不起作用,并返回以下错误:

忽略索引:https : //pypi.python.org/simple收集pyfuzzy找不到满足pyfuzzy要求的版本(来自版本:)找不到与pyfuzzy匹配的发行版

最后,我找到了一个简单的好方法: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

所以以下命令对我有用:

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

希望它能对您有所帮助。

I am installing pyfuzzybut is is not in PyPI; it returns the message: 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

At last , I have found a simple good way there: 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.


回答 3

这是我最终使用的解决方案:

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")

我从pip安装示例以及Rikard另一个问题的回答中总结了这一点。“ –pre”参数使您可以安装非生产版本。“ –no-index”参数避免搜索PyPI索引。“ –find-links =”。参数在本地文件夹中搜索(可以是相对的也可以是绝对的)。我使用了“ –log-file”,“ log.txt”和“ -vv”参数进行调试。“ –upgrade”参数使您可以在较旧的版本上安装较新的版本。

我还找到了卸载它们的好方法。当您有多个不同的Python环境时,这很有用。这是相同的基本格式,只是使用“卸载”而不是“安装”,并采取了安全措施来防止意外卸载:

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")

本地文件夹包含以下文件:install.py,uninstall.py,mypackagename-1.0.zip

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. The “–pre” argument lets you install non-production versions. The “–no-index” argument avoids searching the PyPI indexes. The “–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. The “–upgrade” argument lets you install newer versions over older ones.

I also found a good way to uninstall them. This is useful when you have several different Python environments. 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


回答 4

一个–find-links选项可以完成这项工作,并且可以通过requirements.txt文件运行!

您可以将软件包归档文件放在某个文件夹中,并在不更改需求文件的情况下使用最新的归档文件,例如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

现在requirements/base.txt放入:

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

一种更新专有软件包的好方法,只需将新软件包放入文件夹中

这样,您可以通过相同的一次调用从local folderAND 安装软件包pypipip install -r requirements/production.txt

PS。请参阅我的cookiecutter-djangopackage分支,以了解如何拆分需求并使用基于文件夹的需求组织。

An option –find-links does the job and it works from requirements.txt file!

You can put package archives in some folder and take the latest one without changing the requirements file, for example 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:

--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

PS. See my cookiecutter-djangopackage fork to see how to split requirements and use folder based requirements organization.


回答 5

安装程序包页面,您可以简单地运行:

点安装/ srv / pkg / mypackage

其中/ srv / pkg / mypackage是目录,包含setup.py


另外1,您可以从存档文件中安装它:

点安装./mypackage-1.0.4.tar.gz

1 尽管在问题中指出,但由于其受欢迎程度,它也包括在内。

From the installing-packages page you can simply run:

pip install /srv/pkg/mypackage

where /srv/pkg/mypackage is the directory, containing setup.py.


Additionally1, you can install it from the archive file:

pip install ./mypackage-1.0.4.tar.gz

1 Although noted in the question, due to its popularity, it is also included.


回答 6

假设您有virtualenv和一个requirements.txt文件,则可以在此文件中定义获取软件包的位置:

# Published pypi packages 
PyJWT==1.6.4
email_validator==1.0.3
# Remote GIT repo package, this will install as django-bootstrap-themes
git+https://github.com/marquicus/django-bootstrap-themes#egg=django-bootstrap-themes
# Local GIT repo package, this will install as django-knowledge
git+file:///soft/SANDBOX/python/django/forks/django-knowledge#egg=django-knowledge

Assuming you have virtualenv and a requirements.txt file, then you can define inside this file where to get the packages:

# Published pypi packages 
PyJWT==1.6.4
email_validator==1.0.3
# Remote GIT repo package, this will install as django-bootstrap-themes
git+https://github.com/marquicus/django-bootstrap-themes#egg=django-bootstrap-themes
# Local GIT repo package, this will install as django-knowledge
git+file:///soft/SANDBOX/python/django/forks/django-knowledge#egg=django-knowledge

回答 7

其要求requirements.txt,并egg_dir作为目录

您可以构建本地缓存:

$ pip download -r requirements.txt -d eggs_dir

然后,使用该“缓存”非常简单,例如:

$ pip install -r requirements.txt --find-links=eggs_dir

Having requirements in requirements.txt and egg_dir as a directory

you can build your local cache:

$ pip download -r requirements.txt -d eggs_dir

then, using that “cache” is simple like:

$ pip install -r requirements.txt --find-links=eggs_dir


回答 8

要仅从本地安装,您需要2个选项:

  • --find-links:在哪里寻找依赖项。不需要file://其他人提到的前缀。
  • --no-index:不要在pypi索引中查找缺少的依赖项(未安装依赖项,也不在--find-links路径中)。

因此,您可以从任何文件夹运行以下文件:

pip install --no-index --find-links /srv/pkg /path/to/mypackage-0.1.0.tar.gz

如果您的mypackage设置正确,它将列出其所有依赖关系,并且如果您使用pip download下载了一系列依赖关系(即,依赖关系等的依赖关系),那么一切都会正常。

如果您想使用pypi索引(如果可以访问),但是如果不使用本地索引,则可以删除--no-index并添加--retries 0。在尝试检查pypi是否缺少依赖项(未安装依赖项)时,您会看到pip暂停一会儿,当它发现无法达到它时,将回落到本地。似乎没有办法告诉pip“先查找本地索引,然后查找索引”。

To install only from local you need 2 options:

  • --find-links: where to look for dependencies. There is no need for the file:// prefix mentioned by others.
  • --no-index: do not look in pypi indexes for missing dependencies (dependencies not installed and not in the --find-links path).

So you could run from any folder the following:

pip install --no-index --find-links /srv/pkg /path/to/mypackage-0.1.0.tar.gz

If your mypackage is setup properly, it will list all its dependencies, and if you used pip download to download the cascade of dependencies (ie dependencies of depencies etc), everything will work.

If you want to use the pypi index if it is accessible, but fallback to local wheels if not, you can remove --no-index and add --retries 0. You will see pip pause for a bit while it is try to check pypi for a missing dependency (one not installed) and when it finds it cannot reach it, will fall back to local. There does not seem to be a way to tell pip to “look for local ones first, then the index”.


回答 9

我一直在尝试实现一些非常简单且失败的尝试,也许我很愚蠢。

无论如何,如果您有一个script / Dockerfile可以下载python软件包zip文件(例如从GitHub),然后要安装它,则可以使用file:///前缀来安装它,如以下示例所示:

$ wget https://example.com/mypackage.zip
$ echo "${MYPACKAGE_MD5}  mypackage.zip" | md5sum --check -
$ pip install file:///.mypackage.zip

注意:我知道您可以使用来立即安装软件包,pip install https://example.com/mypackage.zip但就我而言,我想验证校验和(永远不要偏执),并且在尝试使用pip提供的各种选项/ #md5片段时,我惨败。

直接使用进行如此简单的操作令人感到沮丧pip。我只是想通过一个校验和pip,并安装之前验证该zip匹配。

我可能做的很愚蠢,但最终我放弃了,选择了这样做。我希望它可以帮助其他尝试做类似事情的人。

I’ve been trying to achieve something really simple and failed miserably, probably I’m stupid.

Anyway, if you have a script/Dockerfile which download a python package zip file (e.g. from GitHub) and you then want to install it you can use the file:/// prefix to install it as shown in the following example:

$ wget https://example.com/mypackage.zip
$ echo "${MYPACKAGE_MD5}  mypackage.zip" | md5sum --check -
$ pip install file:///.mypackage.zip

NOTE: I know you could install the package straight away using pip install https://example.com/mypackage.zip but in my case I wanted to verify the checksum (never paranoid enough) and I failed miserably when trying to use the various options that pip provides/the #md5 fragment.

It’s been surprisingly frustrating to do something so simple directly with pip. I just wanted to pass a checksum and have pip verify that the zip was matching before installing it.

I was probably doing something very stupid but in the end I gave up and opted for this. I hope it helps others trying to do something similar.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。