首页 > 代码库 > Learn Python the Hard Way--Exercise 46

Learn Python the Hard Way--Exercise 46

0. 缘起

    《Learn Python the Hard Way》Exercise 46 要求安装四个python package pip, distribute, nose, virtualenv,(原书作者特别提醒: Do not just donwload these packages and install them by hand. Instead see how other people recommend you install these packages and use them for your particular system.)以完成练习。

0.1 学习目标

    研究这个例子:1)搞明白书中需要做什么事;2)这件事现在用工具应该怎么做。

1. 这些包是做什么的

    该练习安装包,给一个project搭建一个虚拟环境,测试所建project。

    为安装包(installing packages),需要安装pip, setuptools(distribute的升级替代品),同时可以选择(optionally)搭建一个虚拟环境virtualenv。术语解释参看 Glossary。

    留意术语解释中的Python Packaging Authority (PyPA)和Python Package Index (PyPI)。后者可以查到python的所有包,当然也包括练习中的5个包的详细说明。

1.1 pip
1.1.1 是什么

    1) pip:官方推荐安装PYTHON 包的工具(The PyPA recommended tool for installing Python packages.)。
    2) pip 支持版本控制(supports installing from PyPI, version control, local projects, and directly from distribution files)。
    3) Pip 就是能将Python包安装到电脑上的东西(is a thing that installs packages, pip itself is a package that someone might want to install, especially if they‘re looking to run this get-pip.py script. pip)。

1.1.2 安装方法

    1) 下载get-pip.py,python get-pip.py
    2) 与之相对的是sudo apt-get install python-pip (Ubuntu) 或 sudo yum install python-pip (Fefora)     

1.2 setuptools
1.2.1 是什么

    setuptools 使得包下载、编译(?)、安装、升级、卸载变得很容易(Easily  download, build, install, upgrade, and uninstall Python packages)。

    安装pip时,如果没安装setuptools,会自动安装。

1.3 virtualenv

    virtualenv支持将python包安装到局部环境中,而不必影响到全局环境。

    Python “Virtual Environments” allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally.

1.4. nose

    nose扩展了单元测试,简化了测试工作(extends the test loading and running features of unittest, making it easier to write, find and run tests.)。

    因此,该练习要我们做的事情就是:

    1) 安装pip(同时自动安装setuptools);

    2)安装virtualenv(没有将project的目录设为虚拟目录。虚拟目录在本例中似未用到)

    3)安装并用nose测试project。

2. 练习

2.1 安装

 

技术分享
 1 $ sudo python get-pip.py  2 [sudo] password for fred:  3 The directory /home/fred/.cache/pip/log or its parent directory is not owned by the current user and the debug log has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. 4 The directory /home/fred/.cache/pip/http or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. 5 The directory /home/fred/.cache/pip/http or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. 6 Collecting pip 7   Downloading pip-6.0.7-py2.py3-none-any.whl (1.3MB) 8     100% |################################| 1.3MB 31kB/s  9 [?25hCollecting setuptools10   Downloading setuptools-12.0.5-py2.py3-none-any.whl (502kB)11     100% |################################| 503kB 27kB/s 12 [?25hInstalling collected packages: setuptools, pip13 14 15 Successfully installed pip-6.0.7 setuptools-12.0.5
shell安装pip,并自动安装setuptools
技术分享
 1 ~$ sudo pip install virtualenv 2 [sudo] password for fred:  3 The directory /home/fred/.cache/pip/log or its parent directory is not owned by the current user and the debug log has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. 4 The directory /home/fred/.cache/pip/http or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. 5 The directory /home/fred/.cache/pip/http or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. 6 Collecting virtualenv 7   Downloading virtualenv-12.0.6.tar.gz (1.8MB) 8     100% |################################| 1.8MB 55kB/s  9 [?25hInstalling collected packages: virtualenv10   Running setup.py install for virtualenv11     Installing virtualenv script to /usr/local/bin12     Installing virtualenv-2.7 script to /usr/local/bin13 Successfully installed virtualenv-12.0.6
shell 安装virtualenv
技术分享
1 ~$ sudo -H pip install nose2 [sudo] password for fred: 3 Collecting nose4   Downloading nose-1.3.4-py2-none-any.whl (154kB)5     100% |################################| 155kB 600kB/s 6 [?25hInstalling collected packages: nose7 8 Successfully installed nose-1.3.4
shell 安装 nose
2.2 执行
技术分享
 1 ~/mystuff/myprojects/skeleton$ ls -R 2 .: 3 bin  docs  NAME  setup.py  tests 4 ./bin: 5 ./docs: 6 ./NAME: 7 __init__.py  __init__.pyc 8 ./tests: 9 __init__.py  __init__.pyc  NAME_tests.py  NAME_tests.pyc10 fred@T420i:~/mystuff/myprojects/skeleton$ nosetests11 .12 ----------------------------------------------------------------------13 Ran 1 test in 0.009s14 OK
执行nosetests,附project目录结构

3. 小结

    练习完成。

    知道了pip, setuptools, 需要python package时,知道哪里能找到,找到了怎么安装。

    了解了virtualenv, nosetests。具体如何使用还要继续学习。

 

Learn Python the Hard Way--Exercise 46