模块_python里如何安装模块pip

安装方式介绍

一、方法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

pip更改源[下面提供四种方式]

通过几次 pip 的使用,对于默认的 pip 源的速度实在无法忍受,于是便搜集了一些国内的pip源,如下:

阿里云 http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

豆瓣(douban) http://pypi.douban.com/simple/

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

方法1. 直接在Terminal中敲命令

pip  install  包名  -i  源地址

如上面出现问题使用下面:
pip install django -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

方法2. 修改配置成默认的源

需要创建或修改配置文件(一般都是创建)也可以直接在pycharm解释器中配置,
linux的文件在~/.pip/pip.conf

Windows下找到Python根目录下的pip文件夹,在文件夹内
新建一个文件pip.ini:
windows在%HOMEPATH%pippip.ini)
修改内容为:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple 
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn

方法3. 临时使用其他源安装软件包的python脚本如下

#!/usr/bin/python

import os

package = raw_input("Please input the package which you want to install!
")
command = "pip install %s -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn" % package
os.system(command)
  1. Python配置pip默认源脚本,复制到pip_source.py,执行即可
#!/usr/bin/python
# coding: utf-8

import platform
import os

os_type = platform.system()
if "Linux" == os_type:
    fileDirPath = "%s/.pip" % os.path.expanduser('~')
    filePath = "%s/pip.conf" % fileDirPath
    if not os.path.isdir(fileDirPath):
        os.mkdir(fileDirPath)
    fo = open(filePath, "w")
    fo.write(
        "[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn
")
    fo.close()
    print "Configuration is complete"
elif "Windows" == os_type:
    fileDirPath = "%s\pip" % os.path.expanduser('~')
    filePath = "%s\pip.ini" % fileDirPath
    if not os.path.isdir(fileDirPath):
        os.mkdir(fileDirPath)
    fo = open(filePath, "w")
    fo.write(
        "[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn
")
    fo.close()
    print "Configuration is complete"
else:
    exit("Your platform is unknow!")
原文地址:https://www.cnblogs.com/sunxiuwen/p/11157141.html