distutils - How to add a .pyw script if windows - python

I am distributing a simple library/application which includes a script with GUI. Under windows I want this to be run by pythonw.exe, preferrably by making it a .pyw file.
root/
lib/
lib.py
guiscript.py
setup.py
I want the user to be able to install guiscript in any path.
I stole this hook from this question:
from distutils.core import setup
from distutils.command.install import install
import os, sys
class my_install(install):
def run(self):
install.run(self)
try:
if (sys.platform == "win32") and sys.argv[1] != "-remove":
os.rename("guiscript.py",
"guiscript.pyw")
except IndexError:pass
setup(...
cmdclass={"install": my_install})
But this doesn't work because it changes the name of guiscript.py in the source folder, because the path is relative to setup.py.
Is there a reasonable way to get the script install-path, or alternativly a simple way to find guiscript.py (it's not given it's in PYTHONPATH).
So because I don't have 50 karma i can't answer my own post in 7 hours but here it is:
Okay, I found the solution. I can delete the question if you want, but for now I'll keep it in case someone else has the same question.
from distutils.core import setup
from distutils.command.install_scripts import install_scripts
import os, sys
class my_install(install_scripts):
"""Change main script to .pyw after installation.
If sys.argv == '-remove'; it's ran as uninstall-script.
Override run() and then call parent."""
def run(self):
install_scripts.run(self)
try:
if (sys.platform == "win32") and sys.argv[1] != "-remove":
for script in self.get_outputs():
if script.endswith("guiscript.py"):
os.rename(script, script+"w")
except IndexError:pass
setup(...
cmdclass={"install_scripts": my_install}
)

Related

Is there a way to interrupt shutil copytree operation in Python?

I'm fairly new to programming in general. I need to develop a program that can copy multiple directories at once and also take into account multiple file type exceptions. I came across the shutil module which offers the copytree and ignore_patterns functions. Here is a snippet of my code which also uses the wxPython Multiple Directory Dialog:
import os
import wx
import wx.lib.agw.multidirdialog as MDD
from shutil import copytree
from shutil import ignore_patterns
app = wx.App(0)
dlg = MDD.MultiDirDialog(None, title="Custom MultiDirDialog", defaultPath=os.getcwd(), agwStyle=MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST)
dest = "Destination Path"
if dlg.ShowModal() != wx.ID_OK:
dlg.Destroy()
paths = dlg.GetPaths()
ext = ['*.tiff', '*.raw', '*.p4p', '*.hkl', '*.xlsx']
for path in enumerate(paths):
directory = path[1].replace('Local Disk (C:)','C:')
copytree(directory, dest, ignore=ignore_patterns(directory, *ext))
dlg.Destroy()
app.MainLoop()
This code works well for me. At times, I'll be copying terabytes worth of data. Is there anyway that the shutil.copytree can be interrupted? I ask this, because the first time I ran this program, I selected a rather large directory and copied a ton of files (Successfully!) by accident and wanted to stop it :( . Once I get around this, I'll finally start on the GUI! If there is anymore information that I can provide, please let me know! Thanks in advance for any and all help!
You can run the copy in separate python process using multiprocessing module. The code may look something like this:
import time
import shutil
from multiprocessing import Process
def cp(src: str, dest: str):
shutil.copytree(src, dest)
if __name__ == '__main__':
proc = Process(target=cp, args=('Downloads', 'Tmp'), daemon=True)
proc.start()
time.sleep(3)
proc.terminate()
In my example the main process starts a child process, which does the actual coping, and after 3 seconds terminates it. Also you can check if the process is running by calling is_alive() method of the process.
copytree accepts copy_function as a parameter. If you pass a function that checks for a flag you could raise an error to interrupt the operation.
from shutil import copytree, copy2
# set this flag to True to interrupt a copytree operation
interrupt = False
class Interrupt(Exception):
""" interrupts the copy operation """
def interruptable_copy(*args, **kwargs):
if interrupt:
raise Interrupt("Interrupting copy operation")
return copy2(*args, **kwargs)
copytree(src, dst, copy_function=interruptable_copy)

Python - How to stop child process when the parent script is stoped

