Python第三方库的安装方法总结

       

        Python 是一门优雅的语言,简洁的语法,强大的功能。丰富的第三方库,更能加速开发,库library是一个泛称,在这里介绍Python第三方库的安装方法总结,包括源码安装、包管理器安装和离线安装。

一、源码安装

     大部分第三方库都是开源的,几乎集中在github 或者 pypi上。源码格式大概是 zip 、 tar.zip、 tar.bz2格式的压缩包。进入解压好的文件夹,通常会看见一个 setup.py 的文件。打开命令行,进入该文件夹。运行

python setup.py install

      这个命令,就能把这个第三库安装到系统里,也就是 Python 路径。通过命令行 :where python可以找到python的安装路径。

      源码安装需要下载,解压,再运行安装,太麻烦了,卸载也不是很方便。有没有一种工具,可以帮外面管理这些库呢?

二、包管理器 (pip 和 easy_install)

       现在很多主流编程语言,都带有包管理器,例如 Ruby 的 gem,nodejs的npm。

        Python拥有 pip和 easy_install。

       前面说到的pypi就是一些 python第三库所在的源库,使用 pip 或者 easy_install 安装模块,会搜索这个源,然后自动下载安装。比如我们需要安装 pygame 框架具体命令如下:

pip install pygame

        或者

easy_install pygame

        卸载也很方便,例如我们需要卸载pygame

pip uninstall pygame

         查看所安装的包,包括系统自带的和手动安装的

pip list

          还可以搜索包

pip search flask

        还可以重定向输出项目使用的库。

pip freeze > requirements.txt

        这样就把该环境下的第三方库重定向到 requirements.txt文件内,如果给别人安装项目的依赖,只需要运行:

pip install -r requirements.txt

PIP安装的包在什么位置?一般情况下:包总是被安装在python安装目录下的lib\site-packages\包名\

  三、离线安装

       先在  PYPI上寻找包,然后下载到本地。

       先建一个本地的仓库,把常用的包离线下载。比如可以下载pygame 的源码

pip intall pygame-master.zip

        如果下载的是  .whl 格式的文件,安装命令是:

        cd 到本地仓库(放whl的文件夹):

python -m pip install --user idna-2.7-py2.py3-none-any.whl

四、把第三方库离线安装到虚拟环境下

         先在  PYPI上寻找包,然后下载到本地,如.whl格式的包。

         把.whl放到虚拟环境目录下,运行这个命令就可以把离线包安装到虚拟环境下。

python -m pip install Django-2.0.7-py3-none-any.whl

       其实就是上面的第三个办法的命令,不用写参数 --user。如果保留--user 这个参数,它会把包安装在Python系统下面,而不是虚拟环境。

 

 

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python3.6标准库 It contains data types that would normally be considered part of the “core” of a language, such as numbers and lists. For these types, the Python language core defines the form of literals and places some constraints on their semantics, but does not fully define the semantics. The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import statement. Some of these are defined by the core language, but many are not essential for the core semantics and are only described here. The bulk of the library, however, consists of a collection of modules. There are many ways to dissect this collection. Some modules are written in C and built in to the Python interpreter; others are written in Python and imported in source form. Some modules provide interfaces that are highly specific to Python, like printing a stack trace; some provide interfaces that are specific to particular operating systems, such as access to specific hardware; others provide interfaces that are specific to a particular application domain, like the World Wide Web. Some modules are available in all versions and ports of Python; others are only available when the underlying system supports or requires them; yet others are available only when a particular configuration option was chosen at the time when Python was compiled and installed. This manual is organized “from the inside out:” it first describes the built-in functions, data types and exceptions, and finally the modules, grouped in chapters of related modules. This means that if you start reading this manual from the start, and skip to the next chapter when you get bored, you will get a reasonable overview of the available modules and application areas that are supported by the Python library. Of course, you don’t have to read it like a novel — you can also browse the table of contents (in front of the manual), or look for a specific function, module or term in the index (in the back). And finally, if you enjoy learning about random subjects, you choose a random page number (see module random) and read a section or two. Regardless of the order in which you read the sections of this manual, it helps to start with chapter Built-in Functions, as the remainder of the manual assumes familiarity with this material.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值