简体   繁体   English

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

[英]How can I install packages using pip according to the requirements.txt file from a local directory?

Here is the problem:这是问题所在:

I have a requirements.txt file that looks like:我有一个requirements.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
...

I have a local archive directory containing all the packages + others.我有一个包含所有包和其他包的本地存档目录。

I have created a new virtualenv with我创建了一个新的virtualenv

bin/virtualenv testing

Upon activating it, I tried to install the packages according to requirements.txt from the local archive directory.激活它后,我尝试根据本地存档目录中的requirements.txt安装软件包。

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:我得到一些 output 似乎表明安装没问题:

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 a later check revealed that none of the packages are installed properly.但是后来的检查显示没有一个包被正确安装。 I cannot import the packages, and none are found in the site-packages directory of my virtualenv.我无法导入包,并且在我的 virtualenv 的站点包目录中找不到任何包。 So what went wrong?那么出了什么问题呢?

这对每个人都有效:

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

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). --no-index - 忽略包索引(只查看--find-links URL)。

-f, --find-links <URL> - If a URL or path to an HTML file, then parse for links to archives. -f, --find-links <URL> - 如果是 HTML 文件的 URL 或路径,则解析指向档案的链接。

If a local path or file:// URL that's a directory, then look for archives in the directory listing.如果本地路径或file:// URL 是目录,则在目录列表中查找存档。

For virtualenv to install all files in the requirements.txt file.对于virtualenv 安装requirements.txt文件中的所有文件。

  1. cd to the directory where requirements.txt is located cd到requirements.txt所在目录
  2. activate your virtualenv激活你的虚拟环境
  3. run: pip install -r requirements.txt in your shell在你的 shell 中运行: pip install -r requirements.txt

I had a similar problem.我有一个类似的问题。 I tried this:我试过这个:

    pip install -U -r requirements.txt

(-U = update if it had already installed) (-U = 如果已经安装则更新)

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.不知道这对你有没有帮助。

Use:采用:

pip install -r requirements.txt

For further details, please check the help option:有关更多详细信息,请查看帮助选项:

pip install --help

We can find the option '-r' -我们可以找到选项'-r' -

-r, --requirement Install from the given requirements file. -r, --requirement 从给定的需求文件安装。 This option can be used multiple times.此选项可以多次使用。

Further information on some commonly used pip install options (this is the help option on the pip install command):有关一些常用 pip 安装选项的更多信息(这是 pip install 命令的帮助选项):

在此处输入图片说明

Also the above is the complete set of options.以上也是完整的选项集。 Please use pip install --help for the complete list of options.请使用pip install --help获取完整的选项列表。

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.这里, -r--requirement缩写形式,它要求pip从给定的requirements文件安装。

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. pip只有在检查requirements文件中列出的所有项目的可用性后才会开始安装,即使有一个requirement不可用,它也不会开始安装。

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

Often, you will want a fast install from local archives, without probing PyPI.通常,您会希望从本地存档快速安装,而无需探查 PyPI。

First, download the archives that fulfill your requirements:首先,下载满足您要求的档案:

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

Then, install using –find-links and –no-index :然后,使用–find-links–no-index

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

First of all, create a virtual environment.首先,创建一个虚拟环境。

In Python 3.6在 Python 3.6 中

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

In Python 2.7在 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.然后激活环境并安装requirement.txt文件中的所有可用包。

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

I work with a lot of systems that have been mucked by developers "following directions they found on the Internet".我使用的许多系统都被开发人员“按照他们在 Internet 上找到的指示”搞砸了。 It is extremely common that your pip and your python are not looking at the same paths/site-packages.您的pip和您的python没有查看相同的路径/站点包,这是非常常见的。 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这是不高兴的,因为pip是(python3.6 和)使用/usr/local/lib/python3.6/site-packagespython是(python2.7 和)使用/usr/local/lib/python2.7/site-packages

When I want to make sure I'm installing requirements to the right python, I do this:当我想确保将需求安装到正确的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

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." DevOps 版本是,“如果你没有破坏它并且你可以解决它,不要试图修复它。”

Installing requirements.txt file inside virtual env with Python 3:使用 Python 3 在虚拟环境中安装 requirements.txt 文件:

