Listing variables

V

vsoler

Say that a have:

# file test.py
a=7


At the prompt:
import test
dir()

I would like to see the variables created in the test namespace.
However, variable "a" does not appear in the list, only "test". Since
I know that var "a" is reachable from the prompt by means of test.a,
how can I list this sort of variables?

Vicente Soler
 
T

Tim Chase

Say that a have:
# file test.py
a=7


At the prompt:
import test
dir()

I would like to see the variables created in the test namespace.
However, variable "a" does not appear in the list, only "test". Since
I know that var "a" is reachable from the prompt by means of test.a,
how can I list this sort of variables?

dir(test)

works for any scope you want (except in some C modules...was
peeved at mod_python for this reason when I was playing with it a
while back). I use this for debugging all the time:

dir(foo.bar.whatever)

or if I want to remember some less-used method on a string/list/dict:

dir("")
dir([])
dir({})

-tkc
 
D

Dave Angel

vsoler said:
Say that a have:

# file test.py
a=7


At the prompt:
import test
dir()

I would like to see the variables created in the test namespace.
However, variable "a" does not appear in the list, only "test". Since
I know that var "a" is reachable from the prompt by means of test.a,
how can I list this sort of variables?

Vicente Soler
dir(test)
 
V

vsoler

Say that a have:
# file test.py
a=7
At the prompt:
import test
dir()
I would like to see the variables created in the test namespace.
However, variable "a" does not appear in the list, only "test". Since
I know that var "a" is reachable from the prompt by means of test.a,
how can I list this sort of variables?

   dir(test)

works for any scope you want (except in some C modules...was
peeved at mod_python for this reason when I was playing with it a
while back).  I use this for debugging all the time:

   dir(foo.bar.whatever)

or if I want to remember some less-used method on a string/list/dict:

   dir("")
   dir([])
   dir({})

-tkc

Tim,

If I just input dir(test) I don't get "a" in my list.
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__']
I am using python 2.6

Am I doing anything wrong?
 
T

Tim Chase

If I just input dir(test) I don't get "a" in my list.
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__']

I am using python 2.6

Am I doing anything wrong?


Are you importing the module you think you are?

tim@rubbish:~/tmp$ echo "a=42" > test.py
tim@rubbish:~/tmp$ python2.5['__builtins__', '__doc__', '__file__', '__name__', 'a']


Granted this is 2.5 (the most current I have on my Debian box,
but I also tested in 2.3 and 2.4 which are also installed)
instead of 2.6 but they should all behave the same. If I remove
test.py/test.pyc, I get the following:

tim@rubbish:~/tmp$ rm test.py test.pyc
tim@rubbish:~/tmp$ python2.5
>>> import test
>>> dir(test) ['__builtins__', '__doc__', '__file__', '__name__', '__path__']
>>> test.__file__
'/usr/lib/python2.5/test/__init__.pyc'

because there's apparently a module named "test" in the standard
distribution that gets found instead.

-tkc
 
V

vsoler

If I just input dir(test) I don't get "a" in my list.
import test
dir(test)
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__']
I am using python 2.6
Am I doing anything wrong?

Are you importing the module you think you are?

tim@rubbish:~/tmp$ echo "a=42" > test.py
tim@rubbish:~/tmp$ python2.5
 >>> import test
 >>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', 'a']

Granted this is 2.5 (the most current I have on my Debian box,
but I also tested in 2.3 and 2.4 which are also installed)
instead of 2.6 but they should all behave the same.  If I remove
test.py/test.pyc, I get the following:

tim@rubbish:~/tmp$ rm test.py test.pyc
tim@rubbish:~/tmp$ python2.5
 >>> import test
 >>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', '__path__']
 >>> test.__file__
'/usr/lib/python2.5/test/__init__.pyc'

because there's apparently a module named "test" in the standard
distribution that gets found instead.

-tkc

Tim,

You were right. When I renamed my test.py file into test77.py it
worked perfectly well. Thank you.

Is there a way to know which test.py it was importing?
 
T

Tim Chase

tim@rubbish:~/tmp$ rm test.py test.pyc
tim@rubbish:~/tmp$ python2.5
import test
dir(test)
['__builtins__', '__doc__', '__file__', '__name__', '__path__']
test.__file__
'/usr/lib/python2.5/test/__init__.pyc'

because there's apparently a module named "test" in the standard
distribution that gets found instead.

You were right. When I renamed my test.py file into test77.py it
worked perfectly well. Thank you.

Is there a way to know which test.py it was importing?

well, as my simple code showed, you can check test.__file__ or
test.__path__ if you're curious. Python just searches through
your $PYTHONPATH which you can determine at runtime via sys.path

-tkc
 
V

vsoler

tim@rubbish:~/tmp$ rm test.py test.pyc
tim@rubbish:~/tmp$ python2.5
 >>> import test
 >>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', '__path__']
 >>> test.__file__
'/usr/lib/python2.5/test/__init__.pyc'
because there's apparently a module named "test" in the standard
distribution that gets found instead.
You were right. When I renamed my test.py file into test77.py it
worked perfectly well. Thank you.
Is there a way to know which test.py it was importing?

well, as my simple code showed, you can check test.__file__ or
test.__path__ if you're curious.  Python just searches through
your $PYTHONPATH which you can determine at runtime via sys.path

-tkc

Thank you Tim, everything is clear now.

Vicente Soler
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top