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


83

我是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

我在Google上搜索并找到了此链接,http://stackoverflow.com/questions/28167987/python-pip-trouble-installing-from-requirements-txt但我不太了解该帖子中的解决方案。

以下是我的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

有没有简单的方法来在此python项目中安装所有必需的依赖项?

编辑1

以下是的输出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/

我已经安装了,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.

23
pip install -r requirements.txt
MrLeeh

1
我已经更新了输出,以包括带有-r标志的输出,但仍然无法安装。
Joey Yi Zhao

因此,现在阅读新的输出并思考-不要等我们。
furas

在输出中,您看到它需要freetype。它不是python模块,而是系统软件包。您必须使用ie进行安装。apt-getUbuntu/Mint
furas

为什么-r因为与它无关file
索伦

Answers:


61

如果您使用的是Linux OS:

  1. matplotlib==1.3.1从中删除requirements.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

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

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

9
Nilesh,您好,欢迎来到Stack Overflow。将来,请说明您在答案中给出的命令的作用,而不仅仅是告诉人们运行命令。
yakatz

谢谢@Nilesh。您是说将4.放在3.之前吗?
阿努帕姆

否@Anupam,与在步骤1中一样,我们从requirements.txt中删除了“ matplotlib”,在步骤4中,我们正在使用新安装的软件包更新requirements.txt以供将来使用。step3用于从文件安装其他要求。
Nilesh Pansuriya

40

pip install -r requirements.txt 对于 python 2.x

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


我都尝试了两次,但还是失败了。我已经发布了此命令的输出。
Joey Yi Zhao

1
我认为安装MATPLOTLIB缺少必需的依赖项“ freetype”。尝试安装依赖项,然后再次运行pip install -r requirements.txt。
Joshi Sravan Kumar

pip将不处理系统级别的依赖关系。您必须apt-get install libfreetype6-dev先继续。(它甚至在您的输出中说的很正确。下次尝试对此类错误进行浏览,通常生成的输出会非常详细)
FMaz


3

(摘自我的评论)

pip将不处理系统级别的依赖关系。您必须apt-get install libfreetype6-dev先继续。(它甚至在您的输出中说的很正确。下次尝试对它进行浏览以防此类错误,通常构建输出非常详细)


我已经安装了libfreetype6-dev。但是pip仍然报告此错误。
Joey Yi Zhao

您看到这个错误了吗?github.com/matplotlib/matplotlib/issues/3029
FMaz

3

Python 3:

pip3 install -r requirements.txt

Python 2:

pip install -r requirements.txt

要获取虚拟环境或整个系统的所有依赖关系,请执行以下操作:

pip freeze

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

pip freeze > requirements.txt
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.