I am trying to develop an Mixed Integer Linear Programming (MILP) optimization tool with Excel and Python. Pyomo with GLPK solver are being used for MILP optimization. Python program reads inputs from the excel file and write output to an excel file. I used Pyinstaller to create exe file. The program works when exe file is opened as expected.
The Problem -
I want to trigger the exe file using VBA Macros from the excel file. When the exe file is triggered from the VBA Macro using the Shell() command, the program gives following error - "ValueError: Failed to set executable for solver glpk. File with name=glpk-4.65\w64\glpsol.exe either does not exist or it is not executable. To skip this validation, call set_executable with validate=False."
The program works well when exe is directly used and the same program gives the above error when exe file is triggered through VBA Macro. Please help solving this problem.
Edit:
Part of VBA code that calls the exe
With CreateObject("WScript.Shell")
.Run """" + NewFilePath + """", 1, True
End With
NewFilePath is the location of the exe file.
Please note, GLPK solver works when the exe is executed directly. When called from VBA macro, other sections of python program also works except for the GLPK. The only thing that does not work is GLPK when the exe is called from Macro.
Error Message -
WARNING: Failed to create solver with name '_glpk_shell': Failed to set
executable for solver glpk. File with name=glpk-4.65\w64\glpsol.exe either
does not exist or it is not executable. To skip this validation, call
set_executable with validate=False.
Traceback (most recent call last):
File "pyomo\opt\base\solvers.py", line 152, in __call__
File "pyomo\solvers\plugins\solvers\GLPK.py", line 119, in __init__
File "pyomo\opt\solver\shellcmd.py", line 55, in __init__
File "pyomo\opt\solver\shellcmd.py", line 100, in set_executable
ValueError: Failed to set executable for solver glpk. File with name=glpk-4.65\w64\glpsol.exe either does not exist or it is not executable. To skip this validation, call set_executable with validate=False.
Traceback (most recent call last):
File "optimization.py", line 459, in <module>
File "pyomo\opt\base\solvers.py", line 105, in solve
File "pyomo\opt\base\solvers.py", line 122, in _solver_error
RuntimeError: Attempting to use an unavailable solver.
The SolverFactory was unable to create the solver "_glpk_shell"
and returned an UnknownSolver object. This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").
The original solver was created with the following parameters:
executable: glpk-4.65\w64\glpsol.exe
type: _glpk_shell
_args: ()
options: {}
[15784] Failed to execute script 'optimization' due to unhandled exception!
I figured out the problem. VBA triggers exe file from the cmd default folder. Python/Pyomo looks for the solver in the same folder. From VBA, by going to the folder in which exe and GLPK solver are from cmd and triggering the exe make the program work. Following is the change
With CreateObject("WScript.Shell")
Cmd1 = "cd " & DistFolder
Cmd2 = """" + NewFilePath + """"
Connector = " & "
Commands = "cmd.exe /K " & Cmd1 & Connector & Cmd2
.Run Commands, 1, True
End With
#Tim Williams might be right with his comment.
Related
I'm trying to write a chatting app that when run the first time deletes itself from the current position. Now the code is:
import os
import sys
file=sys.argv[0]
os.remove(file)
if i run it as .py file, it works just fine. Anyway if compile it with pyinstaller, when run by terminal it raises:
Traceback (most recent call last):
File "tests.py", line 6, in <module>
PermissionError: [WinError 5] Denied access: 'tests.exe'
[14320] Failed to execute script 'tests' due to unhandled exception!
nothing changes if i run it as administrator or assigning the file permissions with os.chmod. I have python 3.10, i tried both with python 3.9 and 3.10 and it does not work. I even tried running the command del with the subprocess module with the same result.
This works as .py file because the actual executable is the compiled version of the source code - a separate file/memory space. While the compilation is running the source code .py file is closed and can be removed.
When the program is compiled and run as a .exe file it will not remove itself because it is still open and running and therefore has a permission access denied error.
It is trying to remove itself because file = sys.argv[0]
(If you want to remove your running exe maybe start another program that does the removing from the exe and exit exe so that it is closed before removal - kind of awkward.)
When using the qtmoden library with python.
It works fine when running the code in VS Code.
But after using pyinstaller, it doens't anymore. When opening teh generated .exe file, it states that it does not have access to files located in - C:\Users\MyUsername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\qtmodern\resources
When running the .exe aplication it errors stating:
Traceback (most recent call last):
File "my_filename.py", line 262, in (this number differs depending on my app/code)
File "qtmodern\styles.py", line 70, in dark
File "qtmodern\styles.py", line 23, in _apply_base_theme
FileNotFoundError: [Errno 2] No such file or directory: " 'C:\sers\MyName\AppData\Local\Temp_MEI166802\qtmodern\resources/styles.qss'
[36020] failed to execute script my_filename
Anybody any idea how to get this fixed?
How to make sure pyinstaller also takes these 2 stypes.py files into account?
I tried adding data with pyinstaller via the --add--data command and including paths with --paths command, But the error message stays the exact same.
Good day! I've just faced a very tricky issue which I can hardly explain and resolve.
I have a dll compiled in VC 2017. This dll is basically a python module: SWIG wrapper was used to create the interfaces. So at a given folder I have the following files:
python.exe
python27.dll
TestLib.dll
TestLib.pyd
test_lib.py
load.py
File test_lib.py is the interface generated by SWIG.
My "load.py" script:
from test_lib import *
Command I execute in shell:
python.exe load.py
Tests
If I run this command in cmd.exe as a regular user, I get the following error:
Traceback (most recent call last):
File "load.py", line 1, in <module>
from test_lib import *
File "test_lib.py", line 28, in <module>
TestLib = swig_import_helper()
File "test_lib.py", line 24, in swig_import_helper
_mod = imp.load_module('TestLib', fp, pathname, description)
ImportError: DLL load failed: The specified module could not be found.
It seems that either Windows or Python is unable to load the module. I checked with Dependency Walker the dll dependencies and everything seems to be OK.
Now the strange things start.
If I execute the same command in cmd.exe with Administrative privilege, it works!
If I execute the same command in Powershell as a regular user, it works once again!
Analysis
I took Process Monitor tool and analyzed each of these three calls.
cmd.exe + regular user
cmd.exe + admin or powershell.exe + regular user
Yes, cmd+admin and powershell+regular exhibit the same behavior. You may see that in all of the three calls after command QueryAllInformationFile gets BUFFER OVERFLOW status, Windows starts searching the library in different locations (why???).
In the last two calls after 2 alternative paths have been checked (C:\MyCompany\Project\Project\Project\Project\Bin\Win32\TestLib.pyd and C:\Project\Bin\Win32\TestLib.pyd), Windows goes back to the initial location C:\MyCompany\Project\Project\Project\bin\Win32\TestLib.pyd and finally starts loading the DLL.
However, cmd.exe with regular user privileges behaves in a different way. Windows tries a bunch of directories and it seems at the end terminates the process since consideres that the library has not been found. (why???)
Finally if I do a small trick and besides its original location additionally copy my library to C:\MyCompany\Project\Project\Project\Project\Bin\Win32\TestLib.pyd cmd.exe with regular user privileges runs with no error! This is ridiculous.
Questions
What the hell is happening with this call (cmd.exe + regular user)?
How can I fix it?
Why does Windows try to search my library in so many locations, though it detects it in the initially specified location from the very first step?
I have a script I want to run within blender to generate AO maps (script was given to me and the source guarantees it works).
I try to run the script as follows:
blender --background --python /opt/ff/product_builder/furniture_builder/generate_ao_maps.py --input_dir /tmp/test.obj --output_dir /tmp/test.png --mode ao
Which produces:
AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
found bundled python: /usr/share/blender/2.79/python
Traceback (most recent call last):
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 195, in <module>
main()
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 178, in main
args = parse_args()
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 21, in parse_args
return parser.parse_args(os.getenv(BLENDER_ENV).split(' '))
AttributeError: 'NoneType' object has no attribute 'split'
Error: File format is not supported in file '/tmp/test.obj'
Blender quit
If I run this same script without blender (but with the argument) it tells me:
Traceback (most recent call last):
File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 5, in <module>
import bpy
ImportError: No module named bpy
What do I need to do to pass the parameters to the script and get it working?
You are seeing that error because your script is looking for the environment variable BLENDER_ENV, which is not on your system. I don't recognize BLENDER_ENV as a standard Blender related environment variable so it's probable that your friend added BLENDER_ENV to his or her environment.
Firstly, blender processes its cli args in the order they are given, so your example will start in the background, run a script, then set the input_dir... This will most likely not have the result you are after.
The problem with your script failing is that the arg passed to os.getenv() needs to be a string that is the name of a shell environment variable, if you are using bash you need to export the variable to put it into the environment before you start blender.
export BLENDER_ARGS="arg1 arg2"
blender -b myfile.blend --python myscript.py
If you are using a csh then use setenv BLENDER_ARGS "arg1 arg2"
Then in your py script, you use os.getenv('BLENDER_ARGS').split(' ')
Note that each shell instance is a separate environment, you need to set the variables in the same instance that starts blender.
You may also be interested in passing cli arguments to the script as explained in response to this question.
I have a newbie question about creating osquery extensions using osquery-python. I Created a small extension that gets some additional RPM info from my linux system. Following the instructions in the docs, I added the path to the extension in /etc/osquery/extensions.load to get it to autoload. I restarted osqueryd and I see the extension running using ps ax.
If I interactively run osqueryi, I can see the table and get data. It all works perfectly.
However, when I run an osqueryi command 'one-liner' such as :
osqueryi .tables
I get a bunch of the following errors with my output:
#INFO:thrift.transport.TSocket:Could not connect to /root/.osquery/shell.em
Traceback (most recent call last):
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 104, in open
handle.connect(sockaddr)
File "/usr/lib64/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 2] No such file or directory
ERROR:thrift.transport.TSocket:Could not connect to any of ['/root/.osquery/shell.em']
What have I done wrong?
Extensions are run in a separate process. You can see the socket errors, which indicate the extension process cannot communicate with osquery process. Make sure osqueryd or osqueryi is running. Link: osquery doc page for extensions.