Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I am working on a python code and require to open an executable which is located in my C drive. I dont want to code something like, 

    file = open("C:\\Documents\\CodeRepo...\\codeplay.exe)

I want to use the environment variable called LOCATION_HOME to point to this exe location.

I dont know when do i set this environment variable. Do i set it in my python code or using the terminal in Windows?

Can someone please provide me insight on how to do this.

Thanks in advance.


What I have tried:

I tried to do as follows. I created an environment variable LOCATION_HOME which points to
C:\\Documents\\CodeRepo... and then I did file = open("LOCATION_HOME\\codeplay.exe") in python code. 

It gives error file not found.
Posted
Updated 8-Mar-17 5:04am

You could have easily found the answer yourself just by Googling for "How to resolve an environment variable in python".
 
Share this answer
 
You have to set in the same terminal from where your Python script is started (manually or using another script or batch file) or set it globally (search for "windows set permanent environment variable").

Then use
Python
import os

path = os.environ["LOCATION_HOME"]

See also 16.1. os — Miscellaneous operating system interfaces — Python 3.6.1rc1 documentation[^].
 
Share this answer
 
Comments
Member 13046390 8-Mar-17 11:04am    
I want to do something like this:

I am running my python code from the IDLE. How do I assign that in the subprocess function to open the exe from the location.
I did something like this:

output = subprocess.Popen((LOCATION_HOME\\binaries\\codeplay.exe
), stdout=subprocess.PIPE).stdout


Is it correct?
Jochen Arndt 8-Mar-17 11:16am    
No, because you gave an invalid path.

Use
path = os.environ["LOCATION_HOME"]
path += "\\binaries\\codeplay.exe"
Popen(path)
Member 13046390 8-Mar-17 11:32am    
I am still having some issues.

The thing is I provide some extra things in the Popen like shown below.

output = subprocess.Popen(('path -testroot C:\\....\Configuration -projectfile ProjectFilewithTags -environment'
), stdout=subprocess.PIPE).stdout

not sure on how to integrate that in here? Can you please help? Thanks alot in the meanwhile.
Jochen Arndt 8-Mar-17 11:39am    
That is not related to the initial question.

I suggest to open a new question so that others might also help (and questions can be better formatted than comments). But before doing that I would suggest to think about it and read the documentation.

For the arguments:
Just append them to the variable:
path += " -testroot ..."
Member 13046390 8-Mar-17 11:50am    
i tried that but it still does not work. It says that system cannot find the file. I did a print of the path and it looks correct but there is like ...\\binaries\... two backslashes near the binaries, is that correct?

In the meanwhile, i also posted a new question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900