[Dynamic Language] Python非子包引用

时间:2021-01-22 06:28:02

Python非子包引用

python的搜索路径其实是一个列表(sys.path)

导入模块时python会自动去找搜索这个列表当中的路径,如果路径中存在要导入的模块文件则导入成功.

在项目中如果要引用同级包或另外目录的包(非子包)时:

  1. 将引用包路径加入sys.path
  2. 将引用包路径加入到*.pth文件内,并放入site.getsitepackages() 目录下.
  3. 当然也可以打包发布,安装到python的特定目录下.

测试代码

查看Python搜索路径列表

In [1]: import sys                                                                                                                                                                

In [2]: sys.path
Out[2]:
['',
'/Library/Frameworks/Python.framework/Versions/3.7/bin',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/extensions',]

代码目录

mac-abeen:page_pth_test abeen$ tree -L 3
├── dakjpka
│   ├── __init__.py
│   ├── pa
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   ├── pam.py
│   │   └── pam_rf.py
│   └── pb
│   └── pbm.py
└── tpackage.py

兄弟模块或者主运行程序与包同一级别或存在子包关系时

from package import module

import package

pam.py

   1 # -*- coding: utf-8 -*-
2
3
4 def pam_fun():
5 print('p.pa pam_fun')
6
7
8 __all__ = ['pam_fun']

pam_rf.py

   1 # -*- coding: utf-8 -*-
2
3 from pam import pam_fun
4
5 if __name__ == "__main__":
6 pam_fun()

同级或另外的包引用

  1. 直接添加到sys.path.append("package path")
  2. 使用*.pth文件,放到python的特定目录下.

查看python特定目录

In [3]: import site                                                                                                                                                               

In [4]: site.getsitepackages()
Out[4]: ['/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

增加目录内容

mac-abeen:pa abeen$ cat /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/dakj_project.pth
/Users/abeen/abeen/alearn/python/page_pth_test
/Users/abeen/abeen/ap/gitwork/dabda

增加后效果

In [5]: sys.path
Out[5]:
['',
'/Library/Frameworks/Python.framework/Versions/3.7/bin',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages',
**'/Users/abeen/abeen/alearn/python/page_pth_test',**
** '/Users/abeen/abeen/ap/gitwork/dabda',**
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/extensions',
'/Users/abeen/.ipython']

pbm.py 引用pam.py内的pam_fun如下

   1 # -*- coding: utf-8 -*-
2
3 # from dakjpka.pa import pam
4 import dakjpka.pa.pam as pam
5
6
7 def pbm_fun():
8 print('p.pbm pbm_fun')
| 9 pam.pam_fun()
10
11
12 if __name__ == '__main__':
13 pbm_fun()
14
15
16 __all__ = ['pbm_fun']

run

mac-abeen:pb abeen$ python3 pbm.py
p.pbm pbm_fun
p.pa pam_fun