2013-12-16 28 views
0

我有这个脚本从图像中读取条形码。脚本只适用于Python安装目录

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) 

当我主目录是C:\Python27它工作没有任何问题,把这个脚本蟒。

但是,当我把这个脚本以外的主目录,如C:\myscript,它给了我错误说,import zbar - module The specified module could not be found

是什么导致了这个问题?

我使用的Windows XP 32位SP3的Python 2.7 32位

编辑:

我从IDLE窗口通过使用运行模块命令(F5) 执行;全回溯

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. 

当我输入import zbar; print zbar.__file__ 我收到以下信息

C:\Python27\lib\site-packages\zbar.pyd 
+5

什么是* full *回溯(即复制/粘贴打印到命令提示符的所有内容)?你如何执行这个脚本? – Blender

+1

如果你在'c:\ python27'运行下面的命令,你会得到什么:'import zbar;打印zbar .__ file__'? – falsetru

+0

哪个模块?你得到了什么确切的错误? – 2013-12-16 03:57:31

回答

-3

请确保您具有与此脚本在同一目录中导入的所有文件

+1

所有导入的文件不需要与脚本相同的目录 - 所以这个答案显然是错误的。 – martineau

0

似乎dll位于c:\ Python27中,但c:\ Python27不在搜索路径中。尝试在导入zbar之前添加

import sys 

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

到您的代码。

如果工作正常,那么你必须配置你的Python的搜索路径,以便添加C:\ Python27。我在linux上工作,抱歉,我无法帮助您在Windows上执行此操作。

编辑:嗯,我不喜欢写一个答案,我不知道该怎么做。所以我做了一些研究,寻找一些帮助我找出问题所在的文档。并在这里找到它importing PYD files

+0

手动配置Python搜索路径的一种方法是修改标准库中的'site.py'文件。 Python解释器运行时会自动导入此模块(除非这样做是通过命令行开关禁止的)。有关更多详细信息,请参阅联机文档中的[_修改Python的搜索路径_](http://docs.python.org/release/2.7.6/install/index.html#modifying-python-s-search-path)。 – martineau