问题:如何根据本地目录中的requirements.txt文件使用pip安装软件包?

这是问题所在

我有一个require.txt看起来像:

BeautifulSoup==3.2.0
Django==1.3
Fabric==1.2.0
Jinja2==2.5.5
PyYAML==3.09
Pygments==1.4
SQLAlchemy==0.7.1
South==0.7.3
amqplib==0.6.1
anyjson==0.3
...

我有一个本地存档目录,其中包含所有软件包和其他软件包。

我创建了一个新的virtualenv

bin/virtualenv testing

激活它后,我尝试根据本地存档目录中的requirements.txt安装软件包。

source bin/activate
pip install -r /path/to/requirements.txt -f file:///path/to/archive/

我得到一些输出,似乎表明安装正常

Downloading/unpacking Fabric==1.2.0 (from -r ../testing/requirements.txt (line 3))
  Running setup.py egg_info for package Fabric
    warning: no previously-included files matching '*' found under directory 'docs/_build'
    warning: no files found matching 'fabfile.py'
Downloading/unpacking South==0.7.3 (from -r ../testing/requirements.txt (line 8))
  Running setup.py egg_info for package South
....

但后来检查发现该软件包均未正确安装。我无法导入软件包,但在virtualenv的site-packages目录中找不到任何软件包。那么出了什么问题?

Here is the problem

I have a requirements.txt that looks like:

BeautifulSoup==3.2.0
Django==1.3
Fabric==1.2.0
Jinja2==2.5.5
PyYAML==3.09
Pygments==1.4
SQLAlchemy==0.7.1
South==0.7.3
amqplib==0.6.1
anyjson==0.3
...

I have a local archive directory containing all the packages + others.

I have created a new virtualenv with

bin/virtualenv testing

upon activating it, I tried to install the packages according to requirements.txt from the local archive directory.

source bin/activate
pip install -r /path/to/requirements.txt -f file:///path/to/archive/

I got some output that seems to indicate that the installation is fine

Downloading/unpacking Fabric==1.2.0 (from -r ../testing/requirements.txt (line 3))
  Running setup.py egg_info for package Fabric
    warning: no previously-included files matching '*' found under directory 'docs/_build'
    warning: no files found matching 'fabfile.py'
Downloading/unpacking South==0.7.3 (from -r ../testing/requirements.txt (line 8))
  Running setup.py egg_info for package South
....

But later check revealed none of the package is installed properly. I cannot import the package, and none is found in the site-packages directory of my virtualenv. So what went wrong?


回答 0

这对我有用:

$ pip install -r requirements.txt --no-index --find-links file:///tmp/packages

--no-index-忽略软件包索引(仅查看--find-linksURL)。

-f, --find-links <URL>-如果是URL或html文件的路径,请解析出指向归档文件的链接。如果是file://目录的本地路径或URL,请在目录列表中查找档案。

This works for me:

$ pip install -r requirements.txt --no-index --find-links file:///tmp/packages

--no-index – Ignore package index (only looking at --find-links URLs instead).

-f, --find-links <URL> – If a URL or path to an html file, then parse for links to archives. If a local path or file:// URL that’s a directory, then look for archives in the directory listing.


回答 1

我已经阅读了上面的内容,意识到这是一个古老的问题,但它仍未完全解决,仍然位于我的Google搜索结果的顶部,因此,这是一个适用于所有人的答案:

pip install -r /path/to/requirements.txt

I’ve read the above, realize this is an old question, but it’s totally unresolved and still at the top of my google search results so here’s an answer that works for everyone:

pip install -r /path/to/requirements.txt

回答 2

为了使virtualenv将所有文件安装在requirements.txt文件中。

  1. cd到requirements.txt所在的目录
  2. 激活您的虚拟环境
  3. 运行: pip install -r requirements.txt 在您的外壳中

For virtualenv to install all files in the requirements.txt file.

  1. cd to the directory where requirements.txt is located
  2. activate your virtualenv
  3. run: pip install -r requirements.txt in your shell

回答 3

我有一个类似的问题。我尝试了这个:

pip install -U -r requirements.txt 

(-U =更新(如果已安装))

但是问题仍然存在。我意识到缺少一些通用的开发库。

sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

我不知道这是否对您有帮助。

I had a similar problem. I tried this:

pip install -U -r requirements.txt 

(-U = update if it had already installed)

But the problem continued. I realized that some of generic libraries for development were missed.

sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

I don’t know if this would help you.


回答 4

pip install -r requirements.txt

有关更多详细信息,请检查帮助选项。

pip install --help

我们可以找到选项“ -r”

-r,–requirement从给定的需求文件安装。此选项可以多次使用。

有关一些常用的pip安装选项的更多信息:(这是pip install命令上的帮助选项)

在此处输入图片说明

以上是完整的选项集。请使用pip install –help获得完整的选项列表。

pip install -r requirements.txt

For further details please check the help option.

pip install --help

We can find the option ‘-r’

-r, –requirement Install from the given requirements file. This option can be used multiple times.

Further information on some commonly used pip install options: (This is the help option on pip install command)

enter image description here

Also the above is the complete set of options. Please use pip install –help for complete list of options.


回答 5

简短答案

pip install -r /path/to/requirements.txt

或其他形式:

python -m pip install -r /path/to/requirements.txt

说明

在这里,-r是的缩写--requirement,它要求pip从给定的requirements文件进行安装。

pip只有在检查了requirements文件中所有列出的项目的可用性之后,它才会开始安装,即使有一个项目也不会开始安装requirement不可用。

安装可用软件包的一种解决方法是逐一安装列出的软件包。为此使用以下命令。将显示红色警告,以通知您有关不可用的软件包的信息。

cat requirements.txt | xargs -n 1 pip install

要忽略注释(以开头的行#)和空白行,请使用:

cat requirements.txt | cut -f1 -d"#" | sed '/^\s*$/d' | xargs -n 1 pip install

Short answer

pip install -r /path/to/requirements.txt

or in another form:

python -m pip install -r /path/to/requirements.txt

Explanation

Here, -r is short form of --requirement and it asks the pip to install from the given requirements file.

pip will start installation only after checking the availability of all listed items in the requirements file and it won’t start installation even if one requirement is unavailable.

One workaround to install the available packages is installing listed packages one by one. Use the following command for that. A red color warning will be shown to notify you about the unavailable packages.

cat requirements.txt | xargs -n 1 pip install

To ignore comments (lines starting with a #) and blank lines, use:

cat requirements.txt | cut -f1 -d"#" | sed '/^\s*$/d' | xargs -n 1 pip install

回答 6

通常,您将需要从本地档案中快速安装,而无需探究PyPI。

首先,下载符合您要求的档案:

$ pip install --download <DIR> -r requirements.txt

然后,使用–find-links和安装–no-index

$ pip install --no-index --find-links=[file://]<DIR> -r requirements.txt

Often, you will want a fast install from local archives, without probing PyPI.

First, download the archives that fulfill your requirements:

$ pip install --download <DIR> -r requirements.txt

Then, install using –find-links and –no-index:

$ pip install --no-index --find-links=[file://]<DIR> -r requirements.txt

回答 7

我使用了很多系统,这些系统被开发人员“遵循他们在互联网上找到的指示”所破坏。您pip和您使用python的路径/站点程序包不同,这是非常普遍的。因此,当我遇到奇怪的事物时,我首先要做的是:

$ python -c 'import sys; print(sys.path)'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']

$ pip --version
pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)

那是一个快乐的系统

下面是一个不愉快的系统。(或者至少是一个幸福无知的系统,导致其他人感到不高兴。)

$ pip --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

$ python -c 'import sys; print(sys.path)'
['', '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']

$ which pip pip2 pip3
/usr/local/bin/pip
/usr/local/bin/pip3

不高兴,因为pip(python3.6和)正在使用/usr/local/lib/python3.6/site-packageswhile python是(python2.7和)正在使用/usr/local/lib/python2.7/site-packages

当我要确保将要求安装到正确的 python时,请执行以下操作:

$ which -a python python2 python3
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python2
/usr/local/bin/python3

$ /usr/bin/python -m pip install -r requirements.txt

您听说过,“如果它没有损坏,请不要尝试对其进行修复。” DevOps的版本是“如果您没有破坏它并且可以解决它,请不要尝试对其进行修复。”

I work with a lot of systems that have been mucked by developers “following directions they found on the internet”. It is extremely common that your pip and your python are not looking at the same paths/site-packages. For this reason, when I encounter oddness I start by doing this:

$ python -c 'import sys; print(sys.path)'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']

$ pip --version
pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)

That is a happy system.

Below is an unhappy system. (Or at least it’s a blissfully ignorant system that causes others to be unhappy.)

$ pip --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

$ python -c 'import sys; print(sys.path)'
['', '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']

$ which pip pip2 pip3
/usr/local/bin/pip
/usr/local/bin/pip3

It is unhappy because pip is (python3.6 and) using /usr/local/lib/python3.6/site-packages while python is (python2.7 and) using /usr/local/lib/python2.7/site-packages

When I want to make sure I’m installing requirements to the right python, I do this:

$ which -a python python2 python3
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python2
/usr/local/bin/python3

$ /usr/bin/python -m pip install -r requirements.txt

You’ve heard, “If it ain’t broke, don’t try to fix it.” The DevOps version of that is, “If you didn’t break it and you can work around it, don’t try to fix it.”


回答 8

首先,创建一个虚拟环境

在python 3.6中

virtualenv --python=/usr/bin/python3.6 <path/to/new/virtualenv/>

在python 2.7中

virtualenv --python=/usr/bin/python2.7 <path/to/new/virtualenv/>

然后激活环境并安装require.txt文件中所有可用的软件包。

source <path/to/new/virtualenv>/bin/activate
pip install -r <path/to/requirement.txt>

first of all, create a virtual environment

in python 3.6

virtualenv --python=/usr/bin/python3.6 <path/to/new/virtualenv/>

in python 2.7

virtualenv --python=/usr/bin/python2.7 <path/to/new/virtualenv/>

then activate the environment and install all the packages available in the requirement.txt file.

source <path/to/new/virtualenv>/bin/activate
pip install -r <path/to/requirement.txt>

回答 9

使用python 3在虚拟环境中安装Requirements.txt文件:

我遇到过同样的问题。我试图在虚拟环境中安装requirements.txt文件。我找到了解决方案。

最初,我以这种方式创建了虚拟环境:

virtualenv -p python3 myenv

使用以下方法激活环境:

source myenv/bin/activate

现在,我使用以下命令安装了requirements.txt:

pip3 install -r requirements.txt

安装成功,我能够导入模块。

Installing requirements.txt file inside virtual env with python 3:

I had the same issue. I was trying to install requirements.txt file inside a virtual environament. I found the solution.

Initially, I created my virtual env in this way:

virtualenv -p python3 myenv

Activate the environment using:

source myenv/bin/activate

Now I installed the requirements.txt using:

pip3 install -r requirements.txt

Installation was successful and I was able to import the modules.


回答 10

尝试这个

python -m pip install -r requirements.txt

try this

python -m pip install -r requirements.txt

回答 11

pip install --user -r requirements.txt 

要么

pip3 install --user -r requirements.txt 
pip install --user -r requirements.txt 

OR

pip3 install --user -r requirements.txt 

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