這不是一個真正的答案,將滿足您的需求,但也許它會給你一些繼續。
我遇到了一個與Python非常類似的問題,在我的情況下,我的應用程序是一個純Python應用程序。我也注意到,如果我的應用程序被安裝到了一個無法在MBCS中編碼的路徑字符串(Python根據我的理解,至少Python在3.2之前轉換爲內部導入),Python解釋器會失敗,聲稱不存在該名稱的模塊。
我所要做的就是編寫一個Import Hook來誘騙它加載這些文件。
這就是我想出了:
在Windows資源管理器的啓動文件(.pyw文件)
- 雙擊做:
import imp, os, sys
class UnicodeImporter(object):
def find_module(self,fullname,path=None):
if isinstance(fullname,unicode):
fullname = fullname.replace(u'.',u'\\')
exts = (u'.pyc',u'.pyo',u'.py')
else:
fullname = fullname.replace('.','\\')
exts = ('.pyc','.pyo','.py')
if os.path.exists(fullname) and os.path.isdir(fullname):
return self
for ext in exts:
if os.path.exists(fullname+ext):
return self
def load_module(self,fullname):
if fullname in sys.modules:
return sys.modules[fullname]
else:
sys.modules[fullname] = imp.new_module(fullname)
if isinstance(fullname,unicode):
filename = fullname.replace(u'.',u'\\')
ext = u'.py'
initfile = u'__init__'
else:
filename = fullname.replace('.','\\')
ext = '.py'
initfile = '__init__'
if os.path.exists(filename+ext):
try:
with open(filename+ext,'U') as fp:
mod = imp.load_source(fullname,filename+ext,fp)
sys.modules[fullname] = mod
mod.__loader__ = self
return mod
except:
print 'fail', filename+ext
raise
mod = sys.modules[fullname]
mod.__loader__ = self
mod.__file__ = os.path.join(os.getcwd(),filename)
mod.__path__ = [filename]
#init file
initfile = os.path.join(filename,initfile+ext)
if os.path.exists(initfile):
with open(initfile,'U') as fp:
code = fp.read()
exec code in mod.__dict__
return mod
sys.meta_path = [UnicodeImporter()]
我仍然在使用這種碰到兩個問題當應用程序安裝在故障目錄中時不起作用。我相信這與Windows文件關聯如何將參數傳遞給pythonw.exe有關(我的猜測是Windows將包含不可編碼字符的完整路徑字符串作爲參數傳遞給exe)。如果我創建了一個批處理文件並讓該批處理文件僅使用我的啓動程序的文件名稱調用Python可執行文件,並確保它是從同一個目錄啓動的,則它將啓動罰款。再次,我敢打賭,這是因爲現在我可以使用相對路徑作爲python.exe的參數,並避免路徑中的這些故障字符。
- 使用py2exe打包我的應用程序,如果放置在這些故障路徑之一中,生成的exe將不會運行。我認爲這與zipimporter模塊有關,不幸的是它是一個編譯的Python模塊,所以我不能輕易修改它(我將不得不重新編譯等等)。
[這篇文章](http://www.codeproject.com/Articles/4563/Upgrading-an-STL-based-application-to-use-Unicode)可能是你的興趣。看看它如何定義'toWideString'函數並使用它。 – Hossein 2012-02-01 18:24:59
我嘗試使用winapi函數SetEnvironmentVariable將PATH變量與路徑附加到我的應用程序 - 失敗。 – 2012-02-02 07:56:32
看來,我將路徑版本2.7.2或遷移到3.x – 2012-02-02 15:01:22