Python Notes (16) - bag

I. Introduction

If different people write the same module name how to do ? To avoid module name collisions, Python and the introduction of the method according to the organization directory module, called the package (Package)

Second, create a package of steps

1. Create a directory for the package name;
2. Create an information package __init__.py files are stored in the folder, the file content may be empty;
3. store script files as needed, the compiled extension and sub-package;
4 can import, import as, from import statements import modules and other packages;

[Python the root @ Code 6] # mkdir mypac setup packets # 
[@ Python the root Code 6] # CD mypac / 
[the root @ Python mypac] # Vim # hello.py a hello packet module 
DEF hello ():
     Print  " hello " 
[ Python mypac @ root] # vim world.py # build world module in the package 
DEF world ():
     Print  " world " 
[root @ Python mypac] # vim __init__.py # create __init__.py file and add the following to reference and hello world module (this file can also be empty, but when calling back the need to import mypac.hello and import mypac.world the package inside the module calls) 
Import hello
 Import world 
[root @ Python mypac]# CD .. 
[the root @ Python Code 6] # IPython 
    ... 
the In [ . 1]: Import mypac                 # call packet mypac 

the In [ 2]: mypac.hello.hello ()        # execution module package 
Hello 

the In [ . 3 ]: mypac.world.world () 
World 
[the root @ Python Code 6] # CD mypac / # into the package directory, you can see the corresponding .pyc files generated after the called 
[Python mypac the root @] # LS 
hello.py    the __init__ world.py .py 
hello.pyc   __init__ .pyc world.pyc

 

Guess you like

Origin www.cnblogs.com/vaon/p/11117884.html
Bag