簡體   English   中英

腳本僅在Python安裝目錄中有效

[英]Script only works in Python installed directory

我有這個腳本,可以從圖像中讀取條形碼。

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 python主目錄中時,它可以正常工作。

但是,當我將此腳本放到C:\\myscript類的主目錄之外時,它給了我錯誤提示,說import zbar - module The specified module could not be found

是什么原因引起的?

我在Windows Xp 32bits SP3上使用Python 2.7 32bits

編輯:

我正在使用運行模塊命令(F5)從IDLE窗口執行它;完全回溯

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__ import zbar; print zbar.__file__我得到以下消息

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

似乎dll位於c:\\ Python27中,但c:\\ Python27不在搜索路徑中。 嘗試添加

import sys

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

導入zbar之前將其添加到代碼中

如果工作正常,則必須配置python的搜索路徑才能添加C:\\ Python27。 我在linux上工作,很抱歉,我在Windows上無法幫助您。

編輯 :好吧,我不喜歡寫一個我不知道該怎么做的答案。 因此,我進行了一些研究,尋找一些文檔來幫助我弄清您的問題所在。 並在這里導入PYD文件

確保您要導入的所有文件都與此腳本位於同一目錄中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM