关于python:在ubuntu 12.04中编译statsmodels示例

compiling statsmodels example in ubuntu 12.04

我对python不太熟悉,在阅读了一些运行示例之后,我想尝试使用statsmodels。

我从StatsModels网站复制了以下示例,

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python3
import numpy as np
import statsmodels.api as sm
spector_data = sm.datasets.spector.load()
spector_data.exog = sm.add_constant(spector_data.exog, prepend=False)

#Fit and summarize OLS model
mod = sm.OLS(spector_data.endog, spector_data.exog)
res = mod.fit()
print (res.summary())

我得到以下错误:Scipy的运行单元测试

1
2
3
4
5
6
7
8
9
10
11
12
Traceback (most recent call last):
  File"./statsmodels.py", line 3, in <module>
    import statsmodels.api as sm
  File"/home/ra/python/statsmodels.py", line 3, in <module>
    scipy.test()
  File"/usr/lib/python3/dist-packages/numpy/testing/nosetester.py", line 318, in test
    self._show_system_info()
  File"/usr/lib/python3/dist-packages/numpy/testing/nosetester.py", line 187, in _show_system_info
    nose = import_nose()
  File"/usr/lib/python3/dist-packages/numpy/testing/nosetester.py", line 69, in import_nose
    raise ImportError(msg)
ImportError: Need nose >= 0.10.0 for tests - see http://somethingaboutorange.com/mrl/projects/nose

因此,我运行了statsmodels的以下命令,它在运行安装程序时给出了以下消息:

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
sudo easy_install3 -U statsmodels
Searching for statsmodels
Reading http://pypi.python.org/simple/statsmodels/
Reading http://statsmodels.sourceforge.net/
Reading https://github.com/statsmodels/statsmodels/downloads
Best match: statsmodels 0.5.0
Downloading https://pypi.python.org/packages/source/s/statsmodels/statsmodels-0.5.0.zip#md5=4ed78e8c6ababdcae0400fc0fe6f31a7
Processing statsmodels-0.5.0.zip
Running statsmodels-0.5.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-hnrc4z/statsmodels-0.5.0/egg-dist-tmp-2_kign
Traceback (most recent call last):
  File"setup.py", line 116, in check_dependency_versions
ImportError: No module named pandas.version

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File"/usr/bin/easy_install3", line 9, in <module>
    load_entry_point('distribute==0.6.24dev-r0', 'console_scripts', 'easy_install')()
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1931, in main
    with_ei_usage(lambda:
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1912, in with_ei_usage
    return f()
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1935, in <lambda>
    distclass=DistributionWithoutHelpCommands, **kw
  File"/usr/lib/python3.2/distutils/core.py", line 148, in setup
    dist.run_commands()
  File"/usr/lib/python3.2/distutils/dist.py", line 917, in run_commands
    self.run_command(cmd)
  File"/usr/lib/python3.2/distutils/dist.py", line 936, in run_command
    cmd_obj.run()
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 368, in run
    self.easy_install(spec, not self.no_deps)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 608, in easy_install
    return self.install_item(spec, dist.location, tmpdir, deps)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 638, in install_item
    dists = self.install_eggs(spec, download, tmpdir)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 828, in install_eggs
    return self.build_and_install(setup_script, setup_base)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1105, in build_and_install
    self.run_setup(setup_script, setup_base, args)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1094, in run_setup
    run_setup(setup_script, args)
  File"/usr/lib/python3/dist-packages/setuptools/sandbox.py", line 31, in run_setup
    lambda: exec(compile(open(
  File"/usr/lib/python3/dist-packages/setuptools/sandbox.py", line 73, in run
    return func()
  File"/usr/lib/python3/dist-packages/setuptools/sandbox.py", line 34, in <lambda>
    {'__file__':setup_script, '__name__':'__main__'})
  File"setup.py", line 463, in <module>
  File"setup.py", line 118, in check_dependency_versions
ImportError: statsmodels requires pandas

因此,我运行下面的程序以确保我有最新的大熊猫(我以前运行过sudo apt-get install python-pandas,但是由于statsmodels没有发现这一点,我执行了以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo easy_install3 -U pandas
Searching for pandas
Reading http://pypi.python.org/simple/pandas/
Best match: pandas 0.14.0
Downloading https://pypi.python.org/packages/source/p/pandas/pandas-0.14.0.zip#md5=9afe57bd470a2ddaf67bdfb5fc9c0eee
Processing pandas-0.14.0.zip
Running pandas-0.14.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-deg0nw/pandas-0.14.0/egg-dist-tmp-19hrp_
warning: no files found matching 'README.rst'
no previously-included directories found matching 'doc/build'
warning: no previously-included files matching '*.so' found anywhere in distribution
warning: no previously-included files matching '*.pyd' found anywhere in distribution
warning: no previously-included files matching '*.pyc' found anywhere in distribution
warning: no previously-included files matching '.git*' found anywhere in distribution
warning: no previously-included files matching '.DS_Store' found anywhere in distribution
warning: no previously-included files matching '*.png' found anywhere in distribution
pandas/index.c:4:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: Setup script exited with error: command 'gcc' failed with exit status 1

这次我的错误在哪里?

顺便说一句,这是我在python提示符中看到的pandas版本:

1
2
3
4
5
>>> import pandas
/usr/local/lib/python2.7/dist-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0.
  .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver))
>>> pandas.__version__
'0.14.0'

我不明白为什么我在导入时收到这个消息。

另外,我想发布EDOCX1[1]的结果,以防有任何用处:

1
2
3
4
5
6
ra@ra-VGN-FZ:~$ which python
/usr/bin/python
ra@ra-VGN-FZ:~$ which easy_install
/usr/bin/easy_install
ra@ra-VGN-FZ:~$ which pip3
/usr/local/bin/pip3

为了解决OpenPYXL问题,我执行了以下操作:

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
pip uninstall openpyxl
Cannot uninstall requirement openpyxl, not installed
Storing debug log for failure in /tmp/tmpci3yro
ra@ra-VGN-FZ19:~$ pip install openpyxl==1.8.6
Downloading/unpacking openpyxl==1.8.6
  Downloading openpyxl-1.8.6-py2.py3-none-any.whl (190kB): 190kB downloaded
Installing collected packages: openpyxl
Cleaning up...
Exception:
Traceback (most recent call last):
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/commands/install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/req.py", line 671, in install
    self.move_wheel_files(self.source_dir, root=root)
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/req.py", line 901, in move_wheel_files
    pycompile=self.pycompile,
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/wheel.py", line 215, in move_wheel_files
    clobber(source, lib_dir, True)
  File"/usr/local/lib/python3.2/dist-packages/pip-1.5.6-py3.2.egg/pip/wheel.py", line 205, in clobber
    os.makedirs(destdir)
  File"/usr/lib/python3.2/os.py", line 152, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python3.2/dist-packages/openpyxl-1.8.6.dist-info'

Storing debug log for failure in /tmp/tmp15h3o4

这是python3的错误:

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
sudo easy_install3 -U statsmodels
Searching for statsmodels
Reading http://pypi.python.org/simple/statsmodels/
Reading http://statsmodels.sourceforge.net/
Reading https://github.com/statsmodels/statsmodels/downloads
Best match: statsmodels 0.5.0
Downloading https://pypi.python.org/packages/source/s/statsmodels/statsmodels-0.5.0.zip#md5=4ed78e8c6ababdcae0400fc0fe6f31a7
Processing statsmodels-0.5.0.zip
Running statsmodels-0.5.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-oudla7/statsmodels-0.5.0/egg-dist-tmp-o6dh5a
Traceback (most recent call last):
  File"setup.py", line 120, in check_dependency_versions
ImportError: No module named patsy

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File"/usr/bin/easy_install3", line 9, in <module>
    load_entry_point('distribute==0.6.24dev-r0', 'console_scripts', 'easy_install')()
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1931, in main
    with_ei_usage(lambda:
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1912, in with_ei_usage
    return f()
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1935, in <lambda>
    distclass=DistributionWithoutHelpCommands, **kw
  File"/usr/lib/python3.2/distutils/core.py", line 148, in setup
    dist.run_commands()
  File"/usr/lib/python3.2/distutils/dist.py", line 917, in run_commands
    self.run_command(cmd)
  File"/usr/lib/python3.2/distutils/dist.py", line 936, in run_command
    cmd_obj.run()
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 368, in run
    self.easy_install(spec, not self.no_deps)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 608, in easy_install
    return self.install_item(spec, dist.location, tmpdir, deps)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 638, in install_item
    dists = self.install_eggs(spec, download, tmpdir)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 828, in install_eggs
    return self.build_and_install(setup_script, setup_base)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1105, in build_and_install
    self.run_setup(setup_script, setup_base, args)
  File"/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 1094, in run_setup
    run_setup(setup_script, args)
  File"/usr/lib/python3/dist-packages/setuptools/sandbox.py", line 31, in run_setup
    lambda: exec(compile(open(
  File"/usr/lib/python3/dist-packages/setuptools/sandbox.py", line 73, in run
    return func()
  File"/usr/lib/python3/dist-packages/setuptools/sandbox.py", line 34, in <lambda>
    {'__file__':setup_script, '__name__':'__main__'})
  File"setup.py", line 463, in <module>
  File"setup.py", line 122, in check_dependency_versions
ImportError: statsmodels requires patsy. http://patsy.readthedocs.org
ra@ra-VGN-FZ19:~$ sudo easy_install3 -U patsy
Searching for patsy
Reading http://pypi.python.org/simple/patsy/
Best match: patsy 0.2.1
Downloading https://pypi.python.org/packages/source/p/patsy/patsy-0.2.1.zip#md5=d51fa2fcb60018d75073f375c87ddccf
Processing patsy-0.2.1.zip
Running patsy-0.2.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-l1wpxc/patsy-0.2.1/egg-dist-tmp-2qgd7e
no previously-included directories found matching 'doc/_build'
zip_safe flag not set; analyzing archive contents...
Adding patsy 0.2.1 to easy-install.pth file

Installed /usr/local/lib/python3.2/dist-packages/patsy-0.2.1-py3.2.egg
Processing dependencies for patsy
Finished processing dependencies for patsy

感谢Jonathan Villemaire Krajden在我寻求Matplotlib()的解决方案时误导了我,我的statsmodels坏了。我尝试重新安装StatsModels,现在出现以下错误:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
easy_install3 -U statsmodels
Traceback (most recent call last):
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 449, in _build_master
    ws.require(__requires__)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 742, in require
    needed = self.resolve(parse_requirements(requirements))
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 643, in resolve
    raise VersionConflict(dist, req) # XXX put more info here
pkg_resources.VersionConflict: (distribute 0.7.3 (/usr/local/lib/python3.2/dist-packages/distribute-0.7.3-py3.2.egg), Requirement.parse('distribute==0.6.24dev-r0'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File"/usr/bin/easy_install3", line 5, in <module>
    from pkg_resources import load_entry_point
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 2837, in <module>
    working_set = WorkingSet._build_master()
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 451, in _build_master
    return cls._build_from_requirements(__requires__)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 464, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 799, in __init__
    self.scan(search_path)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 829, in scan
    self.add(dist)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 849, in add
    dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
TypeError: unorderable types: NoneType() < str()
ra@ra-VGN-FZ19:~$ sudo easy_install3 -U statsmodels
Traceback (most recent call last):
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 449, in _build_master
    ws.require(__requires__)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 742, in require
    needed = self.resolve(parse_requirements(requirements))
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 643, in resolve
    raise VersionConflict(dist, req) # XXX put more info here
pkg_resources.VersionConflict: (distribute 0.7.3 (/usr/local/lib/python3.2/dist-packages/distribute-0.7.3-py3.2.egg), Requirement.parse('distribute==0.6.24dev-r0'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File"/usr/bin/easy_install3", line 5, in <module>
    from pkg_resources import load_entry_point
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 2837, in <module>
    working_set = WorkingSet._build_master()
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 451, in _build_master
    return cls._build_from_requirements(__requires__)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 464, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 799, in __init__
    self.scan(search_path)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 829, in scan
    self.add(dist)
  File"/usr/local/lib/python3.2/dist-packages/setuptools-5.1-py3.2.egg/pkg_resources.py", line 849, in add
    dists.sort(key=operator.attrgetter('hashcmp'), reverse=True)
TypeError: unorderable types: NoneType() < str()

sudo pip3 install statsmodels
Downloading/unpacking statsmodels
  Downloading statsmodels-0.5.0.tar.gz (5.5MB): 5.5MB downloaded
  Running setup.py (path:/tmp/pip_build_root/statsmodels/setup.py) egg_info for package statsmodels
    Traceback (most recent call last):
      File"<string>", line 17, in <module>
      File"/tmp/pip_build_root/statsmodels/setup.py", line 463, in <module>
        check_dependency_versions(min_versions)
      File"/tmp/pip_build_root/statsmodels/setup.py", line 109, in check_dependency_versions
        from scipy.version import short_version as spversion
      File"/usr/lib/python3/dist-packages/scipy/__init__.py", line 124, in <module>
        pkgload(verbose=SCIPY_IMPORT_VERBOSE,postpone=True)
      File"/usr/local/lib/python3.2/dist-packages/numpy/_import_tools.py", line 177, in __call__
        for package_name in self._get_sorted_names():
      File"/usr/local/lib/python3.2/dist-packages/numpy/_import_tools.py", line 114, in _get_sorted_names
        for name in depend_dict.keys():
    RuntimeError: dictionary changed size during iteration
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File"<string>", line 17, in <module>

  File"/tmp/pip_build_root/statsmodels/setup.py", line 463, in <module>

    check_dependency_versions(min_versions)

  File"/tmp/pip_build_root/statsmodels/setup.py", line 109, in check_dependency_versions

    from scipy.version import short_version as spversion

  File"/usr/lib/python3/dist-packages/scipy/__init__.py", line 124, in <module>

    pkgload(verbose=SCIPY_IMPORT_VERBOSE,postpone=True)

  File"/usr/local/lib/python3.2/dist-packages/numpy/_import_tools.py", line 177, in __call__

    for package_name in self._get_sorted_names():

  File"/usr/local/lib/python3.2/dist-packages/numpy/_import_tools.py", line 114, in _get_sorted_names

    for name in depend_dict.keys():

RuntimeError: dictionary changed size during iteration

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/statsmodels
Storing debug log for failure in /home/ra/.pip/pip.log


使用python 3运行statsmodels.py,几乎可以肯定使用python 2运行ipython。

当使用pip安装时,它使用spython 2,并将包安装在与python 3看起来不同的位置。

使用pip3安装机头(如果已安装,请阅读此处)

1
sudo pip3 install nose

或者用python 2运行statsmodels.py(在第一行用python替换python3):

1
#!/usr/bin/env python