如何使用requirements.txt在一个python项目中安装所有依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41457612/
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-20 00:59:28  来源:igfitidea点击:

How to use requirements.txt to install all dependencies in a python project

pythonpiprequirements.txt

提问by Joey Yi Zhao

I am new to python. Recently I got a project written by python and it requires some installation. I run below command to install but got an error.

我是python的新手。最近我得到了一个用python写的项目,它需要一些安装。我运行下面的命令进行安装,但出现错误。

# pip install requirements.txt 
Collecting requirements.txt
  Could not find a version that satisfies the requirement requirements.txt (from versions: )
No matching distribution found for requirements.txt

I searched on google and found this link http://stackoverflow.com/questions/28167987/python-pip-trouble-installing-from-requirements-txtbut I don't quite understand what the solution in that post.

我在谷歌上搜索并找到了这个链接,http://stackoverflow.com/questions/28167987/python-pip-trouble-installing-from-requirements-txt但我不太明白那篇文章中的解决方案。

Below is my requirements.txt file:

下面是我的requirements.txt文件:

# cat requirements.txt 
ordereddict==1.1
argparse==1.2.1
python-dateutil==2.2
matplotlib==1.3.1
nose==1.3.0
numpy==1.8.0
pymongo==3.3.0
psutil>=2.0

Is there a easy way to install all required dependencies in this python project?

有没有一种简单的方法可以在这个 python 项目中安装所有必需的依赖项?

EDIT1

编辑1

Below is the output from pip3 install -r requirements.txt.

以下是来自pip3 install -r requirements.txt.

# pip3 install -r requirements.txt 
Requirement already satisfied: ordereddict==1.1 in /usr/local/lib/python3.5/dist-packages (from -r requirements.txt (line 1))
Collecting argparse==1.2.1 (from -r requirements.txt (line 2))
  Using cached argparse-1.2.1.tar.gz
Collecting python-dateutil==2.2 (from -r requirements.txt (line 3))
  Using cached python-dateutil-2.2.tar.gz
Collecting matplotlib==1.3.1 (from -r requirements.txt (line 4))
  Using cached matplotlib-1.3.1.tar.gz
    Complete output from command python setup.py egg_info:
    ============================================================================
    Edit setup.cfg to change the build options

    BUILDING MATPLOTLIB
                matplotlib: yes [1.3.1]
                    python: yes [3.5.2 (default, Nov 17 2016, 17:05:23)  [GCC
                            5.4.0 20160609]]
                  platform: yes [linux]

    REQUIRED DEPENDENCIES AND EXTENSIONS
                     numpy: yes [version 1.11.3]
                  dateutil: yes [using dateutil version 2.6.0]
                   tornado: yes [tornado was not found. It is required for the
                            WebAgg backend. pip/easy_install may attempt to
                            install it after matplotlib.]
                 pyparsing: yes [using pyparsing version 2.1.10]
                     pycxx: yes [Official versions of PyCXX are not compatible
                            with Python 3.x.  Using local copy]
                    libagg: yes [pkg-config information for 'libagg' could not
                            be found. Using local copy.]
                  freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                            could not be found.  You may need to install the
                            development package.]
                       png: yes [pkg-config information for 'libpng' could not
                            be found. Using unknown version.]

    OPTIONAL SUBPACKAGES
               sample_data: yes [installing]
                  toolkits: yes [installing]
                     tests: yes [using nose version 1.3.7]

    OPTIONAL BACKEND EXTENSIONS
                    macosx: no  [Mac OS-X only]
                    qt4agg: no  [PyQt4 not found]
                   gtk3agg: no  [gtk3agg backend does not work on Python 3]
                 gtk3cairo: no  [Requires cairo to be installed.]
                    gtkagg: no  [Requires pygtk]
                     tkagg: no  [TKAgg requires Tkinter.]
                     wxagg: no  [requires wxPython]
                       gtk: no  [Requires pygtk]
                       agg: yes [installing]
                     cairo: no  [cairo not found]
                 windowing: no  [Microsoft Windows only]

    OPTIONAL LATEX DEPENDENCIES
                    dvipng: no
               ghostscript: no
                     latex: no
                   pdftops: no

    ============================================================================
                            * The following required packages can not be built:
                            * freetype

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-don4ne_2/matplotlib/

I have already installed libfreetype6-devbut the pip command still reports missing this dependency.

我已经安装了,libfreetype6-dev但 pip 命令仍然报告缺少此依赖项。

# apt-get install libfreetype6-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libfreetype6-dev is already the newest version (2.6.1-0.1ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.

采纳答案by Nilesh Pansuriya

If you are using Linux OS:

如果您使用的是 Linux 操作系统:

  1. Remove matplotlib==1.3.1from requirements.txt
  2. Try to install with sudo apt-get install python-matplotlib
  3. Run pip install -r requirements.txt(Python 2), or pip3 install -r requirements.txt(Python 3)
  4. pip freeze > requirements.txt
  1. 删除matplotlib==1.3.1requirements.txt
  2. 尝试安装 sudo apt-get install python-matplotlib
  3. 运行pip install -r requirements.txt(Python 2) 或pip3 install -r requirements.txt(Python 3)
  4. pip freeze > requirements.txt

If you are using Windows OS:

如果您使用的是 Windows 操作系统:

  1. python -m pip install -U pip setuptools
  2. python -m pip install matplotlib
  1. python -m pip install -U pip setuptools
  2. python -m pip install matplotlib

回答by Krishna Kishore Andhavarapu

pip install -r requirements.txtfor python 2.x

pip install -r requirements.txt为了 python 2.x

pip3 install -r requirements.txtfor python 3.x(in case multiple versions are installed)

pip3 install -r requirements.txtfor python 3.x(如果安装了多个版本)

回答by tri.akki7

Python 3:

蟒蛇3:

pip3 install -r requirements.txt

Python 2:

蟒蛇2:

pip install -r requirements.txt

To get all the dependencies for the virtual environment or for the whole system:

要获取虚拟环境或整个系统的所有依赖项:

pip freeze

To push all the dependencies to the requirements.txt (Linux):

将所有依赖项推送到 requirements.txt (Linux):

pip freeze > requirements.txt

回答by FMaz

(Taken from my comment)

(摘自我的评论)

pipwon't handle system level dependencies. You'll have to apt-get install libfreetype6-devbefore continuing. (It even says so right in your output. Try skimming over it for such errors next time, usually build outputs are very detailed)

pip不会处理系统级依赖项。apt-get install libfreetype6-dev在继续之前,您必须这样做。(它甚至在您的输出中说得很对。下次尝试浏览它以查找此类错误,通常构建输出非常详细)