Python跨文件引用模块
想作一個python模塊庫,自己時時調用。
文件層級結構
~/bin:
---pylib pybin
~bin/pylib:
---notify __init__.py
~bin//pybin:
---ct_weather.py
~/bin/pylib/notify
---ct_notifyGTK.py __init__.py
~bin/pylib/__init__.py
import notify __all__ = ['notify'] ~/bin/pylib/notify/__init__.py
import ct_notifyGTK
__all__ = ['ct_notifyGTK']
ct_notifyGTK.py
import gtk import pynotify notification = 0 def callback(icon):notification.show() def ct_notify(title, body, timeout = pynotify.EXPIRES_NEVER, type = 'notype'):pynotify.init(title)global notificationnotification = pynotify.Notification(title, body, type)notification.set_urgency(pynotify.URGENCY_NORMAL)notification.set_timeout(timeout)icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT)icon.connect('activate', callback)notification.attach_to_status_icon(icon)gtk.main() if __name__ == '__main__':ct_notify('weather', 'rain/n3oC') ct_weather.py
#import sys #sys.path.append('/home/ct/bin/pylib') from notify import ct_notifyGTK from urllib import urlopen import re p = re.compile('<[^>]+>') def ct_getweather(url = /"http://www.weather.com.cn/wap/weather/101010100.shtml"):'''ct_getweather: return the encoded in utf8 texts represents weathercondition.No description.'''sock = urlopen(url)htmlsource = sock.read()sock.close()content = p.sub("", htmlsource)content = '/n'.join(content.split()[1:12])return content.decode('utf8') #------end of ct_getweather() -------------------------------------------------- if __name__ == '__main__':weather = ct_getweather()ct_notifyGTK.ct_notify('Weather In Beijing', weather) #設置PYTHONPATH
#!/bin/bash
echo "export PYTHONPATH=~/bin/pylib" >> ~/.bashrc
?
?
總結
以上是生活随笔為你收集整理的Python跨文件引用模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件测试b s环境如何配置,B/S架构测
- 下一篇: mac网页转换pdf教程,在Mac系统中