namespace problems

K

Kiran

Hi all, I am trying to get the following to work, but cant seem to do
it the way i want to.

ok, so I come into python and do the following:

then, i have a file called test.py in which i say:
print x

Now, after having defined x =1 in the python interpreter, i come in and
say:
import test
it has a problem, which i can understand, because it is in its own
namespace,
but even if i say:
from test import *
the interpreter still gives me the error:
NameError: name 'x' is not defined

the only way i can get it to work is if i say: execfile('./test.py')
how can i get this to work by importing and not execfile?

thanks for your help!
 
D

Dennis Lee Bieber

from test import *
the interpreter still gives me the error:
NameError: name 'x' is not defined
Which is still true -- you never defined "x" INSIDE test.py
the only way i can get it to work is if i say: execfile('./test.py')
how can i get this to work by importing and not execfile?
You don't...

The "x = 1" in the interpreter is NOT seen /in/ the imported module.
The maligned "from/import *" only makes all names from the imported
module visible to the importing module -- it does NOT make names in the
importing module visible to the imported module (in your sample there
are no names to make visible).

The "print x" inside of "test.py", if fully qualified, would be:
print test.x


Now, this only works if imported by the main module... as you'll see
in a moment:

mytest.py:
import __main__
print __main__.x
thanks for your help!
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Terry Reedy

Kiran said:
Hi all, I am trying to get the following to work, but cant seem to do
it the way i want to.

ok, so I come into python and do the following:


then, i have a file called test.py in which i say:
print x

Now, after having defined x =1 in the python interpreter, i come in and
say:
import test
it has a problem, which i can understand, because it is in its own
namespace,
but even if i say:
from test import *
the interpreter still gives me the error:
NameError: name 'x' is not defined

the only way i can get it to work is if i say: execfile('./test.py')
how can i get this to work by importing and not execfile?

Dennis gave you one way: import __main__ into test.
Another way to come close is to put the print inside a function.

test.py
---------
def p(): print x

Then your script could say

import test
test.x = 1
test.p()

If you don't already, you need to know that import only executes the code
for a module the *first* time you import it. So normally, top level code
in modules consists of function and class definitions and perhaps the
initialization of a few module-level constants or variables.

Terry Jan Reedy
 

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

Similar Threads


Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,331
Latest member
ElaneLyttl

Latest Threads

Top