python模块runpy
2012-08-27 23:49
1111 查看
http://www.python.org/dev/peps/pep-0366/
http://www.python.org/dev/peps/pep-0338/
The runpy module
is used to locate and run Python modules without importing them first. Its main use is to implement the -m command
line switch that allows scripts to be located using the Python module namespace rather than the filesystem.
runpy模块用于:在不import python模块时,定位并执行该模块。主要用途在于实现命令行-m执行python 模块得效果,但是是在脚本中而不是文件系统上。
runpy一个就两个函数:
runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
运行指定模块代码并返回模块得全局字典。
例子:
runpy.run_path(file_path, init_globals=None, run_name=None)
执行指定脚本文件并返回模块全局字典。
例子:
首先写一个脚本
init_globals是传给运行module(文件)的字典,有四个全局变量是一定会传过去得:__name__, __file__, __loader__ and __package__
http://www.python.org/dev/peps/pep-0338/
The runpy module
is used to locate and run Python modules without importing them first. Its main use is to implement the -m command
line switch that allows scripts to be located using the Python module namespace rather than the filesystem.
runpy模块用于:在不import python模块时,定位并执行该模块。主要用途在于实现命令行-m执行python 模块得效果,但是是在脚本中而不是文件系统上。
runpy一个就两个函数:
runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
运行指定模块代码并返回模块得全局字典。
例子:
import runpy from pprint import pprint ttt = runpy.run_module('classproperty', alter_sys=True) pprint(ttt)
runpy.run_path(file_path, init_globals=None, run_name=None)
执行指定脚本文件并返回模块全局字典。
例子:
首先写一个脚本
$ cat a.py print 'iiiiiiiiiiiiinnnnnnnnnnnnnn a %s'%bb print __name__ print __file__ print __loader__ print __package__ if __name__ == '__main__': print 'in __main__'然后
import runpy from pprint import pprint bb = 'uuu' ggg = runpy.run_path('a.py', init_globals={'bb':bb}, run_name='__main__') print '=====================' pprint( ggg)
init_globals是传给运行module(文件)的字典,有四个全局变量是一定会传过去得:__name__, __file__, __loader__ and __package__
相关文章推荐
- Python中的模块和包概念介绍
- 利用C++ Boost编写扩展Python模块
- python time模块使用笔记(更新)
- python 浅析模块,包及其相关用法
- python模块——logging(日志管理)
- Python命令行参数解析模块optparse使用实例
- 4.3 Python获取模块名称
- Python random string模块
- Python安装模块出错(ImportError: No module named setuptools)解决方法
- 使用Python内置的模块与函数进行不同进制的数的转换
- Python-模块和包.深入Celery之子任务及原语组式/链式/回调
- Python使用内置json模块解析json格式数据的方法
- 【python tutorial 3.6】阅读笔记--【6.模块】
- Python os.path模块
- python web编程--CGI模块简单交互。。。
- Python中random模块用法实例分析
- Python的multiprocessing模块多进程用法和管理
- python 模块与包
- python logging模块学习
- 【Python】pickle模块