【1】模块管理-1-Python模块的安装(pip/easy_install)

第一天工作就遇到了问题,安装python模块的时候没有管理员权限。

[Errno 13] Permission denied: '/usr/lib64/python2.7/site-packages/test-easy-install-13645.write-test'

一、事故过程:

安装Cython

https://pypi.python.org/pypi 搜索包Cython

https://pypi.python.org/pypi/Cython/0.22.1 选择Cython-0.22.1.tar.gz (md5, pgp)

[q python]$ cd ./Cython-0.22.1
[q Cython-0.22.1]$ python setup.py install

报错:

[Errno 13] Permission denied: '/usr/lib64/python2.7/site-packages/test-easy-install-13645.write-test'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/usr/lib64/python2.7/site-packages/
Perhaps your account does not have write access to this directory? If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account. If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.
For information on other options, you may wish to consult the
documentation at:
https://pythonhosted.org/setuptools/easy_install.html
Please make the appropriate changes for your system and try again.

原因就在于没有管理员权限,所以我们需要:指定安装位置

解决思路:

正常情况下,我们要给Python安装第三方的扩展包,我们必须下载压缩包,解压缩到一个目录,然后命令行或者终端打开这个目录,然后执行

python setup.py install

来进行安装。 这样是不是很繁琐呢?如果我们直接命令行执行

easy_install Twisted - - prefix /path

所以easy_install安装更容易,同时可以指定安装位置

解决办法:

1.将指定路径下的lib和bin分别加入环境变量中:

echo “export PYTHONPATH=$PYTHONPATH:~/lib/python2.7/site-packages”>>.bashrc
echo "export PATH=~/bin:$PATH" >> .bashrc;
source .bashrc

2.安装easy_install(我的自带的,所以没有安装它)

方法一:

首先下载easy_install的安装包,下载地址:

http://pypi.python.org/pypi/setuptools

下载自己对应的版本,windows上面直接运行exe安装就可以了 linux上面可以直接运行

sh setuptools-0.6c9-py2.4.egg

安装完成后,

easy_install

会被自动复制到bin目录下,也就是我们的PATH路径下,所以我们在终端中可以直接运行easy_install命令了

方法二:(没有权限用下面的方法)

wget http://pypi.python.org/packages/any/s/setuptools/setuptools-0.6c11-1.src.rpm#md5=27a3fe3a4973c0cc97534bc9e9720c3d
bash setuptools-0.6c11-py2.6.egg --prefix=~/

easy_install - -prefix ~/ Twisted

测试一下,看是否成功

3.安装Cython

终端下输入:

easy_install --prefix=~/ Cython

Installed /home/qqin/lib/python2.7/site-packages/Cython-0.22.1-py2.7-linux-x86_64.egg
Processing dependencies for Cython
Finished processing dependencies for Cython

4.如果用easy_isntall或pip无法找到合适的版本

下载该模块,然后到cd 进去

python setup.py install --prefix /home/qqin

二、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的安装

    wget -c https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9

    tar -xzvf pip-9.0.1.tar.gz

    python setup.py install

先进行pip工具的安裝:easy_install pip(pip 可以通过easy_install 安裝,而且也会装到 Scripts 文件夹下。)

  • 安裝:pip install PackageName
  • 更新:pip install -U PackageName
  • 移除:pip uninstall PackageName
  • 搜索:pip search PackageName
  • 帮助:pip help
  • 显示模块的信息: pip show tensorflow

下载指定版本: pip install robotframework==2.8.7

注:当时想用pip来安装python模块

批量安装(requirements中包含各种需要被安装的包):

pip install -r  requirements

安装到指定文件夹中

pip install --target=d:\somewhere\other\than\the\default package_name

更简单的方法(直接安装到本地用户)

pip install package --user

报错:

File "/usr/local/bin/pip", line 5, in 
        from pkg_resources import load_entry_point
    File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2927, in 
        @_call_aside
    File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2913, in _call_aside
        f(*args, **kwargs)
    File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2940, in _initialize_master_working_set
        working_set = WorkingSet._build_master()
    File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 635, in _build_master
        ws.require(__requires__)
    File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 943, in require
        needed = self.resolve(parse_requirements(requirements))
    File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 829, in resolve
        raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

这是pip版本的冲突

sudo which pip
sudo vim /usr/bin/pip
#modify any pip==6.1.1 to pip==8.1.2 or the version you just upgrade to.
发现我用的pip版本跟sudo pip用的版本不一样
sudo easy_install --upgrade pip

报错:

pip is error,TypeError: __call__() takes exactly 2 arguments (1 given)

解决办法:

python -m pip install --upgrade --force pip 
pip install setuptools==33.1.1

三、安装过程中常见的问题

当在安装包的时候,不管是手动安装 还是 easy_install,如果提示

/tmp/pip-build-MU3zkf/Cython/Cython/Plex/Scanners.c:8:22: fatal error: pyconfig.h: No such file or directory
#include "pyconfig.h"
^
compilation terminated.
error: command 'gcc' failed with exit status 1 

错误为:找不到python.h文件,那说明我们安装的python不是开发版

如果ubuntu下面的解决办法如下:(这里需要管理员的权限帮助一下)

sudo apt-get install python-dev

如果是redhat

sudo yum install python-devel ()

apt-get是debian的软件包管理方式 yum 、rpm是redhat的包管理方式

至于是python-dev还是python-devel可以先查看一下含python-devel的包

yum search python | grep python-devel
[qqin@lizard Cython-0.22.1]$ yum search python | grep python-devel
dbus-python-devel.i686 : Libraries and headers for dbus-python
dbus-python-devel.x86_64 : Libraries and headers for dbus-python
gstreamer-python-devel.i686 : Headers for developing programs that will use
gstreamer-python-devel.x86_64 : Headers for developing programs that will use
python-devel.x86_64 : The libraries and header files needed for Python
qscintilla-python-devel.noarch : Development files for QScintilla PyQt4 bindings

在没有root权限的情况下安装软件的方法如下:

1.很多软件都在~目录下有配置文件。如果你只需要更改配置的话,只需更改你个人目录下的配置文件即可。如.bashrc、.vimrc、.screenrc

2.如果你要安装的仅仅是某个软件的模块,如果安装时能指定prefix参数的话,可以安装到home目录下。比如python安装模块,增加新的vim配色文件和插件等。

3.如果不满足1,或者你需要安装一个不同的版本,最好下载源代码,自己编译安装

./configure --prefix=~/path
make
make install

4.如果有必要,更改环境变量。需要注意的是,如果你安装了一个不同于系统已有版本的软件,需要在PATH变量中将新安装软件的bin路径放在原有软件的前面,以确保系统先搜到新安装的软件。

参考资料:

http://blog.csdn.net/lonfee88/article/details/6672845

http://www.cnblogs.com/huangjacky/archive/2012/03/28/2421866.html

http://jingyan.baidu.com/article/597035521e76ef8fc00740cf.html

http://blog.163.com/yang_jianli/blog/static/161990006201162152724339/

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学