当前位置: 首页>>技术教程>>正文


如何为python安装numpy和scipy?

, ,

问题描述

我厌倦了尝试为phyton 2.7.5安装numpy和scipy,我需要这个版本的python,因为我的最终目标是安装类(http://class-code.net/)和montepython(http://montepython.net/)。

(我有ubuntu 12.04)

当我尝试导入numpy或scipy时,我每次都会收到以下错误:

$ phyton2.7
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
  import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
  from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
  from type_check import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
  import numpy.core.numeric as _nx
File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
  import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined        symbol: PyUnicodeUCS4_AsUnicodeEscapeString

>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/scipy/__init__.py", line 78, in <module>
  from numpy import show_config as show_numpy_config
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
  import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
  from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
  from type_check import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
  import numpy.core.numeric as _nx
File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
  import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS4_AsUnicodeEscapeString

为了摆脱我曾尝试过的错误:

  1. 满足python要求:$ sudo apt-get install build-essential python-dev gfortran

  2. 使用apt-get安装:$ sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose

  3. 使用pip安装要求:$ sudo pip install requests

  4. …并尝试使用pip进行安装:$ sudo pip install numpy$ sudo pip install scipy

  5. 我已经有环境var:export PYTHONPATH=/usr/lib/python2.7/dist-packages:${PYTHONPATH}我最后一次安装numpy和scipy(在较旧的ubuntu版本10.x中)这对我有用。

  6. sudo sh -c "wget http://python-distribute.org/distribute_setup.py -O- | python" sudo sh -c "wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py -O- | python"然后按照(4)中的pip安装它们

    但现在看来python-distribute.org的服务器已关闭。我收到以下错误:

    http://python-distribute.org/distribute_setup.py
    Resolving python-distribute.org (python-distribute.org)... failed: Name or service not known.
    wget: unable to resolve host address `python-distribute.org'
    
  7. 我还尝试从synaptic和Ubuntu软件中心安装numpy和scipy

我在每次尝试中都失败了,现在我不知道该怎么办。我错过了什么吗?如何为python正确安装numpy和scipy?任何想法将不胜感激。

当我做

echo $PYTHONPATH

我明白了

/usr/lib/python2.7/dist-packages

最佳解决方案

好的,让我们按照installation guide

  1. 它说你需要python 2.7(你已经拥有):

    $ python --version
    Python 2.7.4
    
  2. 然后它说你也需要numpy包,版本> = 1.4.1:

    
    apt-cache policy python-numpy
    python-numpy:
    Installed: (none)
    Candidate: 1:1.7.1-1ubuntu1
    Version table:
     1:1.7.1-1ubuntu1 0
        500 http://archive.ubuntu.com/ubuntu/ raring/main amd64 Packages

    如您所见,我有numpy版本1.7.1,所以让我们继续安装它:

    sudo apt-get install python-numpy
    
  3. 现在它说我们需要cython,让我们检查一下这个包是否可用:

    apt-cache policy cython
    cython:
    Installed: (none)
    Candidate: 0.17.4-0ubuntu1
    Version table:
     0.17.4-0ubuntu1 0
        500 http://archive.ubuntu.com/ubuntu/ raring/main amd64 Packages

    我们有它,我们安装它:

    sudo apt-get install cython
    

    请注意,还有其他正在安装的依赖包。

  4. 奇怪的是,我们也需要scipy模块:

    sudo apt-get install python-scipy
    
  5. 测试。在终端中打开python并输入以下内容:

    $ python
    Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
    [GCC 4.7.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy
    >>> import scipy
    >>> import cython
    >>> exit()
    

    以上,必须没有错误。如果出现问题,请再次阅读指南,忘记/滑了一步。

  6. 从页面下载CLASS源代码,转到终端中的目录,然后解压缩并进行以下操作:

    cd where/the/tar/is
    tar zxf class_v2.0.2.tar.gz
    cd class_v2.0.2
    make

    在这里,我们将等到完成。完成后,我们转到python目录并启动魔术。没有从目录移动运行:

    cd python
    python setup.py build
    python setup.py install --user

    现在我们在用户目录中安装了CLASS。我们应该测试一下!

    python
    Python 2.7.4 (default, Sep 26 2013, 03:20:26)
    [GCC 4.7.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from classy import Class
    >>> exit()
    

    在这里,一切都很好。

  7. 现在真相时,在你的代码目录中下载monthypython tarball:cd~mkdir code cd code wget https://www.dropbox.com/s/l7mnhwsktp1m8b5/montepython_v1.2.5.tar.bz2?dl=1 -O montepython_v1.2.5 .tar.bz2 tar jxf montepython_v1.2.5.tar.bz2在安装说明are some notes中与此特殊情况(新安装)无关,但在更新montepython时可能很重要。

  8. 这种变化很重要。你还记得你安装CLASS的地方吗?现在你应该告诉Monte Python在哪里。在我的情况下它是/home/braiam/src/class_v2.0.2/class,你的可能会有所不同。您可以使用find来查找它:

    find ~ -name class -print
    

    那么,现在我使用您喜欢的任何内容修改default.conf文件(我将使用nano)并修改root路径,因此完整的文件将如下所示:

    # Fill in the relevant path to your personal distribution. 
    # If you create a new file out of this one, please remember to call
    # MontePython.py with the option '-conf my.conf'
    
    # At minimum, this file should contain one line:
    # ** path['cosmo'] =  path to the cosmological code you are using. Note, if you are
    # using a modified version of class, be sure that the path contains the word
    # class, otherwise the code might not recognise it.
    
    # If you want to use a data folder different from the one present in the folder
    # you are executing the code, please also add:
    # ** path['data']  =  /path/to/the/other/data/
    
    root = '/home/braiam/src/class_v2.0.2/'
    
    path['cosmo']           = root+'/class/'
    

    保存文件并退出。

  9. 测试montepython!如果一切正常,你应该能够在你的montepythong目录中运行code/MontePython.py --help并运行一切:〜/src /montepython $ code /MontePython.py –help用法:MontePython.py [-h] [-N步数] [-o output folder] [-p input param file] [-c input cov matrix] [-j jump method] [-f jumping factor] [-conf configuration file] [-chain_number chain number] [-r restart from链[]从最佳拟合文件重新启动] [-info [所需文件的计算信息[所需文件的计算信息…]]] [ – 所需的箱数,默认为20] [-no_mean] [ – comp比较文件夹] [-extra plot file for custom needs] [-noplot] [-all] [-ext change extension for output file] [-fontsize desired fontsize,默认为15] [-ticksize desired ticksize,默认为13 ] Monte Py thon,Python中的蒙特卡罗代码可选参数:-h, – help显示此帮助消息并退出-N步数-o输出文件夹-p输入参数文件-c输入cov矩阵-j跳转方法-f跳跃因子-conf配置文件-chain_number链号-r从链-bf重启从最佳拟合文件-info [所需文件的计算信息[所需文件的计算信息…]] – 所需的箱数,默认为20 – no_mean -comp比较文件夹-extra用于自定义需求的绘图文件-noplot -all -ext更改输出文件的扩展名-fontsize desired fontsize,默认为15 -ticksize所需的ticksize,默认为13

有了这个,你已经安装了montepython并准备好了!

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2124.html,未经允许,请勿转载。