FAQ
I'm new to Python, so I'll try to be clear about my problem.

I'm using Python 3.1 (latest stable version from python.org) on
Windows 7.
I have a program using tkinter for UI, and it works properly from both
pything GUI shell, and running from command prompt, EXCEPT that I have
a menu command to invoke tkinter.filedialog.askopenfile, and it fails
because it says:

file = tkinter.filedialog.askopenfilename()
AttributeError: 'module' object has no attribute 'filedialog'

I made a simple test program:

import tkinter
print (dir(tkinter))

when I run this from the GUI shell, the results include filedialog,
but from the command prompt, it does not (also missing other
attributes, such as messagebox).

All the UI widgets work properly.
My best hypothesis at this point is that from the GUI shell its using
the source code under lib\tkinter (where there is a filedialog.py),
but from the command shell it is using the compiled dll, and that
doesn't export filedialog for some reason.

Thanks in advance for any suggestions

Search Discussions

  • Peter Otten at Apr 26, 2010 at 5:50 pm

    Shane wrote:

    I'm new to Python, so I'll try to be clear about my problem.

    I'm using Python 3.1 (latest stable version from python.org) on
    Windows 7.
    I have a program using tkinter for UI, and it works properly from both
    pything GUI shell, and running from command prompt, EXCEPT that I have
    a menu command to invoke tkinter.filedialog.askopenfile, and it fails
    because it says:

    file = tkinter.filedialog.askopenfilename()
    AttributeError: 'module' object has no attribute 'filedialog'

    I made a simple test program:

    import tkinter
    print (dir(tkinter))

    when I run this from the GUI shell, the results include filedialog,
    but from the command prompt, it does not (also missing other
    attributes, such as messagebox).

    All the UI widgets work properly.
    My best hypothesis at this point is that from the GUI shell its using
    the source code under lib\tkinter (where there is a filedialog.py),
    but from the command shell it is using the compiled dll, and that
    doesn't export filedialog for some reason.

    It's not that complicated; idle and your module share the same python
    interpreter and the same tkinter package. Idle needs a file dialog, too, and
    somewhere in its code there must be a

    import tkinter.filedialog

    statement which of course isn't executed when you run your script from the
    command line. To fix your script simply add the above import statement.

    It is a bit unfortunate that your editor has side effects on your program,
    and I recommend that you never trust the result of importing a module from
    within idle's shell completely.

    Peter
  • Lie Ryan at Apr 26, 2010 at 6:58 pm

    On 04/27/10 03:50, Peter Otten wrote:
    It is a bit unfortunate that your editor has side effects on your program,
    and I recommend that you never trust the result of importing a module from
    within idle's shell completely.
    In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
    Notepad; but for serious work, you need a real IDE or a programmer's
    text editor (vim or emacs, whichever side you're in).

    Always test the you write inside IDLE on a command line.
  • Shane at Apr 27, 2010 at 3:07 am

    On Apr 26, 11:58?am, Lie Ryan wrote:
    On 04/27/10 03:50, Peter Otten wrote:

    It is a bit unfortunate that your editor has side effects on your program,
    and I recommend that you never trust the result of importing a module from
    within idle's shell completely.
    In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
    Notepad; but for serious work, you need a real IDE or a programmer's
    text editor (vim or emacs, whichever side you're in).

    Always test the you write inside IDLE on a command line.
    Thank you both for the replies. importing tkinter.filedialog did the
    trick, and I appreciate the advice about not depending on IDLE. I'm
    not planning on doing any serious work, so I will probably continue to
    rely on IDLE for now.
  • Peter Otten at Apr 27, 2010 at 8:01 am

    Lie Ryan wrote:

    In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
    Notepad; but for serious work, you need a real IDE or a programmer's
    text editor (vim or emacs, whichever side you're in).
    Some people, when confronted with a problem, think "I know, I'll use emacs."
    Now they have two problems.

    More seriously, the subset of emacs I am using is probably supported by
    idle, too. That's why I sympathize with the OP's decision to stick with
    idle.

    Peter
  • Lie Ryan at Apr 27, 2010 at 9:37 am

    On 04/27/10 18:01, Peter Otten wrote:
    Lie Ryan wrote:
    In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
    Notepad; but for serious work, you need a real IDE or a programmer's
    text editor (vim or emacs, whichever side you're in).
    Some people, when confronted with a problem, think "I know, I'll use emacs."
    Now they have two problems.

    More seriously, the subset of emacs I am using is probably supported by
    idle, too. That's why I sympathize with the OP's decision to stick with
    idle.
    It's not about IDLE's text editing capability, which is perfectly fine.
    It's about IDLE mangling your program's execution due to IDLE being
    written in Python itself. It's often the case your program would run
    fine inside IDLE but crashes badly outside; that's why if you need to
    use IDLE, test your program in command line as well. Otherwise you're
    going to be surprised.
  • Paul Rudin at Apr 27, 2010 at 10:01 am

    Peter Otten <__peter__ at web.de> writes:


    Some people, when confronted with a problem, think "I know, I'll use emacs."
    Now they have two problems.
    Probably you know this ... but the original form of this saying had
    "regular expressions" in place of "emacs".

    Since Jamie Zawinski coined this saying and he was a significant
    contributor to XEmacs it's quite ironic to see it in the form you give
    :)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 26, '10 at 5:25p
activeApr 27, '10 at 10:01a
posts7
users4
websitepython.org

People

Translate

site design / logo © 2023 Grokbase