当前位置 : 首页 » 文章分类 :  科研  »  Python-科学计算环境搭建

Python-科学计算环境搭建

Python科学计算环境配置

2to3.py

几乎所有的Python2程序都需要一些修改才能正常地运行在Python3的环境下。为了简化这个转换过程,Python 3自带了一个叫做2to3的实用脚本(Utility Script),这个脚本会将你的Python 2程序源文件作为输入,然后自动将其转换到Python 3的形式。这个脚本的位置位在Python安装的根目录下的 Python35\Tools\scripts\目录中。

-w参数,不加-w参数,则默认只是把转换过程所对应的diff内容打印输出到当前窗口而已。加了-w,就是把改动内容,写回到原先的文件了,同时会将原文件保存为文件名.bak做备份。

将当前目录下的python2源码transform.py转换为python3:
2to3.py -w transform.py
执行完后transform.py变为python3格式源码,同时生成transform.py.bak

将E:\ipv6–master\下的所有python2代码转换为python3:
python 2to3.py -w E:\ipv6--master\

pip

pip freeze 命令可查看通过pip安装了哪些包,分别是什么版本

D:\python.code\4.RandomForest>pip freeze
backports.weakref==1.0rc1
bleach==1.5.0
cycler==0.10.0
html5lib==0.9999999
Keras==2.0.5
Markdown==2.2.0
matplotlib==2.1.0
numpy==1.13.0+mkl
pandas==0.20.2
protobuf==3.3.0
pyparsing==2.2.0
python-dateutil==2.6.0
pytz==2017.2
PyYAML==3.12
scikit-learn==0.18.1
scipy==0.19.0
six==1.10.0
tensorflow==1.2.0
Theano==0.9.0
Werkzeug==0.12.2

python3 Windows安装

从官网 https://www.python.org/ 下载Python3.5.3 Windows版安装文件 python-3.5.3-amd64.exe 安装,勾选Add Python 3.5.3 to PATH加入环境变量,其他默认安装即可。
默认会安装:Documentation,pip,tcl/tk and IDLE,Python test suite,py launcher
默认安装目录为:C:\Users\用户名\AppData\Local\Programs\Python\Python35
安装完成后打开CMD命令窗口,输入python,出现python版本提示并进入python交互环境说明安装成功,如下:

C:\Users\MaSi>python
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

若提示”python不是内部或外部命令,也不是可运行的程序或批处理文件”说明PATH环境变量没配置,手动将python安装目录加入PATH环境变量。

numpy安装

https://pypi.python.org/pypi/numpy 下载numpy-1.13.0
numpy和python是有版本对应关系的,由于我们的Python装的是python-3.5.3-amd64版,所以在这里要选用amd64并对应cp35,下载numpy-1.13.0-cp35-none-win_amd64.whl 安装包
CMD命令窗口中使用pip install安装,如下:

C:\Users\MaSi>pip install d:软件\\Python\\numpy-1.13.0-cp35-none-win_amd64.whl
Processing d:\软件\python\numpy-1.13.0-cp35-none-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.13.0

scipy安装

http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载scipy-0.19.0 的Windows安装包 scipy-0.19.0-cp35-cp35m-win_amd64.whl
CMD命令窗口中使用pip install安装,如下:

C:\Users\MaSi>pip install d:软件\\Python\\scipy-0.19.0-cp35-cp35m-win_amd64.whl
Processing d:\软件\python\scipy-0.19.0-cp35-cp35m-win_amd64.whl
Requirement already satisfied: numpy>=1.8.2 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from scipy==0.19.0)
Installing collected packages: scipy
Successfully installed scipy-0.19.0

安装成功后,在python环境下执行命令import scipy,报错:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\MaSi\AppData\Local\Programs\Python\Python35\lib\site-packages\scipy\__init__.py", line 61, in <module>
    from numpy._distributor_init import NUMPY_MKL  # requires numpy+mkl
ImportError: cannot import name 'NUMPY_MKL'

原因是安装的numpy中不包含MKL包,在 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载Numpy+MKL安装包numpy-1.13.0+mkl-cp35-cp35m-win_amd64.whl,然后卸载刚安装的numpy
pip uninstall numpy-1.13.0-cp35-none-win_amd64.whl
重新安装numpy-1.13.0+mkl-cp35-cp35m-win_amd64.whl,
安装成功后,在python环境下执行命令import scipy,成功。

https://www.zhihu.com/question/30188492/answer/125707763

scikit-learn安装

Scikit-learn requires:
Python (>= 2.6 or >= 3.3),
NumPy (>= 1.6.1),
SciPy (>= 0.9).
http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载Scikit-learn-0.18.2 的Windows安装包 scikit_learn‑0.18.2‑cp35‑cp35m‑win_amd64.whl
CMD命令窗口中使用pip install安装,如下:

C:\Users\MaSi>pip install "d:软件\\Python\\scikit_learn-0.18.1-cp35-cp35m-win_amd64.whl"
Processing d:\软件\python\scikit_learn-0.18.1-cp35-cp35m-win_amd64.whl
Installing collected packages: scikit-learn
Successfully installed scikit-learn-0.18.1

安装成功后进入python命令行执行命令from sklearn import datasets,不报错说明安装成功。

theano安装

在安装keras之前,要先安装numpy,scipy和theano,因为keras是基于theano向上开发的,所以要安装keras先安装theano。
http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载Theano-0.9.0-py2.py3-none-any.whl安装

C:\Users\MaSi>pip install "d:软件\\Python\\Theano-0.9.0-py2.py3-none-any.whl"
Processing d:\软件\python\theano-0.9.0-py2.py3-none-any.whl
Requirement already satisfied: scipy>=0.14 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from Theano==0.9.0)
Requirement already satisfied: numpy>=1.9.1 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from Theano==0.9.0)
Collecting six>=1.9.0 (from Theano==0.9.0)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six, Theano
Successfully installed Theano-0.9.0 six-1.10.0

安装完在python命令行执行import theano提示没有安装g++:

>>> import theano
WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will defau
lt to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.

解决方法是安装MinGW,然后将mingw安装目录中的bin目录添加到PATH环境变量,因为里面有g++.exe
我本机已经安装了cygwin64,其中也有g++.exe,将其中的bin目录加入PATH环境变量也可以。
之后在python命令行执行import theano报出一堆错误信息。

安装keras

C:\Users\MaSi>pip install "d:软件\\Python\\Keras-2.0.5-py2.py3-none-any.whl"
Processing d:\软件\python\keras-2.0.5-py2.py3-none-any.whl
Requirement already satisfied: six in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from Keras==2.0.5)
Requirement already satisfied: theano in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from Keras==2.0.5)
Collecting pyyaml (from Keras==2.0.5)
  Downloading PyYAML-3.12-cp35-cp35m-win_amd64.whl (195kB)
    100% |████████████████████████████████| 204kB 1.4MB/s
Requirement already satisfied: numpy>=1.9.1 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from theano->Keras==2.0.5)
Requirement already satisfied: scipy>=0.14 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from theano->Keras==2.0.5)
Installing collected packages: pyyaml, Keras
Successfully installed Keras-2.0.5 pyyaml-3.12

安装tensorflow

C:\Users\MaSi>pip install "d:软件\\Python\\Keras-2.0.5-py2.py3-none-any.whl"
Processing d:\软件\python\keras-2.0.5-py2.py3-none-any.whl
Requirement already satisfied: six in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from Keras==2.0.5)
Requirement already satisfied: theano in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from Keras==2.0.5)
Collecting pyyaml (from Keras==2.0.5)
  Downloading PyYAML-3.12-cp35-cp35m-win_amd64.whl (195kB)
    100% |████████████████████████████████| 204kB 1.4MB/s
Requirement already satisfied: numpy>=1.9.1 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from theano->Keras==2.0.5)
Requirement already satisfied: scipy>=0.14 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from theano->Keras==2.0.5)
Installing collected packages: pyyaml, Keras
Successfully installed Keras-2.0.5 pyyaml-3.12

C:\Users\MaSi>pip install "d:软件\\Python\\tensorflow-1.2.0-cp35-cp35m-win_amd64.whl"
Processing d:\软件\python\tensorflow-1.2.0-cp35-cp35m-win_amd64.whl
Requirement already satisfied: six>=1.10.0 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from tensorflow==1.2.0)
Collecting backports.weakref==1.0rc1 (from tensorflow==1.2.0)
  Downloading backports.weakref-1.0rc1-py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorflow==1.2.0)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 1.0MB/s
Requirement already satisfied: numpy>=1.11.0 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from tensorflow==1.2.0)
Collecting protobuf>=3.2.0 (from tensorflow==1.2.0)
  Downloading protobuf-3.3.0.tar.gz (271kB)
    100% |████████████████████████████████| 276kB 1.0MB/s
Collecting wheel>=0.26 (from tensorflow==1.2.0)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 1.4MB/s
Collecting markdown==2.2.0 (from tensorflow==1.2.0)
  Downloading Markdown-2.2.0.tar.gz (236kB)
    100% |████████████████████████████████| 245kB 1.3MB/s
Collecting bleach==1.5.0 (from tensorflow==1.2.0)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting werkzeug>=0.11.10 (from tensorflow==1.2.0)
  Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
    100% |████████████████████████████████| 317kB 1.1MB/s
Requirement already satisfied: setuptools in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from protobuf>=3.2.0->tensorflow=
=1.2.0)
Installing collected packages: backports.weakref, html5lib, protobuf, wheel, markdown, bleach, werkzeug, tensorflow
  Running setup.py install for html5lib ... done
  Running setup.py install for protobuf ... done
  Running setup.py install for markdown ... done
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 html5lib-0.9999999 markdown-2.2.0 protobuf-3.3.0 tensorflow-1.2.0 werkzeug-0.12.2 wheel-0
.29.0

安装pandas

C:\Users\MaSi>pip install "d:软件\\Python\\pandas-0.20.2-cp35-cp35m-win_amd64.whl"
Processing d:\软件\python\pandas-0.20.2-cp35-cp35m-win_amd64.whl
Requirement already satisfied: numpy>=1.7.0 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from pandas==0.20.2)
Collecting python-dateutil>=2 (from pandas==0.20.2)
  Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194kB)
    100% |████████████████████████████████| 194kB 605kB/s
Collecting pytz>=2011k (from pandas==0.20.2)
  Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
    100% |████████████████████████████████| 491kB 585kB/s
Requirement already satisfied: six>=1.5 in c:\users\masi\appdata\local\programs\python\python35\lib\site-packages (from python-dateutil>=2->pandas==0.
20.2)
Installing collected packages: python-dateutil, pytz, pandas
Successfully installed pandas-0.20.2 python-dateutil-2.6.0 pytz-2017.2

上一篇 Markdown 语法简介

下一篇 Hexo博客(13)添加MathJax数学公式渲染

阅读
评论
2.3k
阅读预计12分钟
创建日期 2017-06-20
修改日期 2018-06-11
类别
标签

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论