I had the same issue.我遇到过同样的问题。 I was trying to install the requirements.txt file inside a virtual environment.我试图在虚拟环境中安装requirements.txt文件。 I found the solution.我找到了解决方案。

Initially, I created my virtualenv in this way:最初,我以这种方式创建了我的 virtualenv:

virtualenv -p python3 myenv

Activate the environment using:使用以下方法激活环境:

source myenv/bin/activate

Now I installed the requirements.txt file using:现在我使用以下命令安装了requirements.txt文件:

pip3 install -r requirements.txt

Installation was successful and I was able to import the modules.安装成功,我能够导入模块。

试试这个:

python -m pip install -r requirements.txt
pip install --user -r requirements.txt 

要么

pip3 install --user -r requirements.txt 

In Windows, this can lead to less format-related path issues, if you have在 Windows 中,如果您有

c:\folder\subfolder\requirements.txt c:\文件夹\子文件夹\requirements.txt

cd c:\folder\subfolder 
pip install -r requirements.txt

While pip install -r requirements.txt works most of the time, we sometimes face the problem that our requirements have conflicting dependencies.虽然pip install -r requirements.txt大部分时间都有效,但我们有时会遇到我们的需求具有冲突的依赖关系的问题。

You can install the packages line-by-line with the following one-liner:您可以使用以下单行代码逐行安装软件包:

while read -r line; do echo $line | cut -d' ' -f1 | xargs pip install; done < requirements.txt

Hope it helps someone!希望它能帮助别人!

  • Create virtual environment python3 -m venv virtual-env (For windows use python instead of python3)创建虚拟环境python3 -m venv virtual-env (对于 windows 使用 python 而不是 python3)
  • Activate your virtual environment source virtual-env/bin/activate激活你的虚拟环境source virtual-env/bin/activate
  • Now install requirements pip install -r requirements.txt现在安装需求pip install -r requirements.txt

使用pip3 install -r requirements.txt但确保 requirements.txt 文件已从源中提取并且未添加到.gitignore

pip install -r requirements.txt

On windows Using this在 windows 使用这个

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

On linux pc we have python2 and also python3 so most time pip is regarded as在 linux pc 上我们有 python2 和 python3 所以大部分时间pip被认为是

Python2蟒蛇2

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

Python 3 Python 3

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

I have solved with running the below command:我已经通过运行以下命令解决了:

py -m pip install ./requirements.txt

the above command will install all dependencies and libraries for the Django project.上面的命令将为 Django 项目安装所有依赖项和库。

暂无
暂无

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

相关问题 如何安装 requirements.txt pip 错误-&gt;由于 OSError 无法安装软件包:[Errno 2] 没有这样的文件或目录 - How to install requirements.txt pip error ->Could not install packages due to an OSError: [Errno 2] No such file or directory 可以从requirements.txt递归地pip安装包 - Can pip install packages recursively from requirements.txt 如何安装 github zip 文件与 pip 和 setup.py 来自 requirements.txt? - How can I install a github zip file with pip and setup.py from requirements.txt? 如何使用 `pip install -r requirements.txt` 通过 `requirements.txt` 下载 NLTK 语料库? - How can I download NLTK corpora via `requirements.txt` using `pip install -r requirements.txt`? pip 无法从 requirements.txt 安装包 - pip fails to install packages from requirements.txt 如何在使用 pip 为 requirements.txt 中的其他包构建​​轮子之前安装某些包? - How to install certain packages before building wheels for other packages in requirements.txt using pip? pip install -r requirements.txt [Errno 2] 没有这样的文件或目录:'requirements.txt' - pip install -r requirements.txt [Errno 2] No such file or directory: 'requirements.txt' 使用 pip 命令从 requirements.txt 升级 python 包 - Upgrade python packages from requirements.txt using pip command 当我使用requirements.txt时,pip无法从virtualenv内安装到site-packages目录 - pip not installing to site-packages directory from within virtualenv when I use a requirements.txt 使用 requirements.txt 在新的 conda 环境中自动安装来自 conda 通道和 pip 的包 - using requirements.txt to automatically install packages from conda channels and pip in a new conda environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM