简体   繁体   English

使用 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.我已经为我的 package 运行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的另一个地方。

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

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

You can do你可以做

pip install mypackage --no-index --find-links file:///srv/pkg/mypackage

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.希望它可以帮助你。

From the installing-packages page you can simply run:安装包页面,您可以简单地运行:

pip install /srv/pkg/mypackage pip 安装 /srv/pkg/mypackage

where /srv/pkg/mypackage is the directory, containing setup.py .其中/srv/pkg/mypackage是包含setup.py的目录。


Additionally 1 , 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

1 Although noted in the question, due to its popularity, it is also included. 1尽管在问题中有所指出,但由于其受欢迎程度,它也包括在内。

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 install 示例以及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

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.附注。 See my cookiecutter-djangopackage fork to see how to split requirements and use folder based requirements organization.请参阅我的 cookiecutter-djangopackage fork 以了解如何拆分需求并使用基于文件夹的需求组织。

Assuming you have virtualenv and a requirements.txt file, 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

Having requirements in requirements.txt and egg_dir as a directoryrequirements.txtegg_dir有需求作为目录

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

To install only from local you need 2 options:要仅从本地安装,您需要 2 个选项:

  • --find-links : where to look for dependencies. --find-links :在哪里寻找依赖项。 There is no need for the file:// prefix mentioned by others.不需要其他人提到的file://前缀。
  • --no-index : do not look in pypi indexes for missing dependencies (dependencies not installed and not in the --find-links path). --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-index and add --retries 0 .如果您想在可访问的情况下使用 pypi 索引,但如果没有,则回--retries 0本地轮子,您可以删除--no-index并添加--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.当它尝试检查 pypi 是否缺少依赖项(未安装的依赖项)时,您会看到 pip 暂停一段时间,当它发现无法访问它时,将回退到本地。 There does not seem to be a way to tell pip to "look for local ones first, then the index".似乎没有办法告诉 pip “先查找本地的,然后是索引”。

What you need is --find-links of pip install.您需要的是--find-links of pip install。

-f, --find-links If a url or path to an html file, then parse for links to archives. -f, --find-links 如果 url 或 html 文件的路径,则解析指向档案的链接。 If a local path or file:// url that's a directory, then look for archives in the directory listing.如果本地路径或文件:// url 是一个目录,则在目录列表中查找档案。

In my case, after python -m build , tar.gz package (and whl file) are generated in ./dist directory.就我而言,在python -m build之后,在./dist目录中生成 tar.gz package (和whl文件)。

pip install --no-index -f ./dist YOUR_PACKAGE_NAME

Any tar.gz python package in ./dist can be installed by this way. ./dist 中的任何 tar.gz python ./dist都可以通过这种方式安装。

But if your package has dependencies, this command will prompt error.但是如果你的package有依赖,这个命令会提示错误。 To solve this, you can either pip install those deps from official pypi source, then add --no-deps like this要解决这个问题,您可以pip install这些 deps,然后像这样添加--no-deps

pip install --no-index --no-deps -f ./dist YOUR_PACKAGE_NAME

or copy your deps packages to./dist directory.或将您的 deps 包复制到 ./dist 目录。

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 (eg 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.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.注意:我知道您可以使用pip install https://example.com/mypackage.zip立即安装该软件包,但在我的情况下,我想验证校验和(从来没有足够的偏执),并且在尝试使用各种pip 提供的选项/ #md5片段。

It's been surprisingly frustrating to do something so simple directly with pip .直接用pip做一些如此简单的事情令人惊讶地令人沮丧。 I just wanted to pass a checksum and have pip verify that the zip was matching before installing it.我只是想通过校验和并让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.我希望它可以帮助其他人尝试做类似的事情。

pip install packages/*

Works like a charm. 奇迹般有效。

All desired packages are in packages folder populated by: 所有所需的软件包都在“ packages”文件夹中,并由以下文件夹填充:

pip download $(pip freeze)

In my case, it was because this library depended on another local library, which I had not yet installed.就我而言,这是因为该库依赖于我尚未安装的另一个本地库。 Installing the dependency with pip, and then the dependent library, solved the issue.用pip安装依赖,然后安装依赖库,解决了这个问题。

If you want to install one local package (package A) to be used inside another local project/package (B) this is quite simple.如果您想安装一个本地 package(包 A)以在另一个本地项目/包(B)中使用,这非常简单。 All you need is to CD to (B) and call:您只需要 CD 到 (B) 并致电:

pip install /path/to/package(A)

Of course you will need to first compile the package (A) with:当然,您需要先编译 package (A):

sudo python3 ./setup.py install

And, each time you change package A, just run again setup.py in package (A) then pip install... inside the using project/package (B)而且,每次更改 package A 时,只需在 package (A) 中再次运行setup.py ,然后在使用项目/包 (B) pip install...

Just add directory on pip command pip install mypackage file:/location/in/disk/mypackagename.filetype只需在 pip 命令上添加目录pip install mypackage file:/location/in/disk/mypackagename.filetype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM