I want to write a python script that adds env variables to PATH. This script will be executed from ssh to windows virtual machine. I tried 2 methods and no one works.
Method 1 :
os.system("setx /M PATH "'"%PATH%;path1;path2"'"")
Method 2 :
os.environ["PATH"] += os.pathsep + os.pathsep.join(["path1"])
Could you help me please ?
Thanks in advance.
You’ll need an administrative command prompt.
Try:
setx EC2_HOME "" /M
Perhaps try this as a variation to your second method
os.environ["PATH"] = os.pathsep.join([os.environ["PATH"], "path1"])
Also check out some of the documentation here related to running python on Windows machines https://docs.python.org/3.4/using/windows.html
Specifically it is mentioned that you can set environment variables outside of your python environment by using the following syntax
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
Thanks for your answers.
Finally, I found the problem, I was setting this env variables with running a subprocess that execute another script, when I set this env vars from the script without a subprocess, it works and the two methods works :)
Related
I added a environmental variable manually as setx NEWVAR SOMETHING,
so that my tool later uses the NEWVAR variable in the script but I am unable to access it, please help. Also below is the code. And for your information I am able to access the predefined system variables
try:
kiran=os.environ["NEWVAR"]
print kiran
except KeyError:
print "Please set the environment variable NEWVAR"
sys.exit(1)
You need to make sure your environment variable is persistent following a restart of your shell otherwise your new environment variable will not be accessible later on
ekavala#elx75030xhv:/var/tmp$ export NEWVAR='alan'
ekavala#elx75030xhv:/var/tmp$ python test.py
alan
*closes shell and reopens*
ekavala#elx75030xhv:/var/tmp$ python test.py
Please set the environment variable NEWVAR
Update your $HOME/.bashrc or /etc/environment with the variable instead of just doing a setx or export
Note: If you update /etc/environment you will need to reboot your computer to have the environment variables set in your shell
If you want to set environment variable through script:
import os
os.environ["JAVA_HOME"] = "somepath"
If you set it using command prompt it will be available only for that shell. If you set it in advanced system settings it will available every where but not when you open a new shell inside python script.
Instead of using setx to set those environment variables; try using EXPORT. The following example worked for me.
export MYCUSTOMVAR="testing123"
python testing.py
Note: If you are on Windows you can set the env variable with SET.
SET MYCUSTOMVAR=testing123
testing.py
import os
if __name__ == '__main__':
print("MYCUSTOMVAR: {0}".format(os.environ['MYCUSTOMVAR']))
output
MYCUSTOMVAR: testing123
Is it possible to set a environment variable in Windows 7 that will run a python script when called? Couldn't do it when I tried.
No, there is no way to set an environment variable that calls a function when addressed. That's simply not something that environment variables can do.
trying to install spark, I've some problems when I try to set the system enviroment variables. I modify the PATH using:
“Advanced system settings” → “Environment Variables”
but when I call these variables from python, using the code:
import os
path = os.environ.get('PATH', None)
print(path)
The path that shows python don't have the modifications that I put. Thanks
Any program invoked from the command prompt will be given the environment variables that was at the time the command prompt was invoked.
Therefore, when you modify or add an environment variable you should restart the command prompt (cmd.exe) and then invoke python to see the changes.
I'm using Tesseract OCR and everytime I run a new session it asks for setting the TESSDATA_PREFIX variable,
I do so by running the command export TESSDATA_PREFIX="PATH_TO_FILES"
How can I do it inside the python script i'm running ?
Thanks !
You can do:
import os
os.putenv("TESSDATA_PREFIX", "PATH_TO_FILES")
More info
http://docs.python.org/2/library/os.html#os.putenv
Please try adding to your python:
import os
os.environ["TESSDATA_PREFIX"] = "PATH_TO_FILES"
You could try using os module to set environment variable:
setting:
os.environ['TESSDATA_PREFIX'] = "PATH_TO_FILES"
getting:
pat_to_files = os.environ['TESSDATA_PREFIX']
Such variable will be accessible from the python code but it may not remain accessible for other programs when your python code quits.
If your goal is to set env variable for other program then you could try this recipe:
http://code.activestate.com/recipes/159462-how-to-set-environment-variables/
I have one problem with os.environ. I set some variables in my bat file (for example):
set MYDIR=%CURDIR%
Then I use set command in command line of Windows to check it. Everything is fine, my variable was added. But!
Then I run my Python script and use os.environ['MYDIR'] or os.getenv('MYDIR') but my envorinment variable doesn't show up!
Why does it happen?
My OS - Windows 7 x64, Python 2.5.4
Thanks.
Set works on session level. WinXP, use SETX from support tools http://www.microsoft.com/en-us/download/details.aspx?id=18546 to permanently set env variable.
Or use MyComputer>Properties>Advanced>Environment Variables to set user- and system- level variables.
Never used PyCharm, but breef scan through docs shows that you might be able to set script-level environment variables within PyCharm, look here http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-python.html