关于pip:如何使用requirements.txt在python项目中安装所有依赖项

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

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

1
2
3
4
# 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

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

以下是my requirements.txt文件:

1
2
3
4
5
6
7
8
9
# 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的输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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命令仍然报告缺少这种依赖性。

1
2
3
4
5
6
# 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.


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

  • requirements.txt上取下matplotlib==1.3.1
  • 试着用sudo apt-get install python-matplotlib安装
  • 运行pip install -r requirements.txt(python 2)或pip3 install -r requirements.txt(python 3)
  • pip freeze > requirements.txt
  • 如果您使用的是Windows操作系统:

  • python -m pip install -U pip setuptools
  • python -m pip install matplotlib

  • pip install -r requirements.txt用于python 2.x

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


    1
    python -m pip install -r requirements.txt

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


    (摘自我的评论)

    pip不处理系统级的依赖关系。在继续之前,您必须先执行apt-get install libfreetype6-dev。(它甚至在你的输出中这么说。下一次尝试略读这些错误,通常构建输出非常详细)