调用模块中的导入 [英] Recalling import in module

查看:53
本文介绍了调用模块中的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习 python,在玩过 pygame 之后,我注意到我正在重新导入我已经导入的模块中的东西.

I'm still learning python and after playing around with pygame I noticed I'm re-importing things in modules I'm importing that I've already imported.

import pygame

例如,我在一个单独的文件中有一些类,但我也必须将 pygame 导入到该文件中才能使它们工作.它实际上导入了两次代码吗?它会减慢我的程序吗?或者它只是从缓存中提取相同的导入,但如果这样做了,我为什么需要再次导入它?

For instance I have some classes in a separate file, but I must also import pygame into that file too for them to work. Does it actually import the code twice? Will it slow down my program? Or does it just pull the same import from cache, but if it does that, why would I need to import it again?

lisp 中是否有类似 (load) 之类的东西,它只是将代码作为主文件的一部分来拉入?

Is there anything like (load) in lisp that just pulls in the code like it is part of the main file?

谢谢

推荐答案

知道了!
好的,我找到了我要找的东西.我只是发现必须重新导入我已经导入的代码很笨拙,尤其是当文件不是库或其他东西时,只需拆分代码即可.在这里找到:http://norvig.com/python-lisp.html

execfile("file.py")

完美地回答了我的问题.它解析一个文件并执行其中的代码.使用它我能够取出额外的导入语句并且它运行完美:D

Answers my problem perfectly. It parses a file and executes the code in it. Using it I was able to take out the extra import statements and it runs perfectly :D

这篇关于调用模块中的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