34

I'm using python 2.7.2 and windows 7. I searched through internet, helps and other sources but i can't find an answer to my problem.

One of my source imports tkinter, and this one imports _tkinter. At this moment it say ImportError DLL load failed:

 Traceback (most recent call last): File "NERO/show_image.py", line 13,
 in <module> import Tkinter File "C:\Python27\lib\lib-tk\Tkinter.py", line 38,
 in <module> import FixTk File "C:\Python27\lib\lib-tk\FixTk.py", line 65,
 in <module> import _tkinter ImportError: DLL load failed: %1 is not a valid Win32 application.
 2012-Jan-04 12:27:21.889374 (M) [python] Finished process 

I searched _tkinter and i found it in Python27/libs as a lib file.

On many sites it says to install tk/tcl or tkinter, but I don't find a separate installation for windows.

Using Windows x86 MSI Installer (2.7) from http://www.python.org/getit/releases/2.7/. In windows 7 64-bit. The python version is 32 bit.

nbro
  • 15,395
  • 32
  • 113
  • 196
paduraru2009
  • 595
  • 2
  • 5
  • 11
  • 2
    Is your computer 64-bit ? You do not need to install anything to make python tkinter work right out of the box when using the official binary installer. Could you post the actual, full traceback? – joaquin Jan 04 '12 at 09:52
  • 1
    Yes, i have installed windows 7 64. The python version is 32 bit. Traceback (most recent call last): File "NERO/show_image.py", line 13, in import Tkinter File "C:\Python27\lib\lib-tk\Tkinter.py", line 38, in import FixTk File "C:\Python27\lib\lib-tk\FixTk.py", line 65, in import _tkinter ImportError: DLL load failed: %1 is not a valid Win32 application. 2012-Jan-04 12:27:21.889374 (M) [python] Finished process – paduraru2009 Jan 04 '12 at 10:29
  • 2
    Not sure what your problem is, but this is a tipical error I get when having some conflict with 32/64-bits. Sorry I can not help more. Maybe you should try cleaning your actual setup and reinstalling python for 64-bits afresh. – joaquin Jan 04 '12 at 11:42
  • this problem madly occurs to me in 64bit windows with 64 bit python executable :( – greendino Jun 14 '20 at 22:53

12 Answers12

28

I had a similar problem importing Tkinter on Windows 7 64-bit - seems that the 64-bit library was still in the libs folder from a previous 64-bit python install.

Uninstalling 64-bit python properly and then repairing with the 32-bit installer fixed the problem for me - you don't need to use 64-bit python.

David Jacka
  • 433
  • 5
  • 7
  • 4
    A little elaboration: I had the same issue; it turned out that I had a factory-installed 32-bit version of Python 2.7 sitting on my machine when I got it, and I'd then installed a 64-bit version over it without noticing. This resulted in mixed-version libraries all in the "Python27" directory--not pretty. Uninstalling them both and reinstalling a single version worked just fine. – Henry Keiter Nov 21 '13 at 15:32
  • 1
    This appears to be the best choice for answer...it solved my problem. Perhaps give @david some credit and mark it as the correct answer? – Brad P. Sep 25 '14 at 02:14
6

Re-install Python.

If this still doesn't work, there is another simple solution:

  1. Uninstall your current python. Delete Python27 folder entirely from your C: drive.
  2. Download and install the 64-bit version of Python from http://www.python.org/download/.
  3. Next time do not mix your 32-bit application with 64-bit OS whenever a 64-bit choice for the application is available!
Hossein
  • 4,097
  • 2
  • 24
  • 46
  • 5
    I don't know if this is very good advice, don't lots of libraries not work properly with 64 bit python? – Alex S May 19 '12 at 14:59
  • 1
    @AlexS I have a few gripes with Python but that isn't one of them. Any library written in python will run just fine and I've yet to find one written in C that doesn't try to compile for the local architecture on install and/or fall back to a pure python solution at the expense of performance. – Basic Jul 19 '15 at 03:55
3

I had the same issue and was able to uninstall Python 2.7 (using the Windows 7 'uninstall' service) and then reinstall it from here: python-2.7.6.msi

Also, if you're concerned about ensuring 64bit conformance across your python libraries then you may find the following library list site to be a great resource. It has numerous 64bit versions of python libraries.

3

I was getting this error while trying to use matplotlib in a simple application.

Uninstall Python and remove its folder from your C-Drive. Download and Install 64bit python. Follow this to install pip on your machine. http://www1.cmc.edu/pages/faculty/alee/cs40/penv/installFlaskOnWindows.html This was finally what worked for me

3

Please don't reinstall Python for this. A simple fix can resolve this issue as below:

  1. Find the matplotlibrc file at Python27\Lib\site-packages\matplotlib\mpl-data\matplotlibrc
  2. Open using notepad
  3. Change the backend parameter to 'backend: Qt5Agg'

And it will be solved. No need to reinstall.

AbhiGupta
  • 474
  • 1
  • 6
  • 14
  • Amazing! This worked for me. Do you know what is going on under the hood? – StackG Nov 13 '18 at 00:10
  • 1
    The new matplotlib installation is not compatible with Tk backend(default in matplotlib), hence through these steps we change the default backend to Qt5Agg – AbhiGupta Dec 01 '18 at 06:01
1

SOLVED without reinstalling anything:

1-add these 3 lines to your setup.py :

...
import os
os.environ['TCL_LIBRARY']= r'C:\<your python folder>\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\<your python folder>\tcl\tk8.6'
...
setup(
... 
) 

2- copy tcl86t.dll and tk86t.dll from C:\<your python folder>\DLLs to <your project folder>(where you have batch file, setup.py).

3- add "include_files": ["tcl86t.dll", "tk86t.dll"] to option in your setup.py, something like:

options= { 'build_exe':{'packages':[.....], "include_files": ["tcl86t.dll", "tk86t.dll"]}},

note: if you didn't find tcl86t.dll and tk86t.dll in DLLs folder as mentionned in step 2, then try again to search for it,but using research tab in DLLs folder this time.

SAADOV
  • 11
  • 3
  • did you already try this on target machine WITHOUT PYTHON? how is this get upvoted? – greendino Jun 12 '20 at 20:39
  • @greendino Why on Earth would anyone try this "on a machine without Python?" Of course the Tkinter library won't work without Python. It's **for** Python! – Karl Knechtel Apr 26 '23 at 19:23
  • @KarlKnechtel been many years passed, the point is, our client doesnt have python installed if we were compiling this tkinter app, do you really wanna run tkinter app uncompiled on client side? might as well give them the source code, so to get around this, i included the dll inside the compiled tkinter folder – greendino May 04 '23 at 11:37
1

I had a similar problem. My Python version was 2.7.12 and 64bit and I was using Windows 10, 64bit OS. I tried to import Tkinter and what I got back was

ImportError: DLL load failed: %1 is not a valid Win32 application

I uninstalled Python and removed all traces of it. I then installed Python 2.7.14 (32bit) and this seems to have solved my problem. I don't get this error message any longer.

LW001
  • 2,452
  • 6
  • 27
  • 36
0

I have a lot of installed python packages on my machine so I did not want to unistall and reinstall the whole python, I did as follow and the problem was solved:

  1. Change your C:\Python27 home folder to another name, for example C:\Python27_old

  2. Install a new Python27 on your computer (32 bit)

  3. Copy tcl and DLLs folder from the fresh installed python to the old one (Before this step make a copy of tcl and DLLs of C:\Python27_old to recover it if you face any problem in the next steps)

C:\Python27\tcl Copy and replace to C:\Python27_old

C:\Python27\DLLs Copy and replace to C:\Python27_old

  1. Delete the C:\Python27
  2. Rename the C:\Python27_old to the C:\Python27

Please pay attention to get a backup form your tcl and DLLs folder to recover it in case you face any further problem.

0

Removing 64 bit python and doing a repair on 32 bit python worked for me. OS: 64 bit Windows Server 2008.

Jeremy
  • 1
0

I also met this issue, because I use 64-bit python. Maybe python 64-bit doesn't have a good support.

My way is to solve it:

  1. Uninstall python 64-bit.
  2. Install python 32-bit.
  3. Uninstall the 64-bit package. pip uninstall xxx.
  4. Install the 32-bit package. pip install xxx.
Raven
  • 197
  • 4
  • 7
0

For Windows 10,

  1. Uninstall all versions of Python
  2. Install Python 3.x (your latest version) with PATH variable
  3. python -mpip install -U matplotlib
  4. Install Python x.x (your other versions) without PATH variable

Done. Try running your scripts again.

0

If Python was installed by OsGeo4W then you should also install the python-tcltk and/or python3-tcltk packages in the OsGeo4W setup tool.

SzieberthAdam
  • 3,999
  • 2
  • 23
  • 31