python 自定义模块怎么用_python自定义模块使用说明

保存一个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

不改变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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值