Problem with modules reloading

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Mar 4 23:01:21 EST 2010


On Thu, 04 Mar 2010 18:10:59 -0800, 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. 

I think you are confused.

.pyc files are Python compiled byte-code files. You don't use them, 
Python does. When you import a module, Python *automatically* compiles it 
and creates a .pyc file, which is then used to speed up loading next time 
you run Python and import the same module.

If you delete the .pyc file, it is quietly recreated next time you import 
the module again.

Using "one big .py file" or not, this has nothing to do with whether 
Python loads the module from source-code or byte-code. Just ignore 
the .pyc files.




> 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 *

That won't work, because Python looks for a package called "m1" which 
contains a module called "py". The only way that line can succeed is if 
you have a directory called "m1" containing two files called 
"__init__.py" and "py.py", like this:

m1/
m1/__init__.py
m1/py.py


Python will then load __init__.py, then load py.py. Since I'm pretty sure 
you don't have that directory, the above line cannot possible work.




> What is the solution?

Don't use "from module import *", it is confusing and doesn't work with 
reload(module) at all.

You shouldn't use reload(module) except for experimentation in the 
interactive interpreter.

You should work through the part of the tutorial that talks about modules 
and imports.

http://docs.python.org/tutorial/
http://docs.python.org/tutorial/modules.html


Good luck!


-- 
Steven




More information about the Python-list mailing list