Creating a package in Python

Make a package root directory

$work_dir=/tmp
mkdir $work_dir/my_py_pakage
touch  $work_dir/my_py_pakage/__init__.py 

my_py_pakage is a package; init file that makes the package a python module 


If a path is not in sys.path, add it

import sys
sys.path.append('/tmp')

Now you can import your package

import mypackage
type(mypackage)
<class module>
dir(mypackage)
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__']


See some information for you  module

mypackage.__file__
mypackage.__path__
mypackage.__specs__



-packages are a special type of module
-unlike normal module, packages can contain other modules and other packages
-package has __path__ variable showing path from which package is loaded
-sys.path is a list where moduled will be searched
-a package is implemented by creating __init__.py. It executes when the package is imported

-A namespace package is a package split into several directories ( with the same name). All such directory should be in sys.path. They do not use __init__py

-An executable directory is created by creating __main__.py in a directory. When an executable directory is executed as python CLI argument, its __main__ attribute is set __main__. When __main__ executes, its parent directory automatically appended in sys.path

-The executable directory can me compressed and executed as an argument to python CLI. It is a convenient way to distribute python program




Comments

Popular posts from this blog

Python programming language : quick and easy recipes

How does yaml data look when converted in python data structure?