python 不同包之间调用(包同级)
2018-03-23 16:24
423 查看
工程目录
. └── com ├── app │ ├── crawler02.py │ └── __init__.py ├── core │ ├── crawler_core.py │ └── __init__.py │ │ │ ├── crawler01.py ├── __init__.py │ └── tool
目的
crawler01 和 crawler02都需要调用crawler_core(下简称f)中的方法。
crawler01 是f的父级目录下的文件其调用方法是:
#!/usr/bin/env python #coding=utf-8 from core import crawler_core if __name__ == '__main__': url = "url" html = crawler_core.getHtml(url) print(html)
crawler02 是f的同级目录下的文件其调用方法是:
#!/usr/bin/env python #coding=utf-8 import os import sys sys.path.append(os.path.abspath(os.path.dirname(__file__)+'/'+'..')) from core import crawler_core url = "url" html = crawler_core.getHtml(url) print(html)
上面是把把当前python程序所在目录的父目录的绝对路径加入到环境变量PYTHON_PATH中。PYTHON_PATH是python的搜索路径,再引入模块时就可以从父目录中搜索得到了
crawler_core的代码:python3.X 后使用urllib.request
#!/usr/bin/env python #coding=utf-8 #简单爬虫实现 import urllib.request def getHtml(url): page =urllib.request.urlopen(url) html = "" for line in page.readlines(): html = html+str(line)+"\n" return html
相关文章推荐
- Python不同包之间调用注意事项
- Python 不同级目录之间模块的调用
- python项目中不同文件夹py源文件之间如何相互调用--Python import中相对路径的问题
- 使用DLL进行不同语言之间的调用
- win7安装ubuntu 14以及python环境,不同版本python之间切换(virturalenv)
- 【matlab】题目:(不同GUI之间控件的调用(原创))
- 『Python CoolBook』C扩展库_其五_C语言层面Python库之间调用API
- Python与C之间的相互调用(Python C API及Python ctypes库)【转】
- GPU(CUDA)学习日记(二)续------ VS2010 不同工程之间调用DLL
- Python 模块之间的调用
- 不同编码网站内容之间的JS调用方法
- 《同一个类中不同方法之间的调用相关问题(省略的类名或者this)》
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Java中不同的包之间的调用。。
- java不同类之间调用同一个常量
- python 不同版本之间的print
- 两个不同frame之间调用对方的办法
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- SendMessage在不同线程之间调用与本线程调用的区别
- python不同文件夹下的py文件调用