Use of Python module and import method of self built module

Module usage
Modular:

Modular pyth1.on basic way to organize code

A python script can be run by itself or imported into another script. When the script is imported and run, we call it module.

All point p files can be imported as a module

The module name is the same as the file name of the script. For example, if we write a script named hello.pv, we can import it with import hello.py in another script.

Bag:

python modules can be organized into packages by directory

To create a package:

Create a directory named package name, create a "init. Py" file in the directory, and store script files or compiled extensions and subpackages in the directory as needed.

Syntax:

import pack.m1,pack.m2,pack.m3
sys.path

//Give an example:

In [1]: import sys

In [2]: sys.path

Out[2]:

['',

'/usr/bin',

'/usr/lib64/python27.zip',

'/usr/lib64/python2.7',

'/usr/lib64/python2.7/plat-linux2',

'/usr/lib64/python2.7/lib-tk',

'/usr/lib64/python2.7/lib-old',

'/usr/lib64/python2.7/lib-dynload',

'/usr/lib64/python2.7/site-packages',

'/usr/lib64/python2.7/site-packages/gtk-2.0',

'/usr/lib/python2.7/site-packages',

'/usr/lib/python2.7/site-packages/IPython/extensions',

'/root/.ipython']

vim /root/.bashrc

//Finally, add export PYTHONPATH=/root/library

//Successfully added to Python environment variable

In [1]: import sys

In [2]: sys.path

Out[2]:

['',

'/usr/bin',

 '/root/library',

'/usr/lib64/python27.zip',

'/usr/lib64/python2.7',

'/usr/lib64/python2.7/plat-linux2',

'/usr/lib64/python2.7/lib-tk',

'/usr/lib64/python2.7/lib-old',

'/usr/lib64/python2.7/lib-dynload',

'/usr/lib64/python2.7/site-packages',

'/usr/lib64/python2.7/site-packages/gtk-2.0',

'/usr/lib/python2.7/site-packages',

'/usr/lib/python2.7/site-packages/IPython/extensions',

'/root/.ipython']

//Scripts that can be called by other scripts:

#!/usr/bin/env python

#

def wordCount(s):

    chars = len(s)

    words = len(s.split())

    lines = s.count('\n')

    print lines,words,chars

if __name__=='__main__':  #After setting, the functions in this script can be called test.py by other scripts

    s = open('/etc/passwd').read()

    wordCount(s)

#

[root[@localhost](https://my.oschina.net/u/570656) 20181203]# python words.py

27 52 1324

[root[@localhost](https://my.oschina.net/u/570656) 20181203]# wc /etc/passwd

27   52 1324 /etc/passwd

test.py Import in words.py:

[root[@localhost](https://my.oschina.net/u/570656) 20181203]# cat test.py

#!/usr/bin/env python

import words

#

s="""hello world python"""

words.wordCount(s)

[root[@localhost](https://my.oschina.net/u/570656) 20181203]# python test.py

27 52 1324

[root[@localhost](https://my.oschina.net/u/570656) 20181203]# ls

test.py  words.py  words.pyc   ###Automatically generate compiled files after calling words.

Modules and packages:
Module is a script file that can be imported

Packages are modules and sub packages organized by directory. There is init.py file in the directory, which can store package information.

Syntax for importing modules and packages:

import,import as

from ... import ...
In [2]: from mode import mode
2 10 158
In [3]: mode.wordCount('abc')
0 1 3

In [4]: import mode.mode
In [5]: mode.mode.wordCount('hello')
0 1 5

In [7]: from mode.mode import wordCount
In [8]: wordCount('aaa')
0 1 3

#Rename wc
In [10]: from mode.mode import wordCount as wc
In [11]: wc('aaaddd')
0 1 6

#from package.mod import wordCount
#from package import mod

In [10]: from package import mod

In [11]: mod.wordCount('abc 123')
0 2 7

In [15]: from package.mod import wordCount

In [16]: wordCount('ava adsf34')
0 2 10

Tags: Python IPython vim

Posted on Fri, 01 Nov 2019 07:03:43 -0700 by vigge89