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)
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.
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.)
Related
I know there have been a bunch of questions already asked regarding this but none of them really helped me. Let me explain the whole project scenario so that I provide a better clarity to my problem. The directory structure is somewhat like this shown below:
Project Directory Layout
I need to convert this whole GUI based project (The main file is using Tkinter module to create GUI) into main.exe which I can share with others while making sure that all the additional files work exactly the same way it is working now when I run this main.py via Command Prompt. When I use this command with pyinstaller -
"pyinstaller --onefile --noconsole main.py"
It creates main.exe which shows "Failed to execute script" on running. Please provide me a detailed explanation on what should I do to achieve what I have stated above. Thank you in advance.
pyinstaller uses a few dirty tricks to compress a bunch of files into one
I recommend using cx_Freeze instead along with inno setup installer maker
do pip install cx_Freeze to install that and go here for inno setup
then copy the following into a file named setup.py in the same folder as your project
from cx_Freeze import setup, Executable
setup(name = "YOUR APP NAME" ,
version = "1.0.0" ,
description = "DESCRIPTION" ,
executables = [Executable("PYTHON FILE", base = "Win32GUI")]
)
lastly run python setup.py build
if you want as onefile download this file here
just edit the file a bit and use inno compiler to make into installer
Suppose our project has the following structure.
MyApp
|-models
| |-login.kv
|-data
| |-words.json
| |-audio.tar.gz
|-fonts
| |-FredokaOne.ttf
|-images
| |-gb.pngsound.png
| |-icon.ico
|-main.py
|-main.kv
|-draw.py
|-image.py
and depends on the following packages:
- kivy
- kivymd
- ffpyplayer
- gtts
First things first is to install cx_Freeze.
pip install cx_Freeze
Copy the following into a file named setup.py in the same folder as your project.
# https://cx-freeze.readthedocs.io/en/latest/distutils.html
import sys
from cx_Freeze import setup, Executable
includes = []
# Include your files and folders
includefiles = ['models/','data/','fonts/','images/','main.kv','draw.py','image.py']
# Exclude unnecessary packages
excludes = ['cx_Freeze','pydoc_data','setuptools','distutils','tkinter']
# Dependencies are automatically detected, but some modules need help.
packages = ['kivy','kivymd', 'ffpyplayer','gtts']
base = None
shortcutName = None
shortcutDir = None
if sys.platform == "win32":
base = "Win32GUI"
shortcutName='My App'
shortcutDir="DesktopFolder"
setup(
name = 'MyApp',
version = '0.1',
description = 'Sample python app',
author = 'your name',
author_email = '',
options = {'build_exe': {
'includes': includes,
'excludes': excludes,
'packages': packages,
'include_files': includefiles}
},
executables = [Executable('main.py',
base = base, # "Console", base, # None
icon='images/icon.ico',
shortcutName = shortcutName,
shortcutDir = shortcutDir)]
)
Lastly run.
python setup.py build
This command will create a subdirectory called build with a further subdirectory starting with the letters exe. and ending with the typical identifier for the platform that distutils uses. This allows for multiple platforms to be built without conflicts.
On Windows, you can build a simple installer containing all the files cx_Freeze includes for your application, by running the setup script as:
python setup.py bdist_msi
Cx_freeze references
Doc
Git Hub
I am using python 3.5.1 and the unofficial cx_freeze 5.0 build available from here. I am trying to create an executable version of a python project using tkinter and sympy that I've been working on. I used cxfreeze-quickstart to create a setup.py file for the program, and in terms of building what at least seems to be a valid executable, it works without throwing any errors. However, when I try to run the executable, nothing happens. I know similar questions have been asked on here and I've looked at and tried to understand every one I've found, but none of the solutions have worked for me. I don't understand what's going on, and any help would be appreciated. Code below:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('c:\\users\\joe\\pycharmprojects\\physics2-0\\physics2-0.py', base=base,
targetName = 'c:\\users\\joe\\pycharmprojects\\physics2-0\\physics.exe')
]
setup(name='physics solver',
version = '0.1',
description = 'alpha physics solver',
options = dict(build_exe = buildOptions),
executables = executables)
Thanks in advance.
UPDATE: I am now attempting to write the setup.py script myself according to the template provided in the docs, although any help would still be greatly appreciated.
UPDATE 2: I wrote my own setup.py according to the template provided in the docs, and put it in the same folder as the script I want to freeze, something I hadn't realized I needed to do. I ran python setup.py build in command line, and it created the build subdirectory with the exe and DLLs. However, now when I try and run the exe, an error message pops up that says ImportError: DLL load failed. The specified module could not be found. in reference to tkinter. The code for the 2nd setup.py is below.
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["tkinter", "sympy", "_tkinter"], "excludes": []}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == 'win32':
base = "Win32GUI"
setup( name = "physics solver",
version = "0.1",
description = "a basic physics solver",
options = {"build_exe": build_exe_options},
executables = [Executable("Physics2-0.py", base=base)])
Below are the first 4 lines of physics2-0.py. The line brought into question by the error message is line 1.
from tkinter import *
from tkinter import ttk
from sympy import Symbol
from sympy.solvers import solve
UPDATE 3: Someone please help me out here. I can't figure this out. I've even done a clean re-install of python at this point, just to be sure I didn't accidentally mess something up at some point, and it's still giving me the same error message.
I created a new script with cx_freeze's template. Then I'm running python setup.py build to create the exe. If I move main.exe to the root folder cx_freeze test it will fail to run.
All I want is to move .exe up 1 or two directories.
Here's my code:
main.py
foo = input("Complete")
setup.py:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(
packages = [],
excludes = [],
includes = ["atexit"]
)
# include_files=[]
base = 'Console'
executables = [
Executable('main.py', base=base, targetName = 'main.exe')
]
setup(name='freeze test',
version = '1',
description = '.',
options = dict(build_exe = buildOptions),
executables = executables)
I thought http://cx-freeze.readthedocs.org/en/latest/faq.html#using-data-files might have some help, but since the files are in the subdirectory, I can't use os module?
That feature is not available in 4.x but will be available in 5.x when it is made available. The current source has this capability so if you can compile it you can have it today; otherwise, you'll need to wait for the official release which I hope will be soon.
I'm trying to create a binary on Linux (Manjaro Linux, x86_64, python 3.4).
My app is a GUI software, written with PyQt.
Here is my setup.py:
import sys
import os
from cx_Freeze import setup, Executable
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
my_data_files = ["./images/", "./journals/", "./config/"]
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"],
"excludes": [
"tkinter"
],
'includes': [
'sip',
'PyQt4.QtCore',
'PyQt4.QtGui',
'PyQt4.QtNetwork',
'PyQt4.QtSql',
'scipy.sparse.csgraph._validation',
'sklearn.utils.sparsetools._graph_validation',
'scipy.special._ufuncs_cxx'
],
'include_files': my_data_files
}
setup(name = "guifoo",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("gui.py", base=base)])
For now, I'm just starting. The "includes" part in the options is what I used when I compiled my binary with py2exe (it worked, but I want a unique tool to compile for all the platforms).
When I start the compilation with
python setup.py build
everything seems to work fine, but when I try to start the binary, I have this exception:
NotADirectoryError: [Errno 20] Not a directory: '/home/djipey/Desktop/test/build/exe.linux-x86_64-3.4/library.zip/text_unidecode/data.bin'
So I assume I have a problem with the module text_unidecode, but I can't really identify what the problem is.
Could you give me a hand please ?
EDIT:
Ok, sorry for the lack of precision, I didn't copy/paste the whole error message:
File "/usr/lib/python3.4/site-packages/text_unidecode/__init__.py", line 6, in <module>
with open(_data_path, 'rb') as f:
NotADirectoryError: [Errno 20] Not a directory: '/home/djipey/Desktop/test/build/exe.linux-x86_64-3.4/library.zip/text_unidecode/data.bin'
I think the issue can come from text_unidecode, but I don't know why. I installed it without any problem on my computer.
https://github.com/kmike/text-unidecode/blob/master/src/text_unidecode/init.py
EDIT 2:
If I integrate the code of text-unidecode (it is basically a single function) in my own code, it works. I think I know why I have this issue. In text-unidecode, there is a file called "data.bin" which contains data used by the function of text-unidecode. It is a part of the library, but it is not added to library.zip when I use cx_freeze. So text-unidecode can't work.
Is there an elegant way to solve this with cx_freeze ? Like an option, to add data files to library.zip ?
I want to make a self-contained .exe file.
I have managed to use cx_Freeze to build one that works on my machine, but it is throwing an error about needing the .dlls when I sent it to someone. I read a few of the similar questions, which is how I ended up including packages in the build options.
I suspect that once I get past this particular problem, I will end up needing to include other stuff in the .exe....any help getting around that pitfall is appreciated! The end user needs to be able to only use the .exe and not have to install other files.
Here is my current setup.py:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
build_options = {"includes" : [ "re", "atexit"], "packages": ["PyQt4.QtCore", "PyQt4.QtGui"]}
setup( name = "Hex Script Combination",
version = "0.1",
description = "Contact (info) with questions",
options = {"build_exe" : build_options},
executables = [Executable("Project.py", base=base)])
ETA:
I tried IExpress, and I'm running into this error:
(Picture uploaded but for some reason, neither picture in this post is showing)
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in <module>
code = importer.get_code(moduleName)
ZipImportError: can't find module 'projec~1__main__'
I did NOT find a way to do exactly what I wanted. I did, however, discover that I was getting an installer I wasn't aware of for distribution that did install everything that was in my exe directory.
File path was ~\dist, and it contained only an .msi file. Launching it installed everything that was in ~\build\exe.win32-2.7