How does cxFreeze include internal project modules? - python-3.x

Update:
I suspect this is an error in cxFreeze as I understand this should
go automatically.
end update
Update:
I missed an error given by cxFreeze:
Missing modules:
? Test.MyClass imported from main__main__
end update
I'm not sure what the proper term is for modules within project unlike sys or PyQt, so I'm going with internal project modules.
I have some example code below where I recieve the error "ImportError: cannot import name MyClass." and I would love to know how to get cxFreeze to compile that 'Test.py' module.
Here's my main code:
Main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
#from guiObjects.MainWindow import MainWindow
from Test import MyClass
if __name__ == "__main__":
# Initializing the main window
app = QApplication(sys.argv)
widget = QMainWindow()
#mainWindow = MainWindow(widget)
test = MyClass()
widget.show()
sys.exit(app.exec_())
Test.py
class MyClass(object):
def __init__(self):
pass
__init.py__
'''empty'''
Setup.py
import sys
from cx_Freeze import setup, Executable
path_platforms = ( "C:\Python33\Lib\site-packages\PyQt5\plugins\platforms\qwindows.dll", "platforms\qwindows.dll" )
includes = ["re","sip","atexit","PyQt5.QtCore","PyQt5.QtGui"]
includefiles = [path_platforms]
excludes = [
'_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter'
]
packages = ["os"]
path = []
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"includes": includes,
"include_files": includefiles,
"excludes": excludes,
"packages": packages,
"path": path
}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
exe = None
if sys.platform == "win32":
exe = Executable(
script="../Main.py",
initScript = None,
base="Win32GUI",
targetDir = r"dist",
targetName="Main.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
setup(
name = "Main",
version = "0.1",
author = 'me',
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [exe]
)

This problem happens when you run setup.py in a subfolder of where Main.py is located.
I now placed my setup.py in the same folder as Main.py. and changed my .bat file to python ../setup.py build install.
This seems to be a bug in cx_Freeze as it works fine for Python 2.7, but not Python 3.3.

Your test.py is wrong, you can't leave functions empty, try
class MyClass(object):
def __init__(self):
pass
and in setup.py mabye add "Test" into "includes"
includes = ["re","sip","atexit","PyQt5.QtCore","PyQt5.QtGui", "Test"]

Related

Import internal exe modules from an external subprocess module (pyinstaller)

I'd like to import internal exe modules from an external process. I can do this fine from an IDE, but when I package the project with pyinstaller, then run it, the modules can't be found.
The high level flow looks like this:
Importing maya_app from within the external userSetup.py returns an error: ImportError: No modules named maya_app
This post seems like it should help me but there's something about my setup that's preventing this solution from working. That or I'm just doing it wrong.
Is it even possible to import modules from one exe into another exe? Thanks!
Code:
launch_maya.py
import os
import sys
import subprocess
from PySide2 import QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
# GUI
btn_launch = QtWidgets.QPushButton('launch maya')
btn_launch.clicked.connect(self.on_launch)
# Layout
main_layout = QtWidgets.QHBoxLayout(self)
main_layout.addWidget(btn_launch)
self.setLayout(main_layout)
# Root path exe vs ide
if getattr(sys, 'frozen', False):
self.root_path = sys._MEIPASS
else:
self.root_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
def _set_app_envs(self):
_envs = os.environ.copy()
_envs['MAYA_SCRIPT_PATH'] = os.path.join(self.root_path, 'scripts').replace('\\', '/')
# Python path envs
_python_path_list = [
'C:\\Program Files\\Autodesk\\Maya2020\\Python\\Lib\\site-packages',
'C:\\Program Files\\Autodesk\\Maya2020\\Python\\DLLs',
os.path.join(self.root_path, 'scripts').replace('\\', '/'),
os.path.join(self.root_path, 'internal_source', 'maya_app')
]
# PYTHONPATH exe vs ide
if getattr(sys, 'frozen', False):
_envs['PYTHONPATH'] = os.pathsep.join(_python_path_list)
_envs['PYTHONHOME'] = 'C:\\Program Files\\Autodesk\\Maya2020\\bin'
else:
_envs['PYTHONPATH'] += os.pathsep + os.pathsep.join(_python_path_list)
return _envs
def on_launch(self):
# Maya file path
file_path_abs = '{}/scenes/test.mb'.format(self.root_path).replace('\\', '/')
print(file_path_abs)
app_exe = r'C:/Program Files/Autodesk/Maya2020/bin/maya.exe'
_envs = self._set_app_envs()
if os.path.exists(file_path_abs):
proc = subprocess.Popen(
[app_exe, file_path_abs],
env=_envs,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Widget()
window.resize(400, 400)
window.show()
sys.exit(app.exec_())
bundle.spec
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
added_files = [
('./scenes', 'scenes'),
('./scripts', 'scripts')
]
a = Analysis(['launch_maya.py'],
pathex=[
'D:/GitStuff/mb-armada/example_files/exe_bundle',
'D:/GitStuff/mb-armada/dependencies/Qt.py',
'D:/GitStuff/mb-armada/venv/Lib/site-packages',
],
binaries=[],
datas=added_files,
hiddenimports=['internal_source', 'internal_source.maya_app'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='bundle',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='bundle')
maya_app.py
import os
import sys
import subprocess
from PySide2 import QtWidgets
class MainWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
# GUI
btn_launch = QtWidgets.QPushButton('say hey')
btn_launch.clicked.connect(self.on_say_hey)
# Layout
main_layout = QtWidgets.QHBoxLayout(self)
main_layout.addWidget(btn_launch)
self.setLayout(main_layout)
print('I should be alive')
def on_say_hey(self):
print('hey')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWidget()
window.resize(100, 100)
window.show()
sys.exit(app.exec_())
userSetup.py
import os
import sys
import maya.cmds as mc
print('hey')
def tweak_launch(*args):
print('Startup sequence running...')
os.environ['mickey'] = '--------ebae--------'
print(os.environ['mickey'])
root_path = os.getenv('_MMM_ROOT_PATH')
main_app_path = os.path.join(root_path, 'internal_source')
if not root_path in sys.path:
sys.path.append(main_app_path)
from internal_source import maya_app
w = maya_app.MainWidget()
w.show()
print('window should be up')
mc.evalDeferred("tweak_launch()")
At the highest level, this seems like it's really just about path management within your launched maya -- presumably the launcher app knows it's own paths, and the maya you want to fire off needs to be told about their existence.
The most reliable and least magic-prone way to do this is just to have your launcher unbundle any scripts scripts it needs to share with maya proper to a known location -- a hidden directory or even a session-specific temp directory will give you the maximum likelihood that you've got the latest version as your launcher revs.
When launching maya you can pass the paths a few ways -- probably the easiest is just to use site.addsitedir to put them on the path for that session rather than relying on Maya's many other possible search locations. You can launch the maya with the -c mel flag and pass a python command at startup, so you can control this behavior entirely from the launcher and not have to worry about interactions with userSetup.py. Something along these lines would make your stuff available to maya:
# unpack the 'payload' of scripts to share to a
# known location with something like pkgutil.get_data()
# https://docs.python.org/3/library/pkgutil.html
# note the single quotes -- you need them to handle escaping!
py_cmd = "import site; site.addsitedir('{}'); import my_startup_module"
python_to_execute = py_cmd.format(path_to_unpacked_modules)
# again, note annoying escapes
start_cmd = '\"python(\"{}\");\"'.format(python_to_execute)
maya_session = subprocess.Popen(['maya.exe', '-c', start_cmd])
If your exe has unpacked the files to path_to_unpacked_modules, maya will run that mel command, which will call python, add your module dir as a site-packages directory, and then import and run my_startup_module (from that location). This lets your launcher control the startup process and keeps you from having to fiddle separately with userSetup.py -- plus, since it does not touch env vars, you don't have to work to hard to run different sessions side by side.
If the code you want to share is just a bunch of modules (no binary extensions) you can keep your modules in a zip file and add that to the path -- python will be able to find modules inside the zip file automatically. That saves unpack time and also ensure you don't have leftover PYC files from older runs.
There's a lot of useful info in these threads:
https://tech-artists.org/t/python-maya-startup-script/2145/17
https://tech-artists.org/t/deploy-tools-for-your-maya-team/5029/13

Packaging Python file with cx_Freeze

I am trying to package my Python code into an exe file but after following all the instructions on the link below, I managed to build the an exe file but when I try to run it, nothing happens.
https://cx-freeze.readthedocs.io/en/latest/distutils.html
My setup.py file is
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os","tkinter", "pandas" , "openpyxl", "datetime", "time"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "login",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("login.py", base=base)])
My login file runs without any error on Anaconda.
Can someone comment what may cause the problem?

bdist_mac cx_freeze with tkinter and selenium interaction

I created a python 3 application that has a tkinter GUI and selenium action upon clicking a button. Here is the setup.py file:
from cx_Freeze import setup, Executable
import sys
import os
from pathlib import Path
buildOptions = {"include_files":['execute.py'], "packages": ['encodings'], "includes": ['numpy.core._methods', 'numpy.lib.format'], "excludes": []}
if sys.platform=='win32':
base = "Win32GUI"
else:
base=None
executables = [
Executable('bot_gui.py', base=base)
]
home_path = str(Path.home())
user_txt_path = []
if sys.platform=='win32':
print("windows detected")
user_txt_path.append('\\user.txt')
else:
print("macos detected")
user_txt_path.append('/user.txt')
#for initialization
f = open(home_path + user_txt_path[0], "w+")
for i in range(12):
f.write("---\n")
setup(name='tachysloth',
version = '1.0',
description = 'test',
options = dict(build_exe = buildOptions),
executables = executables)
The problem is when I run python setup.py build, everything works fine; but when I run python setup.py bdist_mac, the GUI works when you click on the application (called tachysloth-1.0 below)
but when I click on a button to in the GUI to open google.com on Chrome, the button does not do anything.
If you need more information to assess this problem, please let me know and I will provide that information as soon as possible.
Thank you again.

Tkinter not found after cx_Freeze process

I wrote the folowwing at the top of my script:
try:
from Tkinter import *
from Tkinter import messagebox
except ModuleNotFoundError:
from tkinter import *
from tkinter import messagebox
else:
print("tkinter error")
input()
sys.exit()
Running my script as .py or .pyw works without problems. But when I make and .exe out of it with cx_Freeze, the exe throws the error:
And why is that? how to solve this?
Thanks...
EDIT:
Also my setup.py:
import sys
import os
from cx_Freeze import setup, Executable
os.environ['TCL_LIBRARY'] = r"C:\Python\Python36-32\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = r"C:\Python\Python36-32\tcl\tk8.6"
base = None
executables = [Executable("toolbar.py", base=base)]
packages = []
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "Desktop Toolbar",
options = options,
version = "1.0",
description = " ",
executables = executables
)

Cannot fix pywintypes.error: (2, 'BeginUpdateResource', 'The system cannot find the file specified.')

I am trying to build an .exe file of my Python application. I have tried various ways to create a setup file from both Github and Stackoverflow and I keep getting this same error:
pywintypes.error: (2, 'BeginUpdateResource', 'The system cannot find the file specified.').
I am using Python 3.6.1, and cx_Freeze to build the application.
Here are the different setup files I have tried:
Attempt 1:
import os.path
from cx_Freeze import setup, Executable
os.environ['TCL_LIBRARY'] = r'C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'
executables = [Executable("myprogram.py", base="Win32GUI")]
options = {
'build_exe': {
'include_files': [
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
]
},
}
setup(
name="Standalone.exe",
options=options,
version="0.1",
description='Simple tkinter application',
executables=executables, requires=['cx_Freeze', 'cx_Freeze']
)
Attempt 2:
from cx_Freeze import setup, Executable
import sys
import os
productName = "My Program"
if 'bdist_msi' in sys.argv:
sys.argv += ['--initial-target-dir', r'C:\Users\RedCode\PycharmProjects\Standalone' + productName]
sys.argv += ['--install-script', 'myprogram.py']
os.environ['TCL_LIBRARY'] = r'C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'
exe = Executable(
script="myprogram.py",
base="Win32GUI",
targetName="Product.exe"
)
setup(
name="Standalone.exe",
version="1.0",
author="Me",
description="Copyright 2012",
executables=[exe],
scripts=[
'myprogram.py'
]
)
Attempt 3:
import sys
import os
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "includes": ["tkinter"]}
def s_SourceFile():
if (sys.argv[0] == ''):
return __file__
else:
return sys.argv[0]
os.environ['TCL_LIBRARY'] = r'C:\Users\stefano.demattia\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\stefano.demattia\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'
setup(name="Standalone",
version="0.1",
description="My GUI application!",
options={"build_exe": build_exe_options},
executables=[Executable("myprogram.py", base="Win32GUI", targetName="Standalone.exe")])
Attempt 4:
application_title = "Car Database"
main_python_file = "cardb.py"
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "myprogram.py",
version = "0.1",
description = "Simple embedded database application",
executables = [Executable("myprogram.py", base = base)])
I have also tried pyinstaller but it keeps giving me a tuple out of range error no matter what I do, and I tried bbfreeze which I can't even get to install. I have checked my files and everything is there, so I do not see how it can still tell me a specified file is not found.
What else can I do or how can I edit the above attempts so that the application actually builds properly? Is there even a way to determine which file is missing?
Remove the version, I got the same error, looked at the code and noticed it was referencing versioning in the source, so thought I'd try it, and it fixed it for me.
I know the poster probably no longer needs this, but posting it for others that come across this in the future since I didn't see the answer anywhere else.
cx_Freeze.setup(
name="SampleName",
options={"build_exe": {"packages": ["tkinter", "os"],
"include_files": ['tcl86t.dll', 'tk86t.dll', 'closenew.png', 'sujata.png', 'config.py', 'ui_attributes.py']}},
version="0.01", # Remove this line
description="Tkinter Application",
executables=executables
)
I have removed the version and it worked.
cx_Freeze.setup(
name="SampleName",
options={"build_exe": {"packages": ["tkinter", "os"],
"include_files": ['tcl86t.dll', 'tk86t.dll', 'closenew.png', 'sujata.png', 'config.py', 'ui_attributes.py']}},
description="Tkinter Application",
executables=executables
)

Resources