2

I choose to try using cx_freeze which converts my simple python 3.x keylogger to an exe. I choose cx_freeze because py2exe is only python 2.x I am compiling my code using this setup.py script.

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)

and I when I compile my code which is

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

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

I get the import error saying both pywin32 and pyHook is not installed. How do you import external modules into cx_freeze.

EDIT - I have tried changing the setup.py to add the includes option but it made no difference.

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)
Coder77
  • 2,203
  • 5
  • 20
  • 28
  • Can you show the output from freezing your application? Also, are those packages in zipped eggs? cx_Freeze currently has trouble finding modules in zipped eggs, but I've got [a pull request](https://bitbucket.org/anthony_tuininga/cx_freeze/pull-request/38/finding-packages-inside-zip-files) to fix that. – Thomas K Mar 02 '14 at 17:45

2 Answers2

1

Find the .pyd file of the external module. Copy and paste that into the build file. So, for example, if it was looking for _cpyHook (I had the same problem as you and it said that module was missing), go to C:\Python33\Lib\site-packages\pyHook and copy and paste the file into C:\Python33\build\exe.win-amd64-3.3.

hmnbvcxz
  • 56
  • 5
0

Try listing the missing packages explicitly in the build options like so:

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

And see the accepted answer to this question if you need to include other (non-Python) files in your build.

EDIT: I finally had time to look at this a little more, and it seems to be a tricky problem. I'll keeping poking at it as time permits, but I thought I'd post my findings in case they're useful to the OP. I suspect that the pyHook module doesn't play nice when 'frozen', i.e., when it's included in a zip file. If I use this 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
)

the generated logger.exe does not—initially, at least—run correctly, and generates the error:

Import Error, pyHook is not installed

However, if I run the following command from the directory containing the EXE:

unzip library.zip

and re-run logger.exe, then everything seems to work fine. It's just not able to load pyHook from the library.zip file that cx_Freeze generates. I've seen this kind of problem before in the past, and worked around it by munging sys.path in my top-level script prior to loading any modules. I'll see if I can dig up one of those examples. In the meantime, perhaps this advice will help the OP: try unzipping the zip file and see if it makes a difference. A couple things to note:

  • I'm not having any problems importing pywin32, only pyHook
  • I did try setting create_shared_zip=False and include_in_shared_zip=False in the build options, but this just resulted in a file named logger.zip instead of library.zip. (Weird. I can't believe that's not a bug.)
Community
  • 1
  • 1
evadeflow
  • 4,704
  • 38
  • 51
  • Sorry, I think you need to set ``includes`` rather than ``packages`` in your ``buildOptions`` dict. I'm not in a place where I can verify this at the moment, but I'll check and update my answer accordingly as soon as I am. – evadeflow Feb 28 '14 at 22:23
  • Just updated my main post adding the includes, however pyHook and pywin32 are still not being imported correctly. – Coder77 Mar 01 '14 at 17:09