如何为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 "", line 1, in  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in  import add_newdocs File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in  from numpy.lib import add_newdoc File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in  from type_check import * File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in  import numpy.core.numeric as _nx File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in  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 "", line 1, in  File "/usr/lib/python2.7/dist-packages/scipy/__init__.py", line 78, in  from numpy import show_config as show_numpy_config File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in  import add_newdocs File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in  from numpy.lib import add_newdoc File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in  from type_check import * File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in  import numpy.core.numeric as _nx File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in  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"然后使用pip安装它们,如(4)中所示

    但现在看来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 

好的,让我们按照安装指南 :

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

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

     apt-cache策略python-numpy
    上python-numpy的:
    已安装:(无)
    候选人:1:1.7.1-1ubuntu1
    版本表:
      1:1.7.1-1ubuntu1 0
         500 http://archive.ubuntu.com/ubuntu/ raring / main amd64包 

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

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

      apt-cache策略cython
    用Cython:
    已安装:(无)
    候选人:0.17.4-0ubuntu1
    版本表:
      0.17.4-0ubuntu1 0
         500 http://archive.ubuntu.com/ubuntu/ raring / main amd64包 

    我们有它,我们安装它:

     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其中/ / tar /是
     tar zxf class_v2.0.2.tar.gz
     cd class_v2.0.2
    使 

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

      cd python
     python setup.py构建
     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代码
     cd代码
     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 

    在安装说明中有一些与这种特殊情况(新安装)无关的注释 ,但在更新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输出文件夹]
                       [-p input param file] [-c input cov matrix]
                       [-j跳跃法] [-f跳跃因子]
                       [-conf配置文件] [-chain_number链号]
                       [-r从链中重启] [-bf从最适合的文件重启]
                       [-info [计算所需文件的信息[所需文件的计算信息...]]]
                       [-bins所需的箱数,默认为20] [-no_mean]
                       [-comp比较文件夹]
                       [-extra plot file for custom needs] [-noplot] [-all]
                       [-ext更改输出文件的扩展名]
                       [-fontsize desired fontsize,默认为15]
                       [-ticksize所需的ticksize,默认为13]
    
     Monte Python,Python中的蒙特卡罗代码 

    可选参数:
    -h, – help显示此帮助消息并退出
    -N步数
    -o输出文件夹
    -p输入参数文件
    -c输入cov矩阵
    -j跳跃法
    -f跳跃因子
    -conf配置文件
    -chain_number链号
    -r从链重启
    -bf从最适合的文件重新启动
    -info [计算所需文件的信息[所需文件的计算信息…]]
    -bins所需的箱数,默认为20
    -no_mean
    -comp比较文件夹
    -extra用于定制需求的绘图文件
    -noplot
    -所有
    -ext更改输出文件的扩展名
    -fontsize所需的fontsize,默认为15
    -ticksize所需的ticksize,默认为13

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