Problem with modules reloading

Rami Chowdhury rami.chowdhury at gmail.com
Fri Mar 5 04:11:39 CET 2010


On Mar 4, 2010, at 18:10 , DSblizzard wrote:

> I have had already following question:
> "How to use *.py modules instead of *.pyc?"
> 
> Last time I when asked that question I resolved problem by using one
> big .py file. And answers doesn't work in my case. Now I ought to use
> several files and almost the same problem arises again. Admit I have 2
> modules: m2.py and m1.py
> 
> in m2.py:
> from m1.py import *
> 
> def foo2():
>  foo1()
> 
> foo2()
> 
> in m1.py:
> 
> # empty
> 
> After copying m2.py contents directly to python.exe:
> "NameError: global name 'foo1' is not defined"
> 
> Then I define foo1 in m1 and copy following lines to python.exe:
> from m1.py import *
> foo2()
> 

When you execute the import statement again, Python checks to see which module you are importing from, sees that you have already imported that module, and doesn't reload it. So nothing changes. If you need to reload m1, then use the reload() function:

reload(m1)

It's generally a good idea to avoid statements like "from m1 import *", to avoid confusion and keep your namespaces separate. So if you had defined m2.py like this...

import m1
def foo2():
  m1.foo1()

...then "reload(m1)" would solve your problem straight away.

> Nothing changes, the same error, and .pyc files, if I have deleted
> them doesn't created again.
> 
> What is the solution?
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list