2023年7月3日 星期一

【Python交易程式開發現場30】在VS Code中使用Python虛擬環境之問題解決 | Comtypes和python版本的相容問題 | ModuleNotFoundError: No module named ‘comtypes.gen.SKCOMLib’

程式執行時遇到了: ModuleNotFoundError: No module named ‘comtypes.gen.SKCOMLib’


1. 參考網路解答1:(失敗收場)

問題:ModuleNotFoundError: No module named ‘comtypes.gen.SKCOMLib’

第一、原因:python 版本問題(3.8 不能)

第二、註冊問題(SKCOM.dll 要和主程式同目錄)

import comtypes.client 
comtypes.client.GetModule(os.path.split(os.path.realpath(__file__))[0] + r'\SKCOM.dll')






2. 參考網路解答2:(失敗收場)


.venv下的comtypes下沒產生gen目錄?


3. 參考網路解答3:(失敗收場)

###########################################################
# pythoncom 是安裝 pywin32 附的,如果 event 沒反應才需要用到
# 第一次使用cc.GetModule, 會在 comtypes\gen\ 下產生幾個.py檔,像是
# _75AAD71C_8F4F_4F1F_9AEE_3D41A8C9BA5E_0_1_0.py
# 如果有 dll 錯誤產生,請檢查 SKCOM.dll 路徑有沒有錯誤
# 如果發生 gen 下沒有 SKCOMLib 錯誤訊息,請將python關閉
# AttributeError: module 'comtypes.gen.SKCOMLib' has no attribute 'SKCenterLib'
# 重新啟動python後,應該就可以找到了
# 如果還是不行,請重新安裝 capital API 元件,注意 API 與python 版本要一致,
# API安裝要以管理員權限執行
# 20190506,程式碼簡化
import pythoncom, time
import comtypes.client as cc

cc.GetModule('C:\\SKCOM\\x86\\SKCOM.dll')
import comtypes.gen.SKCOMLib as sk

4. 參考網路解答4:(失敗收場)



3

I just figured out what the cryptic "SyntaxError: multiple exception types must be parenthesized" message means. All it's trying to tell you is that in the newer version of Python you're using, this syntax is no longer valid:

except COMError, err:

instead, you should be using this syntax:

except COMError(err):


5. 參考網路解答後, 自己想出兩個解法, 第一個解法:(失敗收場)

先用賤招, 將gen由原global的python310目錄中正常的comtypes中的gen搬到.venv中的comtypes中來,
結果問題只解決一半, comtypes.gen的確找到了, 但"multiple exception types must be parenthesized" 的python和comtypes版本不相容的問題仍未解決..

6. 參考網路解答後, 自己想出的第二個解法:(成功)

最後, 用pip install --upgrade comtypes 才解決問題







沒有留言:

張貼留言

prog-0318

test test1 Written with StackEdit .