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

一、Python模块常用的几种安装方式

1.1 单文件模块

直接把文件拷贝到

$python_dir/Lib

1.2 多文件模块,带setup.py

下载模块包,进行解压,进入模块文件夹,执行:

python setup.py install

1.3 easy_install 方式

先下载ez_setup.py,运行python ez_setup 进行easy_install工具的安装,之后就可以使用easy_install进行安装package了。

easy_install packageName
easy_install package.egg

1.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

指定库源(特别是当某些库的下载速度变慢以后)

pip install robotframework==2.8.7 -i https://pypi.tuna.tsinghua.edu.cn/simple/

查看有木有安装某个包

[sam@localhost waves]$ pip list |grep Django
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Django     1.11.29

查看某个包具体信息(包括安置路径

[sam@localhost waves]$ pip show Django
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Name: Django
Version: 1.11.29
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /home/sam/project/waves/venv/lib/python2.7/site-packages
Requires: pytz
Required-by:

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

3.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

3.2 sudo: pip:command not found问题解决

sudo apt-get install python3-pip

原因:编译sudo的时候加入了–with-secure-path 选项。

解决:在环境配置文件里加一个alias

(1) vi  ~/.bashrc 

    #添加如下内容:

       alias sudo='sudo env PATH=$PATH'

(2)source ~/.bashrc

3.3 安装Cython事故过程

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

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

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

3.4 报错4

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

4.5 下载过程提示超时

解决办法: 通过调节timeout参数

pip  install  --timeout 800 absl-py==0.13.0 biopython==1.79 chex==0.0.7 dm-haiku==0.0.4 dm-tree==0.1.6 immutabledict==2.0.0 jax==0.2.14 ml-collections==0.1.0 numpy==1.19.5 scipy==1.7.0 tensorflow==2.5.0 pandas==1.3.4 tensorflow-cpu==2.5.0

四、讨论

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

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

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

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

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

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

参考资料

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