[d]怎么在python中调用python文件

akainixing 2012-09-24 04:45:01
我说的调用不是第一个调第二个的函数,而是调文件,
不是这个
#######################################
#文件1内容:

#FileName: test.py
def work():
print "Hello,world!"
文件保存为test.py
----------------------
#文件2内容:
#FileName: main.py
import test #将文件1 test.py导入进来
test.work() #调用test.py中定义的work()函数
----------------------
###############################################
而是
#############################
#文件1内容:
#FileName: test.py
print 'hello'
----------------------
#文件2内容:
#FileName: main.py
……#调用文件1----------------------
--------------------
Double行动:
原帖分数:30
帖子加分:30
...全文
207 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
荷兰人号 2012-09-29
  • 打赏
  • 举报
回复
__import__
akainixing 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 3 楼 的回复:

不用打开文件,直接给文件地址就行,用execfile()函数

execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file instead of a string. It is different fro……
[/Quote]
那3.x以后的怎么办啊?
qq120848369 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 5 楼 的回复:

引用 3 楼 的回复:

不用打开文件,直接给文件地址就行,用execfile()函数

execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file instead of a string. It ……
[/Quote]

open把代码读进来,然后传给exec。
qq120848369 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

不用打开文件,直接给文件地址就行,用execfile()函数

execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file instead of a string. It is different from the impor……
[/Quote]

可惜,我查了一下我的Python3.x手册,怨不着刚才没看到,已经废弃了:


execfile
Removes usage of execfile(). The argument to execfile() is wrapped in calls to open(), compile(), and exec().
qq120848369 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

不用打开文件,直接给文件地址就行,用execfile()函数

execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file instead of a string. It is different from the impor……
[/Quote]

恩, 是这个.
libralibra 2012-09-24
  • 打赏
  • 举报
回复
不用打开文件,直接给文件地址就行,用execfile()函数

execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file instead of a string. It is different from the import statement in that it does not use the module administration — it reads the file unconditionally and does not create a new module. [1]

The arguments are a file name and two optional dictionaries. The file is parsed and evaluated as a sequence of Python statements (similarly to a module) using the globals and locals dictionaries as global and local namespace. If provided, locals can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are passed as globals and locals, the code will be executed as if it were embedded in a class definition.

Changed in version 2.4: formerly locals was required to be a dictionary.

If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where execfile() is called. The return value is None.
qq120848369 2012-09-24
  • 打赏
  • 举报
回复
不过from xxx import *不是更好吗?
qq120848369 2012-09-24
  • 打赏
  • 举报
回复
exec(object[, globals[, locals]])
This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).

执行代码?open打开,读进来,然后调exec。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