Run an .exe file from python code with path parameters - python

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.

Related

Running a program using os.system in Python

I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.

python subprocess.run with working directory changed by cwd is not working

I am trying to use python subprocess to call an exe. The application usually takes the parameter file from the same directory as exe. However, as the python file is not located at the same directory as exe, the exe cannot find the parameter file when called by subprocess.run. Hence I specified the cwd when calling subprocess.run like below:
subprocess.run([cwd_exe, "--cal-cn-bv", cwd_cif, "Cs1"], cwd=r'd:\Study\softBV_mix\GitHub\projects\Coord\bin', capture_output=True)
However the subprocess still cannot find the dat file in
d:\Study\softBV_mix\GitHub\projects\Coord\bin
The error message appears as
CompletedProcess(args=['d:\Study\softBV_mix\GitHub\projects\Coord\bin/softBV0405.exe',
'--cal-cn-bv',
'd:\Study\softBV_mix\GitHub\projects\Coord\test/CsCl.cif',
'Cs1'], returncode=0, stdout=b'Warning: unable to find
d:\Study\softBV_mix\GitHub\projects\Coord\database_unitary.dat
where the database_unitary.dat is supposed to be in .../coord/bin/. The application works well if I call it from powershell or command prompt.
No one has answered my question but I kind of found the workaround myself though I am sure if I identify the root cause correctly.
In the end I imported os and make sure cwd is the recognised absolute addresss
import os
cwd = os.path.abspath("../bin")
This worked out.
So the expression
r'd:\Study\softBV_mix\GitHub\projects\Coord\bin'
make cause the issue. Hope some PRO can further clarify this.

Forced to enter full pathname every time instead of just file name

Every time I try to do an action such as running a program in the VScode Terminal (also happens in PyCharm) I have to enter in the full pathname instead of just the file name. For example, instead of just doing python3 test.py to run a program, I have to enter in python3 /Users/syedrishad/Desktop/Code/test.py.
Now, this is annoying and all but it doesn't bother me too much. What does bother me is when my program is trying to pull/ open files from somewhere else. If I wanted an image call Apple.jpeg, instead of just typing in Apple.jpeg, I'd have to go and find the full pathname for it. If I were to upload a piece of code doing this to someplace like GitHub, the person who'd want to test this code out for themselves will have to go in and replace each pathname with just the file name or it won't work on their computer. This problem has been going on for a while, and I sadly haven't found a solution to this. I would appreciate any help I get. I'm also on a Mac if that makes a difference.
You could use os and sys that gives you the full path to the folder of the python file.
Sys gives you the path and os gives you the possibility to merge it with the file name.
import sys, os
print(sys.path[0]) # that is the path to the directory of the python file
print(sys.path[0]+'/name.txt') #full path to the file
print(os.path.join(sys.path[0],'name.txt')) # os.path.join takes two parameters and merges them as one path using / but the line above is also fine
In VS Code, its internal terminal is in the currently opened project folder by default. Therefore, when you use the command "python file_name.py" to run the file, the terminal cannot find the file that exists in the inner folder.
Therefore, in addition to using the file path, we can also add related settings to help it find the file.
Run: When using the run button to execute the file, we can add the following settings in "settings.json", it will automatically enter the parent folder of the executed file.
"python.terminal.executeInFileDir": true,
"When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder."
debug: For debugging code, we need to add the following settings in "launch.json":
"cwd": "${fileDirname}",

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

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

PyQt4 - "See the log file for details" error

I have made an application using Python and recently i found that i can use py2exe to make executables for windows.
The problem is that a library i am using (xmpppy) produces this error
DeprecationWarning: the md5 module is deprecated; use hashlib instead
and when i try to run the executable a dialog appears saying this
See the logfile 'C:\Python26\P2E\MyApp\dist\MyApp.exe.log' for details
any ideas how to fix that?
You can try including the following lines down import sys
sys.stdout = open(“my_stdout.log”, “w”)
sys.stderr = open(“my_stderr.log”, “w”)
For more information you can read this
when a python script .py has any error it shows in console but when you run python .exe file it genrates .exe.log file when error accured.so go to your folder and see that there is a .exe.log file is there showing errors.

Categories

Resources