Python重新加载文件 [英] Python reload file

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

问题描述

我有一个可以计算内容的脚本.它使用来自单独文件"inputs.py"的输入.

I have a script that computes some stuff. It uses inputs from a separate file 'inputs.py'.

在"inputs.py"中只有几个变量:

In 'inputs.py' are only a few variables:

A = 2.3
B = 4.5
C = 3.0

在主文件中,我将其导入

In the main file I import them with

from inputs import *

如果我现在更改"inputs.py"中的内容并再次执行脚本,它将仍然使用旧值而不是新值.如何重新加载文件?

If I now change something in 'inputs.py' and execute the script again it still uses the old values instead of the new ones. How can I reload the file?

reload(inputs)

不起作用.

非常感谢!

推荐答案

如果您使用的是Python 3.x,则要重新加载使用从模块导入名称中导入的名称,您将需要做-

If you are using Python 3.x , then to reload the names that have been imported using from module import name , you would need to do -

import importlib
import inputs #import the module here, so that it can be reloaded.
importlib.reload(inputs)
from inputs import A # or whatever name you want.

对于Python 2.x,您只需--p

For Python 2.x , you can simply do -

import inputs   #import the module here, so that it can be reloaded.
reload(inputs)
from inputs import A # or whatever name you want.

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

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