1.如何使用pip安装相关的包
- pip install package==1.8.0 #指定版本
- pip install Package #最新的稳定版
- pip install 包名>=1.4 #满足条件的最小版本
- pip list --outdated
- pip install --upgrade package #更新包
- pip install --upgrade -i https://pypi.doubanio.com/simple/ package #换豆瓣源安装,package换成要安装的包名称即可
- 换源
-i
,--index-url
- 换源
2.Requirements
文件
-
requirements包含了需要通过pip安装的包:
pip install -r requirements.txt
-
requirements的四个常用方法:
- 通过
pip freeze
备份与恢复包环境。
pip freeze > requirements.txt pip install -r requirements.txt PS C:\Users\xiaodong> python -m pip freeze > F:\requirements.txt
- 强制解决依赖问题(并不真正解决依赖问题)。
- 强制使用子依赖的备用版本。
- Constranints文件和requirements类似,不过它不指定必须要安装的版本,而是约束了安装的版本。
pip install -c constranints.txt
- 通过
3.使用Wheel
安装
-
Wheel
是已经构建过的包程序,使用Wheel
包来安装第三方包可以极大的加快安装的速度,并且降低安装的难度。pip install package-1.0-py2.py3-none-any.whl
-
注意:
wheel
包的文件名不能自行修改。
-
注意:
4.卸载包
-
说明:当
pip
更新一个包的时候,会自动启动卸载,再安装新版本。pip uninstall package
5.包信息
-
查看所有已经安装包的列表
pip list
-
查看所有过期的包,并且显示当前最新版本
pip list --outdated
-
包的详细信息
pip show package_name
-
在线搜索包
pip search "pkg" #默认从PyPI开始搜索
6.VirtualEnv——虚拟环境搭建(linux系统)
-
安装
virtualenv
库pip install -i https://pypi.douban.com/simple/ virtualenv
-
创建虚拟环境python2_env
Virtualenv python2_env #python2_env是要创建虚拟环境的目录
-
激活虚拟环境
source python2_env/bin/activate
-
退出虚拟环境(失活)
deactivate
-
指定特定的
Python
版本Virtualenv -p python2 python2_env
7.requests——全能请求库
-
安装
requests
sudo pip install -i https://pypi.douban.com/simple/ requests
-
构造请求
import requests URL = 'http://fanyi.youdao.com/translate? smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null' headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 'X-Requested-With':'XMLHttpRequest' } data = { 'type':'AUTO', 'i':'加油'.encode('utf-8'), 'doctype':'json', 'xmlVersion':'1.8', 'keyfrom':'fanyi.web', 'ue':'UTF-8', 'action':'FY_BY_CLICKBUTTON', 'typoResult':'true' } r = requests.post(URL,headers=headers,data=data)
-
响应对象
>>> r
<Response [200]>#200是状态码,表示http请求正常响应
r.content.split()#split去除空格
[b'{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":0,"translateResult":[[{"src":"\xe5\x8a\xa0\xe6\xb2\xb9","tgt":"Come', b'on"}]],"smartResult":{"type":1,"entries":["","refuel","cheer","play', b'up"]}}']
```
8.JSON
——JavaScript对象标记
-
json
数据举例'{"type":"ZH_CN2EN", "errorCode":0, "elapsedTime":0, "translateResult":[[{"src":"\xe5\x8a\xa0\xe6\xb2\xb9","tgt":"Come', b'on"}]], "smartResult":{"type":1,"entries":["","refuel","cheer","play', b'up"]}}'
-
将json字符串,转换成Python数据
import json
- 直接转换字符串
json.loads()
- 转换文件中的字符串
json.load()
-
将Python数据,转换成json字符串
- 将Python数据类型转换成Json字符串
json.dumps()
- 将Python数据类型转换成含Json字符串的文件
json.dump()
在Linux中搭建属于自己的开发环境:以tornado和sqlalchemy为例
- 1.创建一个单独的项目路径
xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1$
```linux
xiaodong@Xiaodong-VMSrv:~$ mkdir work
xiaodong@Xiaodong-VMSrv:~$ cd work/
xiaodong@Xiaodong-VMSrv:~/work$ ls
xiaodong@Xiaodong-VMSrv:~/work$ mkdir tornado_test
xiaodong@Xiaodong-VMSrv:~/work$ ls
tornado_test
xiaodong@Xiaodong-VMSrv:~/work$ cd tornado_test/
xiaodong@Xiaodong-VMSrv:~/work/tornado_test$ ls
xiaodong@Xiaodong-VMSrv:~/work/tornado_test$ mkdir xiaodong1
xiaodong@Xiaodong-VMSrv:~/work/tornado_test$ ls
xiaodong1
```
- 2.安装Virtualenv库
xiaodong@Xiaodong-VMSrv:~$ pip install -i https://pypi.douban.com/simple/ virtualenv
Collecting virtualenv
Downloading https://pypi.doubanio.com/packages/6f/..../virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 3.4MB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
```
-
3.在此路径中创建一个虚拟环境
xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1$ ls
xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1$ virtualenv python2_tornado
New python executable in /home/xiaodong/work/tornado_test/xiaodong1/python2_tornado/bin/python
Installing setuptools, pip, wheel...done.
```
-
4.激活虚拟环境
xiaodong@XiaodongVMSrv:~/work/tornado_test/xiaodong1/python2_tornado$ source bin/activate
-
5.安装
tornado
,sqlalchemy
(python2_tornado) xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1/python2_tornado$ pip install tornado
Collecting tornado
Downloading tornado-4.5.1.tar.gz (483kB)
100% |████████████████████████████████| 491kB 419kB/s
......#此处省略部分内容
Successfully installed backports-abc-0.5 certifi-2017.4.17 singledispatch-3.4.0.3 tornado-4.5.1
(python2_tornado) xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1/python2_tornado$ pip install sqlalchemy
Collecting sqlalchemy
......#此处省略部分内容
Downloading SQLAlchemy-1.1.9.tar.gz (5.2MB)
100% |████████████████████████████████| 5.2MB 47kB/s
......
Successfully built sqlalchemy
Installing collected packages: sqlalchemy
Successfully installed sqlalchemy-1.1.9
```
网友评论