How can I exit child process when parent process is stoped?
Following is my code.
I want to stop all execution when there is KeyboardInterrupt.
import os, sys
scripts = ["script_1.py","script_2.py"]
try:
for script in scripts:
command = 'python ' + script
os.system(command)
except KeyboardInterrupt:
os._exit(1)
except Exception as e:
raise e
Since you are trying to execute Python scripts within another Python script, let's do this in a Pythonic way and get rid of os.system for importlib.import_module
import os
import importlib
scripts = ['script_1.py', 'script_2.py']
for filename in scripts:
modulename, ext = os.path.splitext(filename)
importlib.import_module(modulename)
If your scripts are like:
if __name__ == '__main__':
# code here
print('hello')
It will not works, because the if __name__ == '__main__': is here to ensure the part under if will be executed only if the file is executed as a script (and not as a module).
So, in this case the better thing to do is something like:
script_1.py:
def main():
# code here
print('hello')
And in the main script:
import os
import importlib
scripts = ['script_1.py', 'script_2.py']
for filename in scripts:
modulename, ext = os.path.splitext(filename)
module = importlib.import_module(modulename)
module.main()

Unable to create a functional executable with PyInstaller and PyQt

I have tried, for a lot of time now, to create an executable for a Python project. In this project, I need to use:
PyQt(4) : for my GUI,
PySerial : to communicate with an arduino,
Subprocess : to launch some avr things with a .bat file
In fact, the executable is created, but when I try to start it nothing happens, except my mouse tell me that she is occupied.
So, I tried to figured out from where could be the problem, by writing some basic programs, which condense every functions I need for my project. Everything is functional when I launch this from python (3.5), but doesn't when I execute the file generated by pyinstaller. (The interface.py file is here, in a pastebin.com file, if you want, I thought it's not very relevant : it's only a form with a pushbutton)
from PyQt4 import QtGui
from interface import Ui_Form
import serial
import subprocess
import sys, os
class win(QtGui.QWidget, Ui_Form):
"""docstring for win"""
def __init__(self):
super(win, self).__init__()
self.setupUi(self)
self.ser = serial.Serial("COM3", 9600)
self.pathBat = "cmd.bat"
def on_pushButton_clicked(self):
#if (self.ser.isOpen() and self.serAvr.isOpen()):
if True:
self.ser.write("start".encode())
p = subprocess.call(self.pathBat, creationflags=subprocess.CREATE_NEW_CONSOLE, **self.subprocess_args())
if p == 1:
self.writeLog("Works")
self.ser.write("stop".encode())
#self.writeLog(p.returncode)
def subprocess_args(include_stdout=True):
# The following is true only on Windows.
if hasattr(subprocess, 'STARTUPINFO'):
# On Windows, subprocess calls will pop up a command window by default
# when run from Pyinstaller with the ``--noconsole`` option. Avoid this
# distraction.
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Windows doesn't search the path by default. Pass it an environment so
# it will.
env = os.environ
else:
si = None
env = None
ret = {}
# On Windows, running this from the binary produced by Pyinstaller
# with the ``--noconsole`` option requires redirecting everything
# (stdin, stdout, stderr) to avoid an OSError exception
# "[Error 6] the handle is invalid."
ret.update({'stdin': subprocess.PIPE,
'stderr': subprocess.PIPE,
'startupinfo': si,
'env': env })
return ret
app = QtGui.QApplication(sys.argv)
v = win()
v.show()
sys.exit(app.exec_())
I added "cmd.bat" to the data in .spec file for pyinstaller, and the function subprocess_arg is here to avoid problems with subprocess (as mentionned on the documentation here)
Firstly I thought the problem was linked to subprocess, I tried to delete all the references to it, still not working. Same for Serial. Moreover, I tried to debug the executable by setting debug = True in the .spec file, but if I try to execute the file from the console, nothing happend at all, it stays blocked on the first line.
So if anybody can help ! thank you in advance !
Maybe the "frozen" application does not find the "cmd.bat"!? You could test it by replacing it with the absolute path.
Your executable unpacks the "cmd.bat" in a temporary folder accessible in python with sys._MEIPASS. You should find your files with something like os.path.join(sys._MEIPASS, "cmd.bat") !?
In case you need it: getattr(sys, 'frozen', False) indicates whether your code is frozen or not (but only for PyInstaller).

How to install a Python Windows service using cx_Freeze?

