Problem with modules reloading

D

DSblizzard

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()

Nothing changes, the same error, and .pyc files, if I have deleted
them doesn't created again.

What is the solution?
 
R

Rami Chowdhury

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.
 
S

Steven D'Aprano

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top