2014-02-27 139 views
1

我選擇嘗試使用cx_freeze,它將我的簡單python 3.x鍵盤記錄轉換爲exe。我選擇cx_freeze,因爲py2exe只是Python 2.x我正在使用這個setup.py腳本編譯我的代碼。cx_freeze無法導入外部模塊

from cx_Freeze import setup, Executable 

# Dependencies are automatically detected, but it might need 
# fine tuning. 
buildOptions = dict(packages = [], excludes = []) 

base = 'Console' 

executables = [ 
    Executable('logger.py', base=base, targetName = 'logger.exe') 
] 

setup(name='PyLogger', 
     version = '0.1', 
     description = 'A Simple Keylogger', 
     options = dict(build_exe = buildOptions), 
     executables = executables) 

,我當我編譯我的代碼是

try: 
    import pythoncom 
except ImportError: 
    input("Import Error, pywin32 is not installed") 

try: 
    import pyHook 
except ImportError: 
    input("Import Error, pyHook is not installed") 

我得到的導入錯誤說法既pywin32並沒有安裝pyHook。如何將外部模塊導入cx_freeze。

編輯 - 我試過改變setup.py來添加includes選項,但沒有任何區別。

from cx_Freeze import setup, Executable 

# Dependencies are automatically detected, but it might need 
# fine tuning. 
buildOptions = dict(packages = ['pyHook','pythoncom'],includes = ['pyHook','pythoncom'], excludes = []) 

base = 'Console' 

executables = [ 
    Executable('logger.py', base=base, targetName = 'logger.exe') 
] 

setup(name='PyLogger', 
     version = '0.1', 
     description = 'A Simple Keylogger', 
     options = dict(build_exe = buildOptions), 
     executables = executables) 
+0

你能顯示凍結你的應用程序的輸出嗎?另外,這些包裝在壓縮蛋中? cx_Freeze目前在查找壓縮雞蛋中的模塊時遇到困難,但我已經[請求](https://bitbucket.org/anthony_tuininga/cx_freeze/pull-request/38/finding-packages-inside-zip-files)到解決該問題。 –

回答

0

嘗試像這樣的編譯選項明確列出缺少的包:

buildOptions = dict(packages = ['pyHook', 'pywin32'], excludes = []) 

而看到接受的答案this question如果你需要在你的構建其他(非Python)的文件。

編輯:我終於有時間再看一遍,這似乎是一個棘手的問題。我會在時間允許的情況下繼續探究它,但我想我會發布我的發現,以防他們對OP有用。我懷疑pyHook模塊在「凍結」時不能很好地播放,即當它包含在一個zip文件中時。如果使用此setup.py

from cx_Freeze import setup, Executable 

buildOptions = dict(
    includes=['pythoncom'], 
    packages=['pyHook'] 
) 

executables = [ 
    Executable('logger.py', base='Console', targetName = 'logger.exe') 
] 

setup(
    name='PyLogger', 
    version = '0.1', 
    description = 'A Simple Keylogger', 
    options = dict(build_exe = buildOptions), 
    executables = executables 
) 

所生成logger.exe不-最初,在正確地至少運行,並生成錯誤:

Import Error, pyHook is not installed 

但是,如果我從目錄運行下面的命令包含EXE:

unzip library.zip 

並重新運行logger.exe,那麼一切似乎很好地工作。它只是無法加載cx_Freeze生成的library.zip文件中的pyHook。過去我曾經見過這種類型的問題,並且在加載任何模塊之前,通過在我的頂級腳本中加載sys.path來解決此問題。我會看看我能否挖掘其中的一個例子。同時,也許這個建議會幫助OP:嘗試解壓zip文件,看看它是否有所作爲。幾件事情要注意:

  • 我並沒有進口任何pywin32問題,只有pyHook
  • 嘗試設置create_shared_zip=False,並在構建選項include_in_shared_zip=False,但這只是導致一個名爲logger.zip文件而不是library.zip。 (奇怪,我不敢相信那不是bug)
+0

不,仍然得到相同的錯誤。 – Coder77

+0

對不起,我想你需要在''buildOptions''中設置''includes''而不是''packages''。我目前還沒有在我可以驗證的地方,但我會盡快檢查並更新我的答案。 – evadeflow

+0

剛剛更新我的主要職位添加包括,但pyHook和pywin32仍然沒有正確導入。 – Coder77

1

找到外部模塊的.pyd文件。複製並粘貼到構建文件中。因此,例如,如果它正在尋找_cpyHook(我有和你一樣的問題,並且它說那個模塊丟失了),請轉到C:\ Python33 \ Lib \ site-packages \ pyHook並將該文件複製並粘貼到C中:\ Python33 \建立\ exe.win-amd64-3.3。