简体   繁体   English

更新 python 中的导入文件

[英]Update to the imported file in python

I am trying to import a class which I have written in Python.我正在尝试导入我在 Python 中编写的 class。 Let's call it XYZ.我们称它为 XYZ。 I am trying to use this in a python script.我正在尝试在 python 脚本中使用它。

I import this in my script.py using:我将它导入到我的 script.py 中,使用:

from XYZ import XYZ
xyz = XYZ()
xyz.a()
xyz.b()

Now, If I try to update my XYZ class, its not reflected when I run script.py.现在,如果我尝试更新我的 XYZ class,它不会在我运行 script.py 时反映出来。 Is there a way to clear cache so that it looks at the latest XYZ everytime I run script.py?有没有办法清除缓存,以便每次运行 script.py 时它都会查看最新的 XYZ? I tried doing:我试着做:

reload(XYZ)

But that fails with:但这失败了:

TypeError: reload() argument must be a module TypeError: reload() 参数必须是一个模块

The imported module is recompiled each time you run script.py and reflect your changes.每次运行 script.py 并反映您的更改时,都会重新编译导入的模块。 reload is only required if you are running interactively.仅当您以交互方式运行时才需要reload If all fails try deleting the __pycache__ directory (or *.pyc files if using python 2).如果全部失败,请尝试删除 __pycache__ 目录(或 *.pyc 文件,如果使用 python 2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM