Python Windows Service - Current Path, Custom Modules, and wrong resolutions - python

I have a working windows service which is referencing a custom schema created in SQLAlchemy. Before moving everything into a seperate module, everything was working just fine in python form, exe form, and msi form.
I have never imported custom modules before. Right now, I am staticly loading the path of the module into the application. And it seems to work just fine.
Now I moved the code out, set up an import did some work, and my config file is failing to load! I can't figure it out!
The static load --
`sys.path.insert(0,r"c:\dev\nbc_dps_tools\dashboardtotal\schema")
from NydpsSchema import Computer, Drive, Workgroup
`
Seems to get me all the objects I need. Figuring out how to dynamically load the path will be done after I figure out the following pathing problem.
service_dir = os.path.dirname(os.path.realpath("__file__"))
yaml_file = os.path.join(service_dir, 'configs.yaml')
Printing the yaml_file path from above gives me the following path in console, when starting the service
However, after the service starts (successfully) it quickly dies out. Providing the following error in event viewer.
Python could not import the service's module
Traceback (most recent call last):
File "c:\dev\nbc_dps_tools\dashboardtotal\Logon_Service_Live\Logon_Service.py", line 82, in <module>
stream = open(yaml_file, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'c:\\Python34\\lib\\site-packages\\win32\\configs.yaml'
%2: %3
As we see, while installing/starting the service, it resolves properly, gets the config file, and starts. However, once it starts, it appears as if the yaml_file path is resolving to win32 site-package. I tried lots of different pathing styles, nothing seems to work.
The idea is that the path should resolve properly whether run from .py, .exe (or msi)
Thanks for reading, if any additional information is required to help, I can always add more.
EDIT : It appears it is resolving to pythonservice.exe, and not to the script

Related

ModuleNotFound Error importing other python files

I've written a custom skill for azure using regex in python, the goal is now to import the function from the file its written in, to the skill build file so it can be referenced in the function.
I keep getting the ModuleNotFound error, although when I start typing it in, VS code is recognising it and offering it as an autocomplete. I think i'm just messing up somehow in my file structuring; although i've tried a few different options and none of them seem to work.
Currently the structure is:
Categoriser Function - the overall folder
. pycache.py
.__init__.py
.azurefunction - the file i'm trying to import into
.assorted other files for the project
.Categoriser - the subfolder that the module is in
.__init__.py
.categoriser.py - the file I want to import to the first file
I've tried shifting the Categoriser folder around so it's not a subfolder of CountInstance, i've tried taking Categoriser out of the folder and popping it just into the main directory, and i've tried doing the file path route, for example:
import Categoriser.Categoriser
Briefly I thought i'd got it working by trying to import to a different .py file in the folder, but that only seems to be if I use the button de-bug and run option in VS code, if I use the keyboard shortcut it gives me the ModuleNotFound error again.
I'm guessing there's something really obvious i'm missing? I just can't seem to find it in the official documents or in a whole load of googling!

Cannot get Perforce Triggers to work properly

Been trying to get this working all day, and i just can't figure out why its not working.
Trying to implement a simple trigger to run when the user submitted a file.
example in the .tmp file:
hello_trigger change-submit //testDepot/... "python F:/triggers/hello_trigger.py"
when i try to submit a file i get this:
Submit validation failed -- fix problems then use 'p4 submit -c 10199'.
'hello_trigger' validation failed: python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
File exists and can be read, so its not a python issue.. same error with a .txt or .bat file.
From what i can gather the problem seems to be coming from the depot line in the trigger.
//testDepot/... fails
//depot/... doesnt fail, but the script is never fired off.
Any suggestions are greatly appreciated.
also testDepot is a stream not sure if that matters.
python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
seems pretty clear that the file doesn't exist, at least from the point of view of this trigger command. Some things to double check:
This is running on the server machine, i.e. the place where the p4d service is running. If you have the script on your client machine the Python executable on the server isn't going to be able to find it!
Similarly, this is being run by whatever user is running p4d (on Windows this is usually the SYSTEM user, which may have limited permissions). Does that user have permission to read this path?
Could it be that your version of Python on Windows doesn't know how to handle paths with Unix-style forward slashes? (Many tools will normalize these for you but you shouldn't depend on it!) Try using a valid Windows path, i.e. F:\triggers\hello_trigger.py.

Run an .exe file from python code with path parameters

This throws file not found error, I guess because of the path argument. The path argument is needed for the clemb.exe, which is a batch runner for the stream. Even when I try to run it without the path arguments I get access dennied. I am currently running this from pycharm but I want to compile to a binary and ship to customer.
Any suggestions how to bypass the file not found error and the access denied issue?
import sys
import subprocess
subprocess.call([r'"C:\Program Files\IBM\SPSS\Modeler\18.2\bin\clemb.exe -stream path\to\spss\modeller\stream\stream.str"'])
I am using Python 3.7
I guess
subprocess.call([r'C:\Program Files\IBM\SPSS\Modeler\18.2\bin\clemb.exe', '-stream', r'path\to\spss\modeller\stream\stream.str'])
should be more clear and working.

How to use websockify with python on windows or compiling websockify as windows executable (for more multiple client connection at a time)

I'm trying to get the websockify 0.6.0 running on windows but without any luck,
I have tried python websockify.py 1501 10.0.0.141:1501 but its not good, getting errors , like that:
Traceback (most recent call last):
File "websockify.py", line 1, in <module>
run
NameError: name 'run' is not defined
I also tried Compiling Websockify as Windows Executable , but this also didn't work
I use the following command run.exe 1501 10.0.0.141:1501 and it looks promising at the beginning, outputting the following to the console:
WARNING: no 'resource' module, daemonizing is disabled
WebSocket server settings:
- Listen on :1501
- Flash security policy server
- No SSL/TLS support (no cert file)
- proxying from :1501 to 10.0.0.141:1501
but then after trying to connect using the following from the browser ws://localhost:1501
**it outputs the following error
Traceback (most recent call last):
File "run", line 5, in <module>
File "websockify\websocketproxy.pyc", line 419, in websockify_init
File "websockify\websocket.pyc", line 1018, in start_server
UnboundLocalError: local variable 'exc' referenced before assignment
Any idea on how to use the websockify on windows / or how to use the compiled websockify as windows executable ?
The easiest way to get websockify working on Windows is to use the Node.js version of websockify (in the other/js directory). It works perfectly out of the box, with no shenanigans required.
To address this, use the modified following commands for your example source, start from the beginning of each step and see if it helps:
Firstly, install Portable Python 2.7
You then need to modify the setup.py (It looks like this is why you are getting your first error, as you may not have defined run):
from setuptools import setup, find_packages
# add (line 2):
import py2exe
setup(name=name,
# add (line 10):
console=['run'],
Ensure the above has executed correctly by inspecting setup.py and ensure it includes run.
In your local code, import the resources module to allow you to monitor, measure and control system resources utilized by your program
import resource
Inspect your local variable exc and ensure you have assigned a value to it before calling it (I'm guessing you may have attributed a system variable to it, but python was unable to do so as you did not have resources imported, and as such it was not assigned). If you like, put up an example of your code in the comment to this response and I'll take a closer look at this part.
Back to your source guide, navigate to the websockify folder in command prompt, then execute the following to compile websockify:
[Your path to Portable Python install]\App\python.exe setup.py py2exe --includes numpy
You will now see in the websockify directory a new dir 'dist' which contains the compiled exe. An example provided is:
run.exe 5901 localhost:5900
There is also a guide here to run websockify as a Windows Service if this suits (again mentioned in your source).
----Further edit for more detail----
Open up the two files that seem to be giving you issues (websockify\websocketproxy.pyc and websockify\websocket.pyc and ensure that any reference to a variable called "exc" is referenced after it has been assigned a value (small chance of an issue if you have not yet modified these files.
I believe that your code is relying upon making and monitoring changes to the system resources (such as ports etc) and you are not allowing your code to have these permissions, so it needs the resources module. If you are calling run.exe from a program (what I called your local code) then you need to import resources at the top. If you are just straight up calling the run.exe program from a command line, then try making this new program and see if this helps. If not, send me the contents of your websockify folder and run.exe and I will take a look
# Program Name: goAndRun.py
# Code:
import sys, string, os, arcgisscripting, resource
os.chdir( 'C:\\[Path_to_the_dir_containing_run.exe]' )
os.system( '"C:\\[Path_to_the_dir_containing_run.exe]\run.exe, 5901 localhost:5900"' )
And then use the command:
python goAndRun.py
Not being in your environment, I cannot tell if this will execute exactly as I have written it. The last line may also be:
os.system( '"C:\\[Path_to_the_dir_containing_run.exe]\run.exe"', '5901 localhost:5900' )

Python - exec(open('file.py').read()) reports error - No such file or directory. 'file.py'

I'm just starting Python (3.3) on Windows 7, and the book I've been using (Learning Python, by Mark Lutz) says I can also run Python modules by using the exec(...) function. This is the code the author presents:
>>> exec(open('script2.py').read())
The error this code shows is:
FileNotFoundError: [Errno 2] No such file or directory: 'script2.py'
To emphasise, I do have the PYTHONPATH variable set, and therefore the import function works properly: it doesn't show any error messages after importing a module.
I have provided the screenshot:
http://i.stack.imgur.com/ZzDLE.png
To prove that the file imports normally, I will take a screenshot when importing that file: http://i.stack.imgur.com/gLiH4.png
Even though I don't believe there is something wrong with the Pathway, but with the function itself... Help!
open('xyz') does not search the various python import paths. If you give a relative path name, it starts with the current working directory, appends your path and looks there. If you give an absolute path, it ignores the current directory.

Categories

Resources