Listing variables

Discussion in 'Python' started by vsoler, Oct 25, 2009.

  1. vsoler

    vsoler Guest

    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
    vsoler, Oct 25, 2009
    #1
    1. Advertising

  2. vsoler

    Tim Chase Guest

    > 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 Chase, Oct 25, 2009
    #2
    1. Advertising

  3. vsoler

    Dave Angel Guest

    vsoler wrote:
    > 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)
    Dave Angel, Oct 25, 2009
    #3
  4. vsoler

    vsoler Guest

    On Oct 25, 12:01 pm, Tim Chase <> wrote:
    > > 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.

    >>> import test
    >>> dir(test)

    ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
    '__path__']
    >>>


    I am using python 2.6

    Am I doing anything wrong?
    vsoler, Oct 25, 2009
    #4
  5. vsoler

    Tim Chase Guest

    > 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 Chase, Oct 25, 2009
    #5
  6. vsoler

    vsoler Guest

    On Oct 25, 1:32 pm, Tim Chase <> wrote:
    > > 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?
    vsoler, Oct 25, 2009
    #6
  7. vsoler

    Tim Chase Guest

    >> 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
    Tim Chase, Oct 25, 2009
    #7
  8. vsoler

    vsoler Guest

    On Oct 25, 5:07 pm, Tim Chase <> wrote:
    > >> 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
    vsoler, Oct 25, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ohms377
    Replies:
    3
    Views:
    289
    Cameron Laird
    May 11, 2005
  2. Replies:
    4
    Views:
    266
    Jaspreet
    Jun 23, 2005
  3. =?iso-8859-1?B?QW5kcuk=?=

    listing all property variables of a class instance

    =?iso-8859-1?B?QW5kcuk=?=, Jun 25, 2007, in forum: Python
    Replies:
    5
    Views:
    273
    =?iso-8859-1?B?QW5kcuk=?=
    Jun 25, 2007
  4. Replies:
    1
    Views:
    148
    Josh Cheek
    Sep 27, 2010
  5. yawnmoth
    Replies:
    12
    Views:
    279
    yawnmoth
    Feb 1, 2009
Loading...

Share This Page