lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

python:包和模块(python3)

 

 

 

 

一、包和模块的说明(python3)

 

  1、包:  包,就是存放‘模块’的‘文件夹’;‘包的名字’,就是‘此文件夹的名字’。每个‘包’中都包含文件‘__init__.py’,文件‘__init__.py’表明‘这个文件夹’是‘python的包’。

 

  2、模块:  模块,就是‘python的源文件’。模块的形式,如‘xx.py’。

 

  3、模块的使用:

    3.1、  import  包名.包名 ... .包名.模块名 

    3.2、  from  包名.包名 ... .包名.模块名  import   模块的部分内容  #模块的部分内容,指的是‘模块中的全局变量、函数、类等内容’。

 

 

 

 

二、模块的编写(包名:libs/test;模块名:testing)

 1 wit@on:libs$ pwd
 2 /home/wit/user/lidawei/tmp/python/libs
 3 wit@on:libs$ 
 4 wit@on:libs$ 
 5 wit@on:libs$ cat ../files/data.json
 6 {
 7         "id":["20230001","20230002", "2023003"],
 8         "name":["laohu", "laoying", "shizi"]
 9 }
10 wit@on:libs$ 
11 wit@on:libs$ 
12 wit@on:libs$ tree
13 .
14 ├── __init__.py
15 ├── __pycache__
16 │   └── __init__.cpython-310.pyc
17 ├── template
18 │   └── __init__.py
19 └── test
20     ├── __init__.py
21     ├── __pycache__
22     │   ├── __init__.cpython-310.pyc
23     │   └── testing.cpython-310.pyc
24     └── testing.py
25 
26 4 directories, 7 files
27 wit@on:libs$ 
28 wit@on:libs$ 
29 wit@on:libs$ cat  test/testing.py 
30 def info():
31         print("This is a testing modules. \n")
32 
33 wit@on:libs$ 
34 wit@on:libs$ 

 

 

 

 

三、模块的应用

 1 wit@on:libs$ pwd
 2 /home/wit/user/lidawei/tmp/python/libs
 3 wit@on:libs$ 
 4 wit@on:libs$ 
 5 wit@on:libs$ cat ../files/data.json
 6 {
 7         "id":["20230001","20230002", "2023003"],
 8         "name":["laohu", "laoying", "shizi"]
 9 }
10 wit@on:libs$ 
11 wit@on:libs$ 
12 wit@on:libs$ tree
13 .
14 ├── __init__.py
15 ├── __pycache__
16 │   └── __init__.cpython-310.pyc
17 ├── template
18 │   └── __init__.py
19 └── test
20     ├── __init__.py
21     ├── __pycache__
22     │   ├── __init__.cpython-310.pyc
23     │   └── testing.cpython-310.pyc
24     └── testing.py
25 
26 4 directories, 7 files
27 wit@on:libs$ 
28 wit@on:libs$ 
29 wit@on:libs$ cat  test/testing.py 
30 def info():
31         print("This is a testing modules. \n")
32 
33 wit@on:libs$ 
34 wit@on:libs$ 
35 wit@on:libs$ cd ..
36 wit@on:python$ 
37 wit@on:python$ 
38 wit@on:python$ cat filepy
39 #!/usr/bin/env python3
40 
41 
42 
43 import json
44 
45 
46 
47 file1 = './files/data.json'
48 fh1 = open(file1, "r")
49 db1 = fh1.read()
50 print(f'db1 := {db1}\n')
51 
52 
53 
54 # json part
55 python_data1 = json.loads(db1)
56 print(f'python_data := {python_data1}\n')
57 print(f'python_data["id"][0] := { python_data1["id"][0] }\n')
58 print(f'python_data["name"][0] := { python_data1["name"][0] }\n')
59 
60 
61 
62 fh1.close()
63 wit@on:python$ 
64 wit@on:python$ 
65 wit@on:python$ ./filepy
66 db1 := {
67         "id":["20230001","20230002", "2023003"],
68         "name":["laohu", "laoying", "shizi"]
69 }
70 
71 
72 python_data := {'id': ['20230001', '20230002', '2023003'], 'name': ['laohu', 'laoying', 'shizi']}
73 
74 python_data["id"][0] := 20230001
75 
76 python_data["name"][0] := laohu
77 
78 wit@on:python$ 
79 wit@on:python$ 

 

 

 

 

四、参考资料:

 

  1、python3模块|菜鸟教程:  https://www.runoob.com/python3/python3-module.html

 

posted on 2023-04-24 01:51  lnlidawei  阅读(71)  评论(0编辑  收藏  举报