python - 在 ubuntu 12.04 中编译 statsmodels 示例

标签 python bash ubuntu statsmodels

我是 python 新手,在阅读了运行一些示例后,我想尝试 statsmodels。

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

#!/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 运行单元测试

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 运行了以下命令,它在运行安装时给了我以下消息:

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

所以,我运行了下面的命令以确保我拥有最新的 pandas (我之前运行过 sudo apt-get install python-pandas ,但由于 statsmodels 没有找到它,所以我做了如下:

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 版本:

>>> 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'

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

另外,我想发布 which 的结果,以防有任何用处:

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 问题,我执行了以下操作:

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 的错误:

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 中寻找 show() 的解决方案时,他误导了我,我的统计模型已经损坏。 我尝试重新安装 statsmodels,现在出现以下错误:

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 安装时,它使用 Python 2,并将软件包安装在与 Python 3 不同的位置。

使用pip3安装nose(如果您安装了它 - read here if not)

sudo pip3 install nose

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

#!/usr/bin/env python

关于python - 在 ubuntu 12.04 中编译 statsmodels 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358661/

相关文章:

python - ExcelPython返回类型不匹配问题

python - BS4 网络抓取到 CSV 文件,我认为我抓取了太多行 ('tr' )s

bash - 如何创建 Linux 脚本(bash、sed、awk...)以在配置文件的特定部分添加一行

bash - 使用 awk 和 df(无磁盘)仅显示挂载名称和已用空间

python - 如何按 <CTRL>

Python Scrapy 提取 aria-label 的值

linux - 不同的输入/输出重定向样式之间有什么区别?

ubuntu - 在 ubuntu 中将 .p12 转换为 .pem

qt - 如何在 Qt5 中获取系统 QFileDialog?

linux - IPython 笔记本显示 `ImportError`