重新导入模块 [英] re-importing modules

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

问题描述




在对保存在文本文件中的代码进行故障排除时,我经常发现我想要对它进行更改,重新保存,然后重新导入它。但是,只需

打字


导入myTestCode


似乎并不总是导入新版本。这应该是吗?我发现

现在我经常要关闭我的iPython窗口,然后重新打开它,

导入我最近修改过的代码。我知道这不是最好的方式来做这个,但我不知道是什么。


任何建议/帮助欢迎并且赞赏,

trevis

解决方案

4月30日下午12:49, "吨。起重机" < tcr ... @ REMOVETHISuiuc.eduwrote:





在对已保存的代码进行疑难解答时一个文本文件,我经常发现我想要对它进行更改,重新保存,然后重新导入它。但是,只需

打字


导入myTestCode


似乎并不总是导入新版本。这应该是吗?我发现

现在我经常要关闭我的iPython窗口,然后重新打开它,

导入我最近修改过的代码。我知道这不是最好的方式来做这个,但我不知道是什么。


任何建议/帮助欢迎并且赞赏,

trevis






今天另一个人发布了同样的事情。和那个人一样,你可能需要使用reload()函数
。查看此帖更多

详情:

http://www.python.org/search/hyperma...1993/0342.html


Mike


周一,2007-04-30 12:44 -0700, ky ****** @ gmail.com 写道:


4月30日下午12:49,T。起重机" < tcr ... @ REMOVETHISuiuc.eduwrote:





在对已保存的代码进行疑难解答时一个文本文件,我经常发现我想要对它进行更改,重新保存,然后重新导入它。但是,只需

打字


导入myTestCode


似乎并不总是导入新版本。这应该是吗?我发现

现在我经常要关闭我的iPython窗口,然后重新打开它,

导入我最近修改过的代码。我知道这不是最好的方式来做这个,但我不知道是什么。


任何建议/帮助欢迎并且赞赏,

trevis






另一个人今天发布了同样的事情。和那个人一样,你可能需要使用reload()函数
。查看此帖更多

详情:

http://www.python.org/search/hyperma...1993/0342.html


Mike



除了reload()不会递归重新加载重新加载模块所依赖的
模块的警告之外,还要警告重新加载

模块不会神奇地影响您可能持有的旧版本/ b $ b版本中的任何函数或对象。例如:


模块代码:

#dog.py

类狗(对象):

def bark(self):print" Arf!"


互动环节:


>>导入狗
d = dog.dog()
d.bark()



Arf!


>>#现在模块代码正在另一个窗口中更改...
重装(狗)



< module''dog''来自''dog.py''>


>>#一个新的狗实例现在会说Woof:
d2 = dog.dog()
d2.bark()



Woof!


>>#但是之前的狗实例仍然说Arf:
d.bark()



Arf!


这可能是也可能不是对你来说是一个问题,但最重要的是

reload必须谨慎使用。


-Carsten


4月30日下午3:00,Carsten Haese< cars ... @ uniqsys.comwrote:


On Mon,2007-04 -30于12:44 -0700,kyoso ... @ gmail.com写道:


4月30日下午12:49,T。起重机" < tcr ... @ REMOVETHISuiuc.eduwrote:



< blockquote class =post_quotes>
在对保存在文本文件中的代码进行故障排除时,我经常发现我想要对它进行更改,重新保存,然后重新进口。但是,只需

输入


import myTestCode


似乎并不总是导入较新的版本。这应该是吗?我发现

现在我经常要关闭我的iPython窗口,然后重新打开它,

导入我最近修改过的代码。我知道这不是最好的方式来做这个,但我不知道是什么。


任何建议/帮助欢迎和赞赏,

trevis




另一个人发布了同样的信息今天的事情。和那个人一样,你可能需要使用reload()函数
。查看此帖更多

详情:

http://www.python.org/search/hyperma...1993/0342.html


Mike



除了警告reload()不会递归重新加载

模块重新加载的模块依赖于,被警告说重新加载一个

模块不会神奇地影响你可能持有的旧版本的b / b版本中的任何函数或对象。例如:


模块代码:

#dog.py

类狗(对象):

def bark(self):print" Arf!"


互动环节:>>导入狗


> d = dog.dog()
d.bark()



Arf!


>#现在模块代码正在另一个窗口中更改...
reload(dog)



< module''dog''来自''dog.py''>>>#一个新的狗实例现在会说Woof:


> d2 = dog.dog()
d2.bark()



Woof!


>#但是来自之前的狗实例仍然说Arf:
d.bark()



