Listing variables

Tim Chase python.list at tim.thechases.com
Sun Oct 25 08:32:16 EDT 2009


> 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 at rubbish:~/tmp$ echo "a=42" > test.py
tim at 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 at rubbish:~/tmp$ rm test.py test.pyc
tim at 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






More information about the Python-list mailing list