Posts

Showing posts from December, 2018

Executable directories and compressed executable directories in python

Executable directories is main entry point when the package is executed. This can be used to distribute a huge python program to wider user to execute package. Example: ~/py$ ls -ld directory0 drwxr-xr-x 3 nansari nansari 4096 Jan  1 09:32 directory0 ~/py$ python directory0 /home/nansari/anaconda3/bin/python: can't find '__main__' module in 'directory0' ~/py$ ~/py$ touch directory0/__main__.py ~/py$ python directory0 ~/py$ ~/py$ cat >>directory0/__main__.py print('__main__ from directory0 is being executed now') ~/py$ python directory0 __main__ from directory0 is being executed now ~/py$ Compressed Excitable Directory ~/py$ cd directory0 ~/py/directory0$ zip -r ../directory0.zip *   adding: __main__.py (stored 0%)   adding: __pycache__/ (stored 0%)   adding: __pycache__/__main__.cpython-37.pyc (deflated 22%) ~/py/directory0$ cd .. ~/py$ python directory0.zip __main__ from directory0 is being executed now ~/py$ Even .zip extension can be removed, sti...

Namespace packages in Python

Namespace packages are package that has several logical sub directories inside a module to organize a large module. They do not have __init__.py to avoid complex initialization ordering problems. hence, they can not have package level initialization code. Nothing will be executed when it is imported. How does loading of normal and namespace packages works? Pyhton scans all paths in sys.path if  a directory with __init__.py is found, it is loaded a normap package if any file with .py extension, that can act as moduke, is found, it is loaded Finally, all matching directories in sys.path ares considered as namespace package, and one that matches as import name, will be loaded as namespace package Example: mkdir directory0 directory1 mkdir directory0/travel_mode mkdir directory1/travel_mode mkdir directory1/travel_mode/ships mkdir directory0/travel_mode/trains python>>> import sys sys.path.extend(['directory0','directory1']) sys.path Out[11]: [...

What is __all__ in python module?

__all__ attribure of a module controls list of attributes are imported by "from module import *". If it is not specified, the import will imports all public names from the imported module. __all__ must be a list of strings. Each string indicate a name that will be imported. For example, in below example,importing 'from urllib3 import *' has imported get_host function in local name space. $ python Python 3.7.1 (default, Dec 14 2018, 19:28:38) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> from urllib3 import * >>> >>> locals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-...

Where does python look for to load a module or package?

-python looks module or packages in each directory listed in sys.path list. First element is empty string. It means, python search module in current directory first. -Add directory where package is residing in PYTHONPATH environment variable (format is similar to PATH variable) import sys sys.path Out[10]: ['',  '/home/nansari',  '/home/nansari/pylearning',  '/home/nansari/anaconda3/lib/python36.zip',  '/home/nansari/anaconda3/lib/python3.6',  '/home/nansari/anaconda3/lib/python3.6/lib-dynload',  '/home/nansari/anaconda3/lib/python3.6/site-packages',  '/home/nansari/anaconda3/lib/python3.6/site-packages/IPython/extensions',  '/home/nansari/.ipython']

What's difference between python package and module?

MODULE Python basic tool for organizing code is the module : mm.py Module typically corresponds to a single source file. You load module by import keyword : import mm imported module is represented by an object of type module : type(mm) PACKAGE Package is a special type of module that contains other modules and packages to group modules with similar functionality. package directory has a __init__.py. It is package init file that makes it an module too. __init__.py is executed when package is imported  Package have __path__ but module does not have many python libraries. Package is just a directory on filesystem having a __init__.py whereas module is a single python file. Module object of package has __path__ attribute but a module that is not package too, will not have __path__ attribute __file__ attribute of package returns __init__.py absolute path. Where as it returns module's file name for package. In below example urllib is package and request is module ...