Unable to start AMS Acomba Connector on Windows - python

I am trying to get a python service to run under Windows 7 however I am unable to start it.
I have started by installing the service using
c:\amsconnector>amsconnector.py --username domain\admin --password ************ install
Installing service AMS Acomba Connector
Service installed
After that, I run
c:\amsconnector>amsconnector.py start
Starting service AMS Acomba Connector
When I check the running services list, I notice that the service is not running and when checking the event log, I am presented with this stacktrace
Python could not import the service's module
Traceback (most recent call last):
File "C:\amsconnector\amsconnector.py", line 343, in <module>
win32serviceutil.HandleCommandLine(AMSConnectorService)
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 521, in HandleCommandLine
usage()
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 505, in usage
sys.exit(1)
SystemExit: 1
%2: %3
The line 343 of amsconnector.py looks like this
if True or __name__ == '__main__':
win32serviceutil.HandleCommandLine(AMSConnectorService)
After checking out the source code of win32serviceutil.py, it appears that the service is just printing out the win32service usage dialog instead of actually running the python class. I am completely confused!

Thanks roeland!
I was able to fix the issue by simply removing the "True or" part of the last line leaving it like this:
if True or __name__ == '__main__':
win32serviceutil.HandleCommandLine(AMSConnectorService)

Related

Issues running working python script from systemd on Raspberry Pi

I have a python script, using pygame and pyautogui which works when run through terminal or any IDLE on my Raspberry Pi 3 Model B V1.2. The script opens a webpage, and reads joystick inputs through pygame.
I want the script to run at boot after a network is connected, so have created a service in /etc/systemd/system/.
When run by the service, the script has an error when using various libraries including pygame and pyautogui.
My service is as follows:
[Unit]
Description=My magic service
After=multi-user.target
Requires=network.target
[Service]
Type=simple
User=pi
ExecStart=/usr/bin/python3 /home/pi/FinalCode.py
Restart=always
StandardOutput=file:/tmp/FinalTests.log
StandardError=inherit
[Install]
WantedBy=multi-user.target
The welcome message from pygame prints (Hello from the pygame community... etc), however it the returns the following error:
Traceback (most recent call last):
File "/home/pi/FinalCode.py", line 89, in <module>
for event in pygame.event.get(): # read joystick commands
pygame.error: video system not initialized
I then commented out all the pygame, but a similar issue also occurs with pyautogui, which returns the following error:
Traceback (most recent call last):
import pyautogui
File "/home/pi/.local/lib/python3.9/site-packages/pyautogui/__init__.py", line 249, in <module>
import mouseinfo
File "/home/pi/.local/lib/python3.9/site-packages/mouseinfo/__init__.py", line 223, in <module>
_display = Display(os.environ['DISPLAY'])
File "/usr/lib/python3.9/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DISPLAY'
I have checked "sys.prefix" and "sys.base_prefix" and both in the terminal and from the service they are "/usr" (I think this shows that it is running in the same environment?). Both the service and idle/terminal are running Python 3.9.2 through "/usr/bin/python3"
How do I get the systemd service to run in exactly the same way as the terminal or IDLE running the script?
The basic problem you're running into is that the Systemd unit has no idea where you want this thing displayed. It is missing the DISPLAY environment variable, which is set when Xorg is running but will not be set otherwise.
I think this answer to a similar question might help you out. Long story short - Make your service a user service and change some dependencies and then import the variables from where ever you start Xorg (Login managers have some way of handling this but I only ever really used startx until I switched to wayland and so I have little to offer here).
Found a solution that works, the issue was as bbenne10 suggested.
With hindsight, somewhat unsurprisingly, pyautogui also needs the display so had no issues afterwards.

pywin32 cannot debug if project is packed into .exe

I have my project installed as a windows service and everything worked smoothly. I had some errors with using venv and pywin32, but eventually figuret that out. Stackoverflow link
However when I use pyinstaller to make onefile executable from it problems start occuring.
I use following command to create executable:
[Path_to_my_venv_pyinstaller] --onefile --hidden-import win32timezone myservice.py
Then I am executing it from cmd with install arg, service is installed.
However next step, executing it with start arg fails. Error message:
Starting service [Service_Name]
Error starting service: The service did not respond to the start or control request in a timely fashion.
So I tried debugging it with ``debug``` argument. It works normally, when I install service without executable, from source files. When service is installed from executable I am getting following error.
Debugging service [Service_Name] - press Ctrl+C to stop.
Traceback (most recent call last):
File "[Service]\[Service_Folder]\service.py", line 48, in <module>
File "lib\site-packages\win32\lib\win32serviceutil.py", line 640, in HandleCommandLine
File "lib\site-packages\win32\lib\win32serviceutil.py", line 461, in DebugService
AttributeError: module 'servicemanager' has no attribute 'Debugging'
[14252] Failed to execute script service
I found similar topic on stack, unanswered, link here. So the problem indeed lays in installing service from pyinstaller executable.
I have no idea, how to track error on this, Event Viewer does not display anything more than i could see from console.
Is there other way to debug a service?

backend module executed while starting python flask server app

I am new to Python Flask and Python.
I use Python 3.7 and Ubuntu.
I have done export FLASK_APP=emptst
And have been executing an online application. It was running fine
In the same environment I have a pure python program & I executed it from command prompt.
I think while executing this the python flask server was listening. It is a localhost server
After this I have stopped the server and ran the python online again.The server came up But it started executing the python program that I executed from command line.
And when I removed the program from the mflask directory it is giving the following error.
Traceback (most recent call last):
File "/home/ss/anaconda3/envs/test/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/ss/mflask/emptst_new.py", line 2, in <module>
import recognize_faces_video_file1_200119
ModuleNotFoundError: No module named 'recognize_faces_video_file1_200119'
Pls advise. Basically i need it to run the online application emptst and not execute the batch application which it executes currently, whenever i start the server

Pywinauto script compiled as exe using Pyinstaller executes locally but not remotely from the cmd line using PSEXEC

I have a requirement whereby the navigation and control of a legacy application has to be automated - this automated method, packaged as a script, would need to be deployed via SCCM to our user base.
After some research, I've decided to use Python's PywinAuto module to achieve my goal.
I've managed to create a script that automates all the GUI steps as required and I've also packaged this script as an exe using the Pyinstaller module, but the issue i'm now having is the following: when running locally on both Windows 7 and Windows 10 machines, the exe executes successful. However, when triggering the exe remotely through either SCCM or PSEXEC, the exe fails to execute.
I've also wrapped the exe in a powershell script, but the same thing occurs when executing the script via PSEXEC or SCCM.
I'm running PSEXEC as the System user, which has full admin rights. The SCCM user has full admin rights aswell.
This is the Pyinstaller example script that I packaged into an exe, just for testing purpose:
from __future__ import print_function
import logging
from pywinauto import actionlogger
from pywinauto import Application
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--log", help = "enable logging", type=str, required = False)
args = parser.parse_args()
actionlogger.enable()
logger = logging.getLogger('pywinauto')
if args.log:
logger.handlers[0] = logging.FileHandler(args.log)
app = Application(backend='uia').start(r'mspaint.exe')
dlg = app.window(title_re='.* - Paint')
dlg.close()
When running the exe locally from the the command line, the following appears indicating successful execution:
c:\Temp>pywinauto_test 2019-05-11 07:47:14,215 INFO: Started
mspaint.exe application. 2019-05-11 07:47:14,344 INFO: Closed window
"Untitled - Paint"
When running remotely through psexec, the following appears:
C:\Temp>pywinauto_test.exe 2019-05-11 07:29:38,221 INFO: Started
mspaint.exe application. [ 5516] Failed to executC:\Temp>e script
Pywinautopaint Traceback (most recent call last): File
"site-packages\pywinauto\application.py", line 256, in
__resolve_control File "site-packages\pywinauto\timings.py", line 458, in wait_until_passes pywinauto.timings.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "Pywinautopaint.py", line
59, in File "site-packages\pywinauto\application.py", line
378, in getattribute File
"site-packages\pywinauto\application.py", line 259, in
__resolve_control File "site-packages\pywinauto\timings.py", line 436, in wait_until_passes File
"site-packages\pywinauto\application.py", line 201, in __get_ctrl
File "site-packages\pywinauto\findwindows.py", line 87, in
find_element pywinauto.findwindows.ElementNotFoundError: {'title_re':
'.* - Paint', 'backend': 'uia', 'process': 2844}
All known methods to run GUI automation remotely are listed in the Remote Execution Guide. It's not specific to PyInstaller or other py2exe-like tools. Mostly it's OS Windows restrictions and RDP features that can be workarounded by several ways.
Also ElementNotFoundError is often raised when default timeout to find window (5 sec.) is insufficient. For example, if remote virtual machine is slow. This might be unrelated to common remote execution problems.

key error when running local app example with dev_appserver.py

I am trying to run the appengine-endpoints-helloendpoints-python app locally using the google cloud platform SDK (which has been added to my path) using the command dev_appserver.py from the command line. It used to run perfectly, but now when I try, I get an error:
Error processing line 2 of
/Users/hannah/anaconda3/envs/py27/lib/python2.7/site-packages/grpc_google_pubsub_v1-0.8.1-py2.7-nspkg.pth:
Traceback (most recent call last):
File "/Users/hannah/anaconda3/envs/py27/lib/python2.7/site.py", line 161, in addpackage
exec line
File "<string>", line 1, in <module>
KeyError: 'google'
Remainder of file ignored
It's in a anaconda python2.7 environment. I've tried looking in site.py but I can't follow it back to a clear missing link.
The appcfg.py command used to deploy from the command line doens't work at all anymore. The command is not found.
bash: appcfg.py: command not found
As I said, I reinstalled and authorised the google cloud SDK and it is added to my path and that hasn't solved anything.
Oh finally, the error comes up whenever I run the dev_appserver.py command, not just with the example code from google.
Any tips would be appreciated

Categories