简体   繁体   English

如何在python中取消缓存导入的模块?

[英]How can I uncache imported module in python?

This is really freaking me out. 这真让我感到震惊。 My project looks like this 我的项目看起来像这样

.
|-- main.py
|-- classes
|   |-- __init__.py
|   |-- exceptions.py

In those exceptions.py , I initally created one exception called SecurityError : 在这些exceptions.py ,我最初创建了一个称为SecurityError异常:

class SecurityError(Exception):
    pass   

I included the file in main.py using from classes.exceptions import * and SecurityError normally works when I just raise it. 我使用from classes.exceptions import *的文件将其包含在main.pyfrom classes.exceptions import *当我raiseraise时, SecurityError通常可以正常工作。

Afterwards, I also added FilesystemError , but whenever using it, I get: 之后,我还添加了FilesystemError ,但是无论何时使用它,我都会得到:

global name 'FilesystemError' is not defined 未定义全局名称“ FilesystemError”

I tried to delete *.pyc files in classes directory but it has no effect. 我试图删除classes目录中的*.pyc文件,但没有效果。 Any changes to the exceptions.py take no effect, including any ridiculous ones that make the file invalid: exceptions.py任何更改均无效,包括使文件无效的任何可笑的更改:

class FileFOO BAR BAZ systemError(Exception):
    pass

class SecurityErr foo bar bazor(Exception):
    pass    

The program still behaves as if the file never changed since the first run. 自第一次运行以来,该程序的行为就像文件从未更改过一样。 I don't think this behaviour has anything to do with intuitive programming - if there's cache, it should be in that directory. 我认为这种行为与直观的编程没有任何关系-如果有缓存,它应该在该目录中。

I am running the python directly from command line: 我直接从命令行运行python:

C:\programing\python\MyProject>py -2 main.py

Confusing errors of the form global name 'XXX' is not defined typically mean that you are shadowing a standard library module. global name 'XXX' is not defined形式的令人困惑的错误通常意味着您正在隐藏标准库模块。 You must rename your exceptions module. 您必须重命名exceptions模块。

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

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