Installing with pip

We'll demonstrate how to use pip by installing the nose testing tool. The nose command  is a sort of power-tool for running unittest based tests such as those we developed in Chapter 10, Unit testing with the Python standard library. One really useful thing it can do is discover all of your tests and run them. This means that you don't need to add unittest.main() into your code; you can just use nose to find and run your tests.

First though, we need to do some groundwork. Let's create a virtual environment (see Appendix B, Pakaging and Distribution) so we don't inadvertently install into our system Python installation. Create a virtual environment using pyenv, and activate it:

$ python3 -m venv test_env
$ source activate test_env/bin/activate
(test_env) $

As pip is updated much more frequently than Python itself, it's good practice to upgrade pip in any new virtual environment, so let's do that. Fortunately, pip is capable of updating itself:

(test_env) $ pip install --upgrade pip
Collecting pip
Using cached pip-8.1.2-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-8.1.2

If you don't upgrade pip it will give you warnings every time you use it, if a new version has become available since you last upgraded.

Now let's use pip to install nose. pip uses subcommands to decide what to do, and to install modules you use pip install package-name:

(test_env) $ pip install nose
Collecting nose
Downloading nose-1.3.7-py3-none-any.whl (154kB)
100% |████████████████████████████████| 163kB 2.1MB/s
Installing collected packages: nose
Successfully installed nose-1.3.7

If this succeeds, nose is ready to use in our virtual environment. Let's check that it's available by trying to import it at the REPL and instrospecting the path at which it is installed:

(test_env) $ python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nose
>>> nose.__file__
'/Users/sixty_north/.virtualenvs/test_env/lib/python3.5/site-packages/nose/__init__.py'

As well as installing a module, nose installs the nosetests program in the bin directory of the virtual environment. To really put the icing on the cake, let's use nosetests to run the tests from palindrome.py from Chapter 11, Debugging with PDB:

(test_env) $ cd palindrome
(test_env) $ nosetests palindrome.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset