您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 阳江分类信息网,免费分类信息发布

详解python自定义模块、包

2024/3/21 7:10:54发布11次查看
1、保存一个hello.py文件在f:/data/python目录下
hello.py
>>> def hello(x):    print x
目录
导入
>>> import sys>>> sys.path.append('f:/data/python') #添加路径,此添加是一次性的>>> import hello>>> hello.hello(5)  #调用5
2、不改变sys.path导入自定义模块:
第一种方法:将hello2.py文件放在d:/python27/lib/site-packages目录下
>>> import sys,pprint>>> pprint.pprint(sys.path) ['', 'd:\\python27\\lib\\idlelib', 'f:\\data\\s', 'd:\\python27', 'c:\\windows\\system32\\python27.zip', 'd:\\python27\\dlls', 'd:\\python27\\lib', 'd:\\python27\\lib\\plat-win', 'd:\\python27\\lib\\lib-tk', 'd:\\python27\\lib\\site-packages']>>> import hello2>>> hello2.hello() hello,world!
第二种方法:将f:/data/python路径添加到环境变量(在系统变量中,添加pythonpath变量,再将路径添加到值中)
>>> import sys>>> import hello>>> hello.hello(1)1
3、自定义包,导入模块
首先,创建如下目录,pytest为包,将路径f:\test添加到环境变量pythonpath中,pytest包中init.py文件是必要的,内容可以为空
>>> import sys>>> from pytest import hello>>> hello.hello(4)4
以上就是详解python自定义模块、包 的详细内容。
阳江分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录