Script only works in Python installed directory

1.1k Views Asked by At

I have this script which read bar codes from images.

from PIL import Image
import zbar

scanner = zbar.ImageScanner()
scanner.parse_config('enable')
pil = Image.open('zbartest2.png').convert('L')
width, height = pil.size
raw = pil.tostring()
image = zbar.Image(width, height, 'Y800', raw)
scanner.scan(image)

for symbol in image:
    print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
del(image)

When i put this script in python main directory which is C:\Python27 it works without any problem.

However, when i put this script outside of main directory such as C:\myscript, it gives me error saying that import zbar - module The specified module could not be found.

What is causing the problem?

I am using Python 2.7 32bits on windows Xp 32bits SP3

EDIT:

I am executing it from the IDLE window by using run module command (F5) ;full traceback

Traceback (most recent call last):
   File "C:\myscript\test.py", line 2, in <module>
   import zbar
ImportError: DLL load failed: The specified module could not be found.

when i type in import zbar; print zbar.__file__ i get the following msg

C:\Python27\lib\site-packages\zbar.pyd
2

There are 2 best solutions below

1
On

It seems the dll is in c:\Python27 but c:\Python27 is not in the search path. Try the adding

import sys

sys.path.append("C:\Python2.7")

to your code before importing zbar.

If works correctly, then you have to configure your python's search paths in order to add C:\Python27. I work on linux, sorry I can't help you to do that on Windows.

EDIT: Well, I don't like write in an answer that I don't know how to do something. So I do some research looking for some doc that help me figure out what your problem is. And found it here importing PYD files.

1
On

Make sure you have all the files that you are importing in the same directory as this script