Arf!


这对你来说可能是也可能不是问题,但最重要的是

reload必须谨慎使用。


-Carsten



Carsten,


非常好点。我不记得曾见过之前解释过的。

谢谢!


Mike

Hi,

When troubleshooting code that''s saved in a text file, I often find that I
want to make a change to it, re-save it, then reimport it. However, just
typing

import myTestCode

doesn''t always seem to import the newer version. Is it supposed to? I find
that right now I often have to close my iPython window, then reopen it and
import my recently modified code. I know this can''t be the best way to do
this, but I don''t know what is.

any suggestions/help welcome and appreciated,
trevis


解决方案

On Apr 30, 12:49 pm, "T. Crane" <tcr...@REMOVETHISuiuc.eduwrote:

Hi,

When troubleshooting code that''s saved in a text file, I often find that I
want to make a change to it, re-save it, then reimport it. However, just
typing

import myTestCode

doesn''t always seem to import the newer version. Is it supposed to? I find
that right now I often have to close my iPython window, then reopen it and
import my recently modified code. I know this can''t be the best way to do
this, but I don''t know what is.

any suggestions/help welcome and appreciated,
trevis

Hi,

Another person posted the same thing today. As with that person, you
probably need to use the reload() function. See this post for more
details:

http://www.python.org/search/hyperma...1993/0342.html

Mike


On Mon, 2007-04-30 at 12:44 -0700, ky******@gmail.com wrote:

On Apr 30, 12:49 pm, "T. Crane" <tcr...@REMOVETHISuiuc.eduwrote:

Hi,

When troubleshooting code that''s saved in a text file, I often find that I
want to make a change to it, re-save it, then reimport it. However, just
typing

import myTestCode

doesn''t always seem to import the newer version. Is it supposed to? I find
that right now I often have to close my iPython window, then reopen it and
import my recently modified code. I know this can''t be the best way to do
this, but I don''t know what is.

any suggestions/help welcome and appreciated,
trevis


Hi,

Another person posted the same thing today. As with that person, you
probably need to use the reload() function. See this post for more
details:

http://www.python.org/search/hyperma...1993/0342.html

Mike

In addition to the warning that reload() does not recursively reload
modules that the reloaded module depends on, be warned that reloading a
module does not magically affect any functions or objects from the old
version that you may be holding on to. For example:

Module code:
# dog.py
class dog(object):
def bark(self): print "Arf!"

Interactive session:

>>import dog
d = dog.dog()
d.bark()

Arf!

>># Now the module code is being changed in another window...
reload(dog)

<module ''dog'' from ''dog.py''>

>># A new dog instance will now say Woof:
d2 = dog.dog()
d2.bark()

Woof!

>># But the dog instance from before still says Arf:
d.bark()

Arf!

This may or may not be a problem for you, but the bottom-line is that
reload must be used with caution.

-Carsten


On Apr 30, 3:00 pm, Carsten Haese <cars...@uniqsys.comwrote:

On Mon, 2007-04-30 at 12:44 -0700, kyoso...@gmail.com wrote:

On Apr 30, 12:49 pm, "T. Crane" <tcr...@REMOVETHISuiuc.eduwrote:

Hi,

When troubleshooting code that''s saved in a text file, I often find that I
want to make a change to it, re-save it, then reimport it. However, just
typing

import myTestCode

doesn''t always seem to import the newer version. Is it supposed to? I find
that right now I often have to close my iPython window, then reopen it and
import my recently modified code. I know this can''t be the best way to do
this, but I don''t know what is.

any suggestions/help welcome and appreciated,
trevis

Hi,

Another person posted the same thing today. As with that person, you
probably need to use the reload() function. See this post for more
details:

http://www.python.org/search/hyperma...1993/0342.html

Mike


In addition to the warning that reload() does not recursively reload
modules that the reloaded module depends on, be warned that reloading a
module does not magically affect any functions or objects from the old
version that you may be holding on to. For example:

Module code:
# dog.py
class dog(object):
def bark(self): print "Arf!"

Interactive session:>>import dog

>d = dog.dog()
d.bark()

Arf!

># Now the module code is being changed in another window...
reload(dog)


<module ''dog'' from ''dog.py''>>># A new dog instance will now say Woof:

>d2 = dog.dog()
d2.bark()

Woof!

># But the dog instance from before still says Arf:
d.bark()


Arf!

This may or may not be a problem for you, but the bottom-line is that
reload must be used with caution.

-Carsten

Carsten,

Very good point. I don''t recall ever seeing that explained before.
Thanks!

Mike


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

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