From: Shane on
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
From: Peter Otten on
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

From: Lie Ryan on
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.
From: Shane on
On Apr 26, 11:58 am, Lie Ryan <lie.1...(a)gmail.com> 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.
From: Peter Otten on
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
 |  Next  |  Last
Pages: 1 2
Prev: Python date time API
Next: py2exe sets error message