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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15031694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:08:57  来源:igfitidea点击:

Installing Python packages from local file system folder to virtualenv with pip

pythonpip

提问by chadgh

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

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

I have run python setup.py sdistfor 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.

我已经运行python setup.py sdist了我的包,它创建了适当的 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?

这可能吗?

PSI 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/pkglocation as another place for pip to search if I typed pip install mypackage.

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

采纳答案by Mikko Ohtamaa

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

我很确定您正在寻找的是所谓的--find-links选项。

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

尽管您可能需要index.html为您的本地包索引生成一个虚拟文件,其中列出了所有包的链接。该工具有助于:

https://github.com/wolever/pip2pi

https://github.com/wolever/pip2pi

回答by drpoo

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”的顶级目录。

回答by Devan Williams

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 examplesas well as from Rikard's answeron 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.

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

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:

我还找到了卸载它们的好方法。当您有多个不同的 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")

The local folder contains these files: install.py, uninstall.py, mypackagename-1.0.zip

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

回答by Victor Choy

I am installing pyfuzzybut is is not in PyPI; it returns the message: No matching distribution found for pyfuzzy.

我正在安装pyfuzzy但不在 PyPI 中;它返回消息: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/simpleCollecting 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.

希望它可以帮助你。

回答by Janusz Skonieczny

An option --find-linksdoes the job and it works from requirements.txtfile!

一个选项--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.txtput:

现在输入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 folderAND pypiwith the same single call: pip install -r requirements/production.txt

通过这种方式,您可以使用相同的单个调用从local folderAND安装软件包pypipip install -r requirements/production.txt

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

附注。请参阅我的 cookiecutter-djangopackagefork 以了解如何拆分需求并使用基于文件夹的需求组织。

回答by S?awomir Lenart

Having requirements in requirements.txtand egg_diras a directory

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

you can build your local cache:

您可以构建本地缓存:

$ pip download -r requirements.txt -d eggs_dir

$ pip download -r requirements.txt -d eggs_dir

then, using that "cache" is simple like:

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

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

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

回答by marquicus

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

假设你有 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

回答by Aldo 'xoen' Giambelluca

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

我一直在努力实现一些非常简单的事情,但惨遭失败,可能我很愚蠢。

Anyway, if you have a script/Dockerfilewhich 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:

无论如何,如果你有一个脚本/Dockerfile它下载了一个 python 包 zip 文件(例如从 GitHub),然后你想安装它,你可以使用file:///前缀来安装它,如下所示:

$ 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.zipbut 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 #md5fragment.

注意:我知道您可以立即使用安装包,pip install https://example.com/mypackage.zip但在我的情况下,我想验证校验和(从来没有足够的偏执),并且在尝试使用 pip 提供的各种选项/#md5片段时我失败了。

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

直接使用pip. 我只是想通过校验和并安装之前pip验证 zip 是否匹配。

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.

我可能正在做一些非常愚蠢的事情,但最终我放弃并选择了这个。我希望它可以帮助其他人尝试做类似的事情。

回答by dim8

From the installing-packages pageyou can simply run:

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

pip install /srv/pkg/mypackage

pip 安装 /srv/pkg/mypackage

where /srv/pkg/mypackageis the directory, containing setup.py.

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



Additionally1, you can install it from the archive file:

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

pip install ./mypackage-1.0.4.tar.gz

pip install ./mypackage-1.0.4.tar.gz

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

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

回答by Oliver

To install only from local you need 2 options:

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

  • --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-linkspath).
  • --find-links: 在哪里寻找依赖项。不需要file://其他人提到的前缀。
  • --no-index:不要在 pypi 索引中查找缺少的依赖项(依赖项未安装且不在--find-links路径中)。

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.

如果你的 mypackage 设置正确,它会列出它的所有依赖项,如果你使用 pip download 下载级联依赖项(即依赖项的依赖项等),一切都会正常进行。

If you want to use the pypi index if it is accessible, but fallback to local wheels if not, you can remove --no-indexand 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".

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