I currently have a Python file that when run using python file_name.py installs a Windows service that is viewable in Event Viewer under application logs and stoppable using sc stop service_name. However, when converted into an executable using cx_Freeze, the executable runs with no errors but the service no longer installs. This happens if I run just the executable by itself, if I run service_name.exe --install service_name, or if I run sc create service_name binPath=service_path
My setup.py file looks something like:
from cx_Freeze import setup, Executable
options = {
'build_exe': {
'packages': ['packagename'],
'includes': ['ServiceHandler', 'cx_Logging']}
}
setup(name='cx_FreezeSampleService',
version='0.1',
description='Sample cx_Freeze Windows serice',
executables=Executable('Config.py', base='Win32Service',
targetName='cx_FreezeSampleService.exe'),
options=options
)
My Config.py looks something like:
NAME = 'cx_FreezeSampleService%s'
DISPLAY_NAME = 'cx_Freeze Sample Service - %s'
MODULE_NAME = 'ServiceHandler'
CLASS_NAME = 'Handler'
DESCRIPTION = 'Sample service description'
AUTO_START = True
SESSION_CHANGES = False
And finally, my ServiceHandler.py looks something like:
class Handler(object):
def Initialize(self, Config):
pass
def Run(self):
#code to run service
def Stop(self):
#code to stop service
This code follows the example at the cx_Freeze source code here (https://bitbucket.org/anthony_tuininga/cx_freeze/src/1282b6b6ee637738210113dd88c3c198d475340f/cx_Freeze/samples/service/?at=default) almost exactly, but neither this nor the example seem to work in actually installing a service.
Thank you in advance!
This is an old question, but I manage to get it working as a window service for a simple flask application with the help of the developers.
[https://github.com/marcelotduarte/cx_Freeze/tree/master/cx_Freeze/samples/service]
You have to set up all the windows service actions you want performance.
this is how the ServiceHandler.py should look like as a template you still need to adapt to run your application.
"""
Implements a simple service using cx_Freeze.
See below for more information on what methods must be implemented and how they
are called.
"""
import threading
import os
import sys
import cx_Logging
class Handler:
# no parameters are permitted; all configuration should be placed in the
# configuration file and handled in the Initialize() method
def __init__(self):
self.stopEvent = threading.Event()
self.stopRequestedEvent = threading.Event()
# called when the service is starting
def initialize(self, configFileName):
self.directory = os.path.dirname(sys.executable)
cx_Logging.StartLogging(os.path.join(self.directory, "teste.log"), cx_Logging.DEBUG)
#pass
# called when the service is starting immediately after Initialize()
# use this to perform the work of the service; don't forget to set or check
# for the stop event or the service GUI will not respond to requests to
# stop the service
def run(self):
cx_Logging.Debug("stdout=%r", sys.stdout)
sys.stdout = open(os.path.join(self.directory, "stdout.log"), "a")
sys.stderr = open(os.path.join(self.directory, "stderr.log"), "a")
self.stopRequestedEvent.wait()
self.stopEvent.set()
# called when the service is being stopped by the service manager GUI
def stop(self):
self.stopRequestedEvent.set()
self.stopEvent.wait()

Python win32 service

I have a minimal python win32 service service.py that does nothing special:
import win32serviceutil
import win32service
import win32event
class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "SmallestPythonService"
_svc_display_name_ = "display service"
# _svc_description_='ddd'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
if __name__=='__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService)
When I run:
service.py install
service.py start
it works fine but when I compile the service.py file with py2exe to service.exe and run the following:
service.exe install
service.exe start [or trying to restart the service from the Services.msc]
I get this message:
Could not start the service name service on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion
How can I resolve this problem?
Also here the distutil code:
from distutils.core import setup
import py2exe
py2exe_options = {"includes": ['decimal'],'bundle_files': 1}
setup(console=[{"script":'Service.py'}],
options={"py2exe": py2exe_options},
zipfile = None,
},
)
Replace your: setup(console=[{"script":'Service.py'}] with setup(service=[{"script":'Service.py'}]. Instead of console use service.
try this setup:
py2exe_options = {"includes": ['decimal'],'bundle_files': 1}
setup(
service=[{'modules':'Service.py','cmdline_style':'pywin32','description':'your service description'}],
options={'py2exe':py2exe_options},
zipfile=None)
A quick google came up with this: http://islascruz.org/html/index.php?gadget=StaticPage&action=Page&id=6
It has Italian comments, but I can help you translate some stuff if you don't know Italian.
To truly debug your problem, I guess we will need to see your setup.py distutils script...
You probably might be missing the right PATH for finding all the DLLs required by the service. Usually the service gets installed as a 'LocalSystem' service so you need to add the PATH to the System (and not to User).
Try adding c:\python27 (or whatever the path to your python dlls is) to the SYSTEM PATH, restart the computer and check if it now starts fine.

Categories