Python, Windows service Import error - python

I am trying to run a python application as a Windows Service. The code I have installs and starts but I am having issues importing modules and classes which are part of the application.
Note:: Python libraries are being included fine.
I have checked the python path and all the correct values are in there, (including the application directory) which is leading me to believe that the windows service could be running in a different location.
Does a python application running as a windows service get run from a different location on windows?

python service creation using win32service created the pythonservice.exe in C:\Python27\Lib\site-packages\win32 by default.
you can perform os.chdir(yourdir) in your code just before service creation,
the best thing would be use absolute paths and setting proper sys.path within your script for accessing files

Related

python code and it's pyInstaller generated executable give different output [duplicate]

I am developing desktop database application. Using rdlc report and reportviewer. Everything was fine in developing process, reportviewer was showing all data smoothly. I deploy app with Inno Setup. But when I install the app, the reportviewer is not showing data. While data is correctly inserted in the tables.
For applications that work incorrectly or fail completely, when installed by Inno Setup to Program Files folder, the first thing to test, is to try to deploy the application manually to the same folder.
If the application fails even after a manual deployment, the most usual problem is that the application requires a user to have write permissions to application folder. As on modern versions of Windows a user typically does not have write permissions to the Program Files folder, the application does not work. So the problem usually has nothing to do with Inno Setup, but it's a problem of the application itself.
To solve the problem:
The best solution is to redesign the application so that it does not require write permissions to its folder. Windows applications should not require write permissions to their folder. That's against Windows guidelines. The application should write data to a user profile folder (C:\Users\username\AppData) or to a common data folder (C:\ProgramData).
A dirty workaround is have the installer grant a user(s) write permissions to the installation folder. Do that only, if you cannot get the application fixed (e.g. it's 3rd party application).
See Inno Setup - How to set permissions of installation folder.
Even more gross workaround is to configure the application to be executed with elevated (Administrator) privileges.
See Inno Setup desktop shortcut (link) which has "Run as administrator" advanced property set
or How to set 'Run as administrator' on a file using Inno Setup.
Another solution is enabling legacy compatibility mode that makes Windows redirect all application write attempts to a virtual store. See also Application installed with Inno Setup writes files to unknown location instead of its installation folder.
There are numerous other possible reasons, why the application might be failing when installed, including:
You omitted some dependency:
DLL library
.NET assembly
.NET Framework
Java Runtime Environment
other runtime
COM/ActiveX object, etc.
The application requires some configuration:
a file
a registry key [including COM/ActiveX object registration]
an environment variable, etc.
The application is not designed to be executed from a folder that has a space in its name (Program Files).
The application gets confused by Windows File virtualization (though it's unlikely). See Application installed with Inno Setup writes files to unknown location instead of its installation folder.

Python program crashes when run through a Windows service

My client has provided me with a Python console application which performs some work and writes the result into a .txt file. My task is to write a Windows service which reads that particular .txt file and performs further actions.
I used C# on .NET to write the service. My solution contains 3 projects:
The logic layer project.
The Windows service layer project.
A test app layer project, used for debugging and other purposes.
Both the Windows service layer and test app layer are using the logic layer for core functionality. When I run the application through the test layer, everything works perfectly, but whenever I try to run the application through the service, the Python standalone application that the service launches doesn't write any output files. I could see that the Python app runs in the task manager, but there's no output anywhere. I believe the Python code is crashing but I couldn't get the exact reason.
I've tried the following ways to debug the issue:
Searched the Windows and System32 directories for any related output files, just to consider the possibility of the service having these directories as the default working directory.
Used absolute paths in the service code to make sure that the Python part is not writing output files to some unknown location.
Had the client implement passing the output directory to the Python code through command line arguments.
Wrote a mock console app in C# which writes a file, tried to call it through the service, but it worked fine and wrote the file as expected.
Suspected the standard IO could be causing the Python application to crash and thus used the standard IO in my mock program, but it worked without any issues.
Tried giving a long task to the Python code, which should've taken about 30 minutes to execute completely, but the Python script ran and closed immediately, which essentially is reliable proof of the theory that it crashes at some point.
Tried running the service with my unelevated Windows user instead of the Local System pseudouser.
Tried configuring the service to be able to interact with the desktop.
I am all out of ideas here. Any direction I should also search in?
Just a note, if that matters: I am using System.Diagnostics.Process to launch the Python script.
If it works from your test app, it sounds like a permissions issue. What security context / user is the windows service running as, and does that user have permission to write to the filesystem where you are expecting it? Have you tried using a full path to the output file to be sure where it is expected?
I'd be inclined to write a tiny python app that just saves "hello world" to a file, and get that to work from a windows service, then build up from there.
Thanks to the help from timhowarduk, I finally was able to get to the root cause of the problem. The python script was looking for a configuration file, and when it was running from the Windows Service, it was looking for that config file in System32.
All the windows services are run from System32.
The above caused the python script to search in System32 since it was running as part of the windows service. I guess I might just ask the client to edit the python script to read config from the windows service application directory.

How do I include dependencies for embedded console apps when using Run From Package

I'm deploying my Azure Function app using a CI/CD pipeline in Azure DevOps. The function invokes three console applications that are included in the package. One of the console applications is a stand alone .exe, it works without issue. The other two have dependencies to a number of dll:s that are also included in the package. This setup works well on my local machine, and when deployed using WebDeploy.
When instead deploying using Run From Package to a freshly created Function App Service, the function app itself loads fine as well as the standalone .exe console app, but both console apps that have dll dependencies fail to run, and both return with exit code 0xC0000135 to my function app (indicating that a dll failed to load).
Now, if I deploy once using Webdeploy and then deploy again using Run From Package, I get the latest build installed - and the console apps now work (!). I think this might be due to the .exe not being able to access the virtual file system when loading the dll:s, is this correct?
I could stick with WebDeploy but I really want to use the package deploy since the cold start time is much faster during scale-out (will need 100+ instances in production). I am also concerned that this way, the app actually needs to copy both the zip package, and the site structure under wwwroot, causing additional overhead.
What is the best way to include dependencies such as dll:s in a package when using Run From Package with Azure Functions?
(The function app is v3, built using .NET Core 3.1)

Python module not found when running local development server of webapp2(Python) in Google Cloud SDK

I am running a webapp2 sever in Google Cloud SDK and making use of the Naked module to run node.js scripts from python2 in a Vagrant box.
While starting the server which has this code snippet
from Naked.toolshed.shell import muterun_js
def get_signed_hash(username):
response_from_js = muterun_js('./views/api/generateSignedTransaction.js',
nonce)
signed_hash = response_from_js.stdout
print('0x' + signed_hash)
I am getting this error message
ImportError: No module named Naked.toolshed.shell
I am making use of the requests library as well and that is not causing any problems. Tried to debug the problem as to maybe the location of the packages, they are both in the same place:
>>> import requests
>>> print(requests.__file__)
/home/vagrant/.local/lib/python2.7/site-packages/requests/__init__.pyc
>>> import Naked
>>> print(Naked.__file__)
/home/vagrant/.local/lib/python2.7/site-packages/Naked/__init__.pyc
Infact I ran the script in the same location as a standalone file calling get_signed_hash('username') and it runs successfully. Is there any other place I need to check?
For the first generation standard environment apps dependencies need to be installed inside the app (or vendored-in). Having them in the system site-packages (where the regular, standalone apps are loading libraries from) is not enough. Even if you somehow convince the local development server to run the app, it won't work when uploaded to GAE as GAE doesn't have access to your local system.
Typically you'd install your library (and all its dependencies) inside a lib directory inside your app dir, so they could be uploaded to GAE together with your app. See Copying a third-party library.
Alternatively you could try to just symlink the Naked directory into your lib dir, which could work if its all other dependencies are already satisfied in a similar manner. See related How do I access a vendored library from a module in Python Google App Engine?

Google App Engine Launcher fails to start

I'm having a problem with the Google App Engine Launcher (the GUI thingy).
I have it installed on two PCs. Both PCs have Python. On both, the launcher ran (the first time) successfully after the installation. On both, at first it failed to deploy an app I was feeding to it, apparently due to being unable to detect Python.
On my home PC, I opened preferences and manually set the path to Python. After that, the app was deployed, and the launcher has been working fine since then, never failing to start. On my work PC, I missed the opportunity to set the Python path, and the launcher fails to start. The log tells me: ConfigParser.NoOptionError: No option 'name' in section: '0'. I suspect this could be due to the missing Python variable.
Unfortunately there doesn't appear to be any kind of config file that contains this setting for GAE. Searching the Windows registry has turned up nothing useful either. I've tried searching the site for problems with the launcher, or how to set the Python path without using GUI, but only found information on problems with apps, not the launcher itself.
A quick google suggests:
try to find and delete file google_appengine_projects.ini
here
or
in Google folder , you have google_appengine_projects.ini file.
Delete it's [0] section.
Or just uninstall and reinstall the launcher.
If still nothing, download an IDE instead - I use pycharm. It's not free (there are free ones out there) but it does manage the launcher for you.

Categories

Resources