0

I have an exe located at C:\Users\srinivast6>C:\Users\srinivast6\Documents\Cipia\Cipia\DriverSenseCLI-v7.4.3-win64. This exe I can run in terminal (non admin mode ,windows OS) without any problem.

I am calling it programmatically using subprocess.Popenas below.

process = subprocess.Popen(['myApplication.exe'])

But it is giving following error. It is not able to read license file. What might be the cause for this?. Do I need to open this in admin mode?

Cannot open license file : license.dat
License 284

Error initializing library:
license is not valid

EDIT1: After changing the directory from where I was running python script to the directory where exe is located,I no more see any error. It is launching as expected. But I am packing this python script as an standalone executable. So the user might use this executable from any directory to launch myApplication.exe. I cannot actually put restriction on user.

So is it possible to set the current working directory programmatically to the path where myApplication.exe is located??

srinivast6
  • 309
  • 3
  • 8
  • @CharlesDuffy, thank you for the input. That helped. After changing the directory, it worked – srinivast6 Jun 01 '21 at 16:19
  • See the link in the "This question already has an answer here" section at the top of your question. – Charles Duffy Jun 01 '21 at 16:21
  • yeah that solves my problem. Am I expected to delete this question now? – srinivast6 Jun 01 '21 at 16:22
  • Not obligated -- it'll be deleted automatically eventually unless it gets enough views or upvotes to make it clear to the system that it ought to stay open (to act as a guidepost to help people find the canonical instance of the question). For that matter, having an upvoted answer may _prevent_ you from being able to self-delete; I don't remember where the reputation level is where that rule gets relaxed.. – Charles Duffy Jun 01 '21 at 16:24

1 Answers1

1

Some more information needs to be shared, but a guess for what could be happening is that you are executing the python script from a directory other than where you normally execute myApplication.exe, and that the call to open the file in myApplication is using a relative path (which seems to be the case here). That would mean when you execute myApplication with Popen, it will have the working directory set to the working directory of wherever you executed the python script and the relative path would be relative to that. If that is the case, try executing the script from the same directory as where you would usually execute myApplication.exe or change the path passed to file the file open call in myApplication to use an absolute path.

Example:

Directory Structure

C:\Users\user
|--popen.py
|--somedir
   |--license.dat
   |--myApplication.exe
   |--myApplication.py

Contents of popen.py:

process = subprocess.Popen(['myApplication.exe'])

Contents of myApplication.py (I realize python files don't get compiled to executables, but it is only for the sake of an example):

f = open('license.dat', 'r')

Now, this wouldn't work:

cwd: C:\Users\user
$ python popen.py # File not found error.

Either execute the script from somedir:

cwd: C:\Users\user\somedir
$ python ..\popen.py

Or alternatively, change the path passed to open in myApplication.py:

f = open('C:\Users\user\somedir\license.dat', 'r')
topoly
  • 301
  • 2
  • 5
  • the path is in correct. In fact , if the path is not proper it will throw filenotfiunderror which is not the scene in my case. – srinivast6 Jun 01 '21 at 15:47
  • @srinivast6 The question is not if the path to the executable is correct. The question is if the _current working directory_ is correct, because it's relative to that directory (not relative to the location of the executable) where the program that's being started will be looking for its files. – Charles Duffy Jun 01 '21 at 15:50
  • @topoly, ...do note the guidance on questions that "have already been asked and answered many times before" in [How to Answer](https://stackoverflow.com/help/how-to-answer) -- which is why this question has been closed as duplicative. This lets us concentrate answers in one place and focus on making them as high-quality as possible; if this answer adds information not present in any of the existing answers to the linked duplicate at https://stackoverflow.com/questions/1685157/how-can-i-specify-working-directory-for-popen, consider moving it there. – Charles Duffy Jun 01 '21 at 16:18
  • @CharlesDuffy, oops, I just noticed that the answer was marked duplicate. – topoly Jun 01 '21 at 16:22
  • To be fair, it wasn't until the OP followed up to make it clear that it really was a problem caused by their current working directory being wrong. – Charles Duffy Jun 01 '21 at 16:22