Python Forum
Importing Modules Have Stopped Working.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing Modules Have Stopped Working.
#1
Hi All,

I had a script which I use to scrape a website, which has been working reliably for months. I installed TensorFlow just over a week ago, and a day or so later, my script stopped working. The issue is that I'm getting import errors in some of the modules.

In order to try to fault find, I have written this script to find which modules are causing the error.

try:
	import sys
except:
	print("sys " + str(ImportError))		
try:
	import os
except:
	print("os " + str(ImportError))
try:
	import urllib
except:
	print("urllib " + str(ImportError))
try:
	import win32clipboard
except:
	print("win32clipboard " + str(ImportError))
try:
	import re
except:
	print("re " + str(ImportError))
try:
	import webbrowser
except:
	print("webbrowser, " + str(ImportError))
try:
	import fnmatch
except:
	print("fnmatch " + str(ImportError))	

print ()	
print(sys.version)

	
finalClose = input("Press any key")
which gives me:
Output:
win32clipboard <class 'ImportError'> re <class 'ImportError'> webbrowser <class 'ImportError'> fnmatch <class 'ImportError'> 3.6.6 (v3.6.6.6:4c41f54eb7, Jun 27 2018, 02:47:15) [MSC v.1900 32 bit (Intel)] Press any key
I'm stumped as to how to resolve this.
What I have tried so far:
  • Repairing Python
  • Re-installing Python
  • Re-installing Python Windows extensions
  • Removing Tensorflow
  • Un-installing python completely, and cleaned the registry
  • read every thread I can find

As I said, I'm stumped. I've spent a week trying to resolve this Confused

If anyone knows what's gone wrong, I'd appreciate your help.
Reply
#2
What are stack traces for each of those?
Reply
#3
Thanks micseydel,

I'm getting:
Error:
Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> import win32clipboard ImportError: DLL load failed: %1 is not a valid Win32 application.
The thing is, I can import the modules through Python Shell (except Win32clipboard), but not in a .py script. What am I missing?
Reply
#4
most of these are part of standard libraries, so no need to import in try/except block at all (i.e. if it fails you have far bigger problem). It may make sense only if you want to support both python2 and python3 and the respective module imports differently in both versions (e.g. Tkinter and tkinter).
Also <class 'ImportError'> isn't very descriptve. If you are going to catch the exception, create more meaningful message with the error description. And while debuging - remove the try except and get benefit of the full traceback info.
for start remove the try/except blocks and show us the traceback (is it same for all of the imports?)
(Jul-14-2018, 04:37 AM)Randomname Wrote: The thing is, I can import the modules through Python Shell (except Win32clipboard), but not in a .py script.
Sounds like you have more than one installation and at least one is corrupt.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hi buran,

I've changed the code to
import traceback
import sys
import os
import urllib
import win32clipboard
import re
import webbrowser
import fnmatch
print ()	
print(sys.version)
with the result of:
Error:
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 02:47:15) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> >>> import win32clipboard Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> import win32clipboard ImportError: DLL load failed: %1 is not a valid Win32 application. >>> import re >>> RESTART: C:\Users\Shane\AppData\Local\Programs\Python\Python36-32\pytest.py Traceback (most recent call last): File "C:\Users\Shane\AppData\Local\Programs\Python\Python36-32\pytest.py", line 5, in <module> import win32clipboard ImportError: DLL load failed: %1 is not a valid Win32 application.
I don't think there's more than one installation - I've deleted everything and cleaned the registry before installing 3.6 again. Or am I missing something here?
Reply
#6
so it's only win32clipboard that create problems. it's part of pywin32. How did you install pywin32? select the correct binary for your python installation. In your case - pywin32-223.win32-py3.6.exe
eventually, if you just want to use clipboard for plain text and no other use of pywin32 - try https://github.com/asweigart/pyperclip
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Cheers buran, you nailed it.

I installed pywin32-223.win32-py3.6.exe and all is well with the world again! Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tensorflow stopped after numpy install Devilish 0 2,194 Dec-28-2018, 09:29 PM
Last Post: Devilish
  Importing Packages/Modules Upandon 7 4,563 Mar-28-2018, 04:15 AM
Last Post: Upandon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020