zhangleivod 发表于 2017-4-19 12:01:58

Beginning Python, 3rd Edition(Chapter 10)

Batteries Included(CHAPTER 10)

1. tell your interpreter where to look for the module by executing the following.
        >>> import sys
        >>> sys.path.append('/home/zhanglei')# In UNIX, you cannot simply append the string '~/python' to sys.path. You must use the full path (such as '/home/yourusername/python')
        >>> sys.path #显示interpreter搜索的路径
2.you try to import it again,nothing happens.
        >>> import hello
        >>>
        They are mostly meant to define things, such as variables, functions, classes, and so on.
        And because you need to define things only once, importing a module several times has the same effect as
        importing it once.
3.hello2.py
        **************************
       #!/usr/bin/env python
       def hello2():
   print 'hello2'
        **************************
        >>> import hello2
        >>> hello2.hello()
4.hello3.py
        **************************
        #!/usr/bin/env python
        def hello():
    print 'hello,world!'
        # A test:
        hello()
**************************
5. pprint is a pretty-printing function, which makes a more intelligent printout.
        >>> import sys, pprint
        >>> pprint.pprint(sys.path)
        As long as your module is located in a place like site-packages('/usr/lib/python2.7/site-packages'), all your programs will be able to import it.
6.PYTHONPATH:
        The standard method is to include your module directory (or directories) in the environment variable PYTHONPATH(Environment variables are not part of the Python interpreter)
        export PYTHONPATH=/home/zhanglei/python
        Tab补全:pythonTab.py(/home/zhanglei/python)
        export PYTHONPATH=$PATHPATH:~/python
        import sys
        import readline
        import rlcompleter
        import atexit
        import os
        # tab completion
        readline.parse_and_bind('tab: complete')
        # history file
        histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
7.To make Python treat it as a package,it must contain a file named __init__.py.
        You can also nest packages inside other packages.
                File/Directory                                        Description
                ~/python/                                                Directory in PYTHONPATH
                ~/python/drawing/                                Package directory (drawing package)
                ~/python/drawing/__init__.py        Package code (drawing module)
                ~/python/drawing/colors.py                colors module
                ~/python/drawing/shapes.py                shapes module
       
        import drawing                                                # (1) Imports the drawing package
        import drawing.colors                                # (2) Imports the colors module
        from drawing import shapes                        # (3) Imports the shapes module
8.To find out what a module contains, you can use the dir function:
        dir(copy)
       
        (Several of these names begin with an underscore—a hint (by convention) that they aren’t meant to be used outside the module.)
9.        from copy import *
        you get only the four functions listed in the __all__ variable.
        If you don’t set __all__, the names exported in a starred import defaults to all global names in the module that don’t begin with an underscore
10.        help(copy.copy):
        A docstring is simply a string you write at the beginning of a function to document it
        >>> print(copy.copy.__doc__)

页: [1]
查看完整版本: Beginning Python, 3rd Edition(Chapter 10)