Friday, December 26, 2014

Where to put libraries in python ?  Follow any of the following approach.

1. Current working directory

2. Set 'PYTHONLIB' environment variable at command line shell

3. In your script, do
    import sys
  sys.path.append(<directory location>)

4. Put in Python specific default lib locations. 
    e.g. /usr/lib/python3.3 

5. Create <anyname>.pth file with list of directory abs paths separated by new line. Where each path contains location of library files.
   import site
   site.addsitedir('/some/dir/you/want/on/the/path')
  

variable scope rule in python

variable scopes in python:
For a variable, python interpreter will access/search it starting from Local scope.
If it is not found in local scope, it will check in Enclosing defs as per following order.


L (local)
 -> E (enclosing def or lamba)
        -> G (global)
              -> B (built in)

import module vs ( from module import name1)

in python, statement like this one:

from module import name1, name2  # Copy these two names out (only)



is equivalent to this statement sequence:


import module                     # Fetch the module object
name1 = module.name1      # Copy names out by assignment
name2 = module.name2
del module


>> It means python interpreter will load entire module even if you want only few attributes of module.