03.python工作环境
TRANSCRIPT
![Page 2: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/2.jpg)
Python环境搭建
![Page 3: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/3.jpg)
Python安装
Window 下载安装包安装,可能需要手动处理PATHLinux预装,如要升级可以手工下载源码安装Mac预装,可以使用brew来安装
![Page 4: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/4.jpg)
编辑器
• 可以是任何编辑器
• UliPad
• PyDev
• Pycharm
![Page 5: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/5.jpg)
Python包的安装
![Page 6: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/6.jpg)
setup.py
• 第三方包可以安装到python安装目录下的site-packages 目录下,通过:
python setup.py install
• 也可以只做链接,不真正安装(setuptools支持):
python setup.py develop
![Page 7: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/7.jpg)
使用setup.py不足
• 下载源码
• 解压
• 在命令行执行
![Page 8: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/8.jpg)
setuptools
• 可以远程安装
easy_install package
• 可以指定版本
easy_install package==0.2
• 支持develop模式
![Page 9: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/9.jpg)
pip
• 比setuptools更强大
• 支持远程安装 pip install package
• 支持下载缓存 pip install –d <dir> package
• 支持安装压缩包 pip install package.tgz
• 支持批量安装 pip install –r requirements.txt
• 支持反安装 pip uninstall package
• 可以更换镜像 pip install package -i <url>
• 本地缓存,开发版本,远程仓库等…
![Page 10: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/10.jpg)
Python统一包仓库
![Page 11: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/11.jpg)
pypi
• http://pypi.python.org/pypi
• 国内镜像: http://pypi.douban.com/simple
• pip install package –i http://pypi.douban.com/simple
![Page 12: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/12.jpg)
上传自已开发的包
• 在pypi上注册用户
• 在自已的包中创建setup.py文件
• 通过python setup.py sdist打源码包
• 通过python setup.py sdist --formats=gztarupload上传至pypi或手工上传
![Page 13: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/13.jpg)
Python常用工具
![Page 14: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/14.jpg)
• virtualenv
• pip
• ipython
• nose
• git
![Page 15: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/15.jpg)
virtualenv
• 创建独立的python运行环境,适用于多Python环境,实现环境的隔离。
系统Python
env1 env2 env3
![Page 16: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/16.jpg)
virtualenv安装
将virtualenv下载后解压到一个目录,进入目录后执行 python setup.py install
自动就安装了 setuptools, pip
![Page 17: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/17.jpg)
环境创建
执行:virtualenv dir
将在dir目录下创建一个当前python版本的运行环境,还可以指定-p pythonhome来创建一个执行python环境的运行环境,其中有bin目录存放着生成的一些执行程序
![Page 18: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/18.jpg)
进入环境
执行:source <dir>/bin/activate
退出:deactivate
![Page 19: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/19.jpg)
ipython
Python交互环境增强,可以实现:
• Shell混用
• 代码提示
• 保存代码
![Page 20: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/20.jpg)
ipython notebook
• Ipython的Web客户端,可以实现非常强大的效果展示
![Page 21: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/21.jpg)
doctest
• Doctest是一种简单方便的测试方式,以交互的方式来测试
>>> import sys>>> def test_write():... sys.stdout.write("Hello\n")... return True>>> test_write()HelloTrue
python –m doctest test.txt
![Page 22: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/22.jpg)
nose
• 测试工具,可以支持多种python的测试代码写法
• nosetests –with-doctest xxx.py
![Page 23: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/23.jpg)
git
• 分布式的版本管理工具,常用命令:• git pull• git push• git add• git commit• git reset• git tag <tag>• git remote add repo_name repo_url• git branch –b <branch>• git checkout [-b] <branch>• git archive –o <outputfile> --prefix=<dir/> HEAD
![Page 24: 03.python工作环境](https://reader034.vdocuments.pub/reader034/viewer/2022052123/55a5c71a1a28abe66d8b4798/html5/thumbnails/24.jpg)
Q&A