Working Directory not work properly after application is packaged
-
I develope one application, and in that application I invoke one external application with QProcess.
My Environment:
Python 3.9.2
PySide6 6.2.2.1Codes just like below:
self.client_process.setProgram('program.exe') self.client_process.setArguments(program_params) self.client_process.setWorkingDirectory(get_program_root_path()) self.client_process.start()
Everything is cool when I test in IDE, but when I package it to executable files, odd things happen:
The program I invoke cannot start because it reports that required dll cannot be found and loaded. The required dll files are all placed in the same directory with the invoked program so if the working directory is correct, it can find them and link them, but current status is with problem, just like setWorkingDirectory not working.
I've checked the workingDirectory with log output and I can assure that the directory is set properly before process start even after packaged, but it acts just like unsetting. Then I try to copy those required dll files in the same directory with the main application, everything is OK... I think it also indicates that the working directory is not set correctly even the value is changed.
I've tried a lot of ways, such as:
set program with full path and relative path
change current directory with QDir.changeCurrent()
change package tools from cx_freeze to pyinstaller
...All don't work except that I put those dlls in main application directory. Can anybody help to check on this? Thanks
-
An new update:
I append the previous working directory in environment parameter "PATH" with method
setProcessEnvironment
And it works.
To my problem it is closed. But I still think setWorkingDirectory may have issues there.
@benjaliu said in Working Directory not work properly after application is packaged:
But I still think setWorkingDirectory may have issues there.
I don't know what that means. So far as I recall
QProcess.setWorkingDirectory()
effectively sets the working directory for a sub-process once it is running. In other words, it's like "start the sub-process, then set its working directory, then execute its code". It does not set the working directory before invoking the sub-process. That means that anything about the sub-process which would require it e.g. to find a DLL in that directory in order to start the sub-process in the first place will not work.Maybe that is what you have found, and why altering
PATH
before launching the sub-process is required for your case? -
@benjaliu said in Working Directory not work properly after application is packaged:
But I still think setWorkingDirectory may have issues there.
I don't know what that means. So far as I recall
QProcess.setWorkingDirectory()
effectively sets the working directory for a sub-process once it is running. In other words, it's like "start the sub-process, then set its working directory, then execute its code". It does not set the working directory before invoking the sub-process. That means that anything about the sub-process which would require it e.g. to find a DLL in that directory in order to start the sub-process in the first place will not work.Maybe that is what you have found, and why altering
PATH
before launching the sub-process is required for your case?I Checked many tutorials about QProcess.setWorkingDirectory(), all are similar to what I wrote before (See my initial post):
- create one QProcess
- set required params for QProcess such as program path, parameters, and working directory.
- start the QProcess
Please correct me if there is something wrong.
In old version API we can send all parameters in one API call but after I upgrade our framework to the newest PySide6, I found it didn't work properly, so I try to set them seperately and hope it is right.
It is interesting that when I run it in IDE, everything is right. The working directory is set successfully. But after I package the application to one exe and run it again, I found that setting working directory seems no effect and I have to copy all required DLLs in the same folder of packaged application so that the invoked process can find them. At first I assume it may be the problem on packaging, so I changed several packaging ways but all failed the same way. Then I go back to consider QProcess again.That's why I said setWorkingDirectory may have issues. It works well in non-packaged status, but have issue after packaged. My current workaround is append additional PATH info for sub-process, then the program in sub-process can find the required dlls, but I still hope the sub-process can be worked in right working directory, not just the one for the main program.