Python模块安装方法
一、方法1: 单文件模块
直接把文件拷贝到 $python_dir/Lib
二、方法2: 多文件模块,带setup.py
下载模块包,进行解压,进入模块文件夹,执行:
python setup.py install
三、 方法3:easy_install 方式
先下载ez_setup.py,运行python ez_setup 进行easy_install工具的安装,之后就可以使用easy_install进行安装package了。
easy_install packageName
easy_install package.egg
四、 方法4:pip 方式
先进行pip工具的安裝:easy_install pip(pip 可以通过easy_install 安裝,而且也会装到 Scripts 文件夹下。)
安裝:pip install PackageName
更新:pip install -U PackageName
移除:pip uninstall PackageName
搜索:pip search PackageName
帮助:pip help
注:虽然Python的模块可以拷贝安装,但是一般情况下推荐制作一个安装包,即写一个setup.py文件来安装。
setup.py文件的使用如下:
% python setup.py build #编译
% python setup.py install #安装
% python setup.py sdist #制作分发包
% python setup.py bdist_wininst #制作windows下的分发包
% python setup.py bdist_rpm
setup.py文件的编写
setup.py中主要执行一个 setup函数,该函数中大部分是描述性东西,最主要的是packages参数,列出所有的package,可以用自带的find_packages来动态获取package。所以setup.py文件的编写实际是很简单的。
简单的例子:
setup.py文件:
from setuptools import setup, find_packages
setup(
name = " mytest " ,
version = " 0.10 " ,
description = " My test module " ,
author = " Robin Hood " ,
url = " http://www.csdn.net " ,
license = " LGPL " ,
packages = find_packages(),
scripts = [ " scripts/test.py " ],
)
mytest.py
import sys
def get():
return sys.path
scripts/test.py
import os
print os.environ.keys()
setup中的scripts表示将该文件放到 Python的Scripts目录下,可以直接用。OK,简单的安装成功,可以运行所列举的命令生成安装包,或者安装该python包。本机测试成功(win32-python25)!
附注:setuptools工具安装方法
setuptools它可以自动的安装模块,只需要你提供给它一个模块名字就可以,并且自动帮你解决模块的依赖问题。一般情况下用setuptools给安装的模块会自动放到一个后缀是.egg的目录里。
在Windows里,easy_install这个命令在python安装目录下的scripts里面,所以需要把scripts加到环境变量的PATH里,这样用起来就更方便,linux下不需要注意这个问题。
在Ubuntu中安装numpy、scipy、matplotlib、OpenCV等
和Python(x,y)不一样,在Ubuntu中需要手工安装科学计算的各个模块,
如何安装IPython, NumPy, SciPy, matplotlib, PyQt4, Spyder, Cython, SWIG, ETS, OpenCV:
在Ubuntu下安装Python模块通常可以使用apt-get和pip命令。apt-get命令是Ubuntu自带的包管理命令,而pip则是Python安装扩展模块的工具,通常pip会下载扩展模块的源代码并编译安装。
Ubuntu 12.04中缺省安装了Python2.7.3,首先通过下面的命令安装pip,pip是Python的一个安装和管理扩展库的工具。
sudo apt-get install python-pip
安装Python开发环境【亦可参见linux中搭建python开发环境安装pycharm IDE】,方便今后编译其他扩展库,占用空间92.8M:
sudo apt-get install python-dev
IPython
为了安装最新版的IPython 0.13beta,需要下载IPython源代码,并执行安装命令。在IPython 0.13beta中提供了改进版本的IPython notebook。下面的命令首先安装版本管理软件git,然后通过git命令从IPython的开发代码库中下载最新版本的IPython源代码,并执行安装命令:
cd
sudo apt-get install git
git clone https://github.com/ipython/ipython.git
cd ipython
sudo python setup.py install
如果安装目前的最新稳定版本,可以输入:
sudo apt-get install ipython
安装完毕之后,请输入ipython命令测试是否能正常启动。
为了让IPython notebook工作,还还需要安装tornado和pyzmq:
sudo pip install tornado
sudo apt-get install libzmq-dev
sudo pip install pyzmq
sudo pip install pygments
下面测试IPython:
cd
mkdir notebook
cd notebook
ipython notebook
为了在IPython中离线使用LaTeX数学公式,需要安装mathjax,首先输入下面的命令启动ipython notebook:
sudo ipython notebook
在IPython notebook界面中输入:
from IPython.external.mathjax import install_mathjax
install_mathjax()
安装NumPy,SciPy和matplotlib
通过apt-get命令可以快速安装这三个库:
sudo apt-get install python-numpy
sudo apt-get install python-scipy
sudo apt-get install python-matplotlib
查看 numpy 版本和路径:
import numpy
print numpy.__version__
print numpy.__file__
如果需要通过pip编译安装,可以先用apt-get命令安装所有编译所需的库:
sudo apt-get build-dep python-numpy
sudo apt-get build-dep python-scipy
然后通过pip命令安装:
sudo pip install numpy
sudo pip install scipy
通过build-dep会安装很多库,包括Python 3.2。
PyQt4和Spyder
下面的命令安装PyQt4,Qt界面设计器,PyQt4的开发工具以及文档:
sudo apt-get install python-qt4
sudo apt-get install qt4-designer
sudo apt-get install pyqt4-dev-tools
sudo apt-get install python-qt4-doc
安装完毕之后,文档位于:
/usr/share/doc/python-qt4-doc
安装好PyQt4之后通过下面的命令安装Spyder:
sudo apt-get install spyder
由于Spyder经常更新,通过下面的命令可以安装最新版:
sudo pip install spyder --upgrade
cython和SWIG
Cython和SWIG是编写Python扩展模块的工具:
sudo pip install cython
sudo apt-get install swig
输入 cython --version 和 swig -version 查看版本。
ETS
ETS是enthought公司开发的一套科学计算软件包,其中的Mayavi通过VTK实现数据的三维可视化。
首先通过下面的命令安装编译ETS所需的库:
sudo apt-get install python-dev libxtst-dev scons python-vtk pyqt4-dev-tools python2.7-wxgtk2.8 python-configobj
sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev
创建ets目录,并在此目录下下载ets.py,运行ets.py可以复制最新版的ETS源程序,并安装:
mkdir ets
cd ets
wget https://github.com/enthought/ets/raw/master/ets.py
python ets.py clone
sudo python ets.py develop
#sudo python ets.py install 或者运行install安装
如果一切正常,那么输入 mayavi2 命令则会启动mayavi。
OpenCV
python-opencv:
环境
ubuntu 12.04 LTS
python 2.7.3
opencv 2.3.1-7
安装依赖
sudo apt-get install libopencv-*
sudo apt-get install python-opencv
sudo apt-get install python-numpy
【在python中使用OpenCV】
【python使用opencv进行人脸识别】
【Ubuntu 12.04下安装OpenCV 2.4.2】
【UBUNTU下安装OPENCV和测试python-opencv】
【Ubuntu下Opencv与Python的协作】
【ubuntu 下 安装 python-opencv 配置】
dpkg -L python-opencv命令查看,搜索安装到何处
root@ubuntu:~#dpkg -L python-opencv
测试opencv安装好没:
python-opencv:
更新下载更新软件包列表信息
apt-get update
查询OpenCV相关软件包
$ apt-cache search opencv
libcv-dev - development files for libcv
libcv1 - computer vision library
libcvaux-dev - development files for libcvaux
libcvaux1 - computer vision extension library
libhighgui-dev - development files for libhighgui
libhighgui1 - computer vision GUI library
opencv-doc - OpenCV documentation and examples
python-opencv - Python bindings for the computer vision library
//以上内容可能是没有及时更新
//用命令行$ apt-cache search opencv在ubuntu(12.04LTS)找到的最新的opencv版本是2.1
harpia - Image Processing/Computer Vision Automatic Prgm. Tool
libcv-dev - development files for libcv
libcv2.1 - computer vision library
libcvaux-dev - development files for libcvaux
libcvaux2.1 - computer vision extension library
libhighgui-dev - development files for libhighgui
libhighgui2.1 - computer vision GUI library
opencv-doc - OpenCV documentation and examples
python-opencv - Python bindings for the computer vision library
在这里,OpenCV的库CxCore和Cv都被包含入Deb包libcv中。
安装相关软件包
如果只是用来运行OpenCV程序,仅需安装libcv1,libcvaux1,libhighgui1:
apt-get install libcv1 libcvaux1 libhighgui1
如果你要使用OpenCV来编写程序,那么还需要安装libcv-dev,libcvaux-dev,libhighgui-dev包。
apt-get install libcv-dev libcvaux-dev libhighgui-dev
文档在opencv-doc包中,python-opencv是OpenCV的Python语言包,可根据需要安装。
测试安装包
测试是否安装成功,你可以使用以下的命令行编译位于源代码包中的drawing.c例子:
g++ drawing.c `pkg-config opencv --libs --cflags opencv` -o drawing
成功编译后你应该能够可以执行./drawing看到highgui输出窗口的结果了.
【Debian/Ubuntu下安装】
c-opencv?
为了编译OpenCV需要下载cmake编译工具,和一些依赖库:
sudo python setup.py install
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install cmake-gui
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libjasper-dev
然后终端输入http://sourceforge.net/projects/opencvlibrary/files/latest/download?source=files下载opencv源代码
或者从 http://sourceforge.net/projects/opencvlibrary/下载最新版的OpenCV源代码,并解压。
然后创建编译用的目录release,并启动cmake-gui:
mkdir release
cmake-gui
在界面中选择OpenCV源代码的目录,和编译输出目录release,然后按Configure按钮,并根据需要设置各个编译选项,最后点Generate按钮,退出cmake-gui界面。进入编译路径,执行下面的命令:
cd release
make
sudo make install
安装完毕之后,启动IPython,并输入 import cv2 测试OpenCV是否能正常载入。在Ubuntu下貌似OpenCv不兼容使用apt-get安装numpy和scipy貌似,好像版本过低。我的解决方法是下了最新的numpy和scipy,然后自己编译安装上去的。为了安装这两个软件,我又安装了另外一大堆东西。
ps:在Ubuntu下貌似OpenCv不兼容使用apt-get安装numpy和scipy貌似,版本过低。解决方法是下载最新的numpy和scipy,然后自己编译安装上去的。为了安装这两个软件,还要安装了另外一大堆东西。
apt-get 和pip的区别
PyPI is the Python Package index — repository of python modules.
pip
is used to download and install packages directly from PyPI. PyPI is hosted by Python Software Foundation. It is a specialized package manager that only deals with python packages.
apt-get
is used to download and install packages from Ubuntu repositories which are hosted by Canonical.
Some of the differences between installing python packages from apt-get
and pip
are as follows:
Canonical only provides packages for selected python modules. Whereas, PyPI hosts a much broader range of python modules. So, there are a lot of python modules which you won't be able to install using apt-get
.
Canonical only hosts a single version of any package (generally the latest or the one released in recent past). So, with apt-get
we cannot decide the version of python-package that we want. pip
helps us in this situation. We can install any version of the package that has previously been uploaded on PyPI. This is extremely helpful in case of conflict in dependencies.
apt-get
installs python modules in system-wide location. We cannot just install modules in our project virtualenv. pip
solves this problem for us. If we are using pip
after activating the virtualenv, it is intelligent enough to only install the modules in our project virtualenv. As mentioned in previous point, if there is a version of a particular python package already installed in system-wide location, and one of our project requires an older version of the same python package, in such situations we can use virtualenv and pip to install that older version of python package without any conflicts.
There is difference in names of packages as well. Canonical usually names Python 2 packages as python-<package_name>
and Python 3 packages as python3-<package_name>
. Whereas for pip
we generally just need to use<package_name>
for both Python 2 as well as Python3 packages.
Both apt-get
and pip
are mature package managers which automatically install any other package dependency while installing. You may use anyone as you like. However, if you need to install a particular version of python-package, or install the package in a virtualenv, or install a package which is only hosted on PyPI; only pip
would help you solve that issue. Otherwise, if you don't mind installing the packages in system-wide location it doesn't really matter whether you use apt-get
or pip
.
其他链接:
Ubuntu 13.10 64位下安装配置 JDK 7:http://blog.csdn.net/richerg85/article/details/13160185