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.
Related
I'm using MacOS and working on a python project.
In my project there is a line:
num_workers = int(os.environ.get('NUM_SAS_WORKERS', 1))
I want to add NUM_SAS_WORKERS in my environment variables and set it to 10 so that my python project can load the value 10 into num_workers
I've tried executing this in my terminal:
export NUM_SAS_WORKERS=10
and I was able to verify that NUM_SAS_WORKERS = 10 exists by running printenv in terminal
But it doesn't work. In my python script num_workers is still loaded with 1
How to do it correctly?
If you want to access the NUM_SAS_WORKERS environment variable in the Python shell, run this command from your terminal:
NUM_SAS_WORKERS=10 python
And once in the Python shell:
>>> import os
>>> int(os.environ.get('NUM_SAS_WORKERS', 1))
10
If you want to access it in a file, very similar:
NUM_SAS_WORKERS=10 python yourfile.py
If you set up environment variables from the terminal command it gets erased as soon as you close that terminal. To set up and keep them you have to update them in .bash_profile file which resides in the home directory as a hidden file. Follow the below commands to update it.
Open terminal
cd ~
(To take you to the home directory of the mac)
open -a "Visual Studio Code" .bash_profile
( To open .bash_profile file, in the place of "Visual Studio Code" you can use any text editor name)
Now, .bash_profile will be opened and there you can set your environment variables and save it.
Example:
NUM_SAS_WORKERS=10
Now run echo $[NUM_SAS_WORKERS] in terminal to read that
After performing the above steps some times still environment variables not available to read in your python project/file. In such cases, you have to restart or log off and log in to your machine that can fix the issue.
To check whether the environment variable is available to your python code run the below script in your python console. This should print all your environment variables.
import os
print(os.eviron)
I've created Windows system variable using below code:
import os
os.environ['BJT'] = 'HELLO'
But I can't see it in Advanced settings \ system variables Also I can't see it when I try to print it:
import os
print(os.environ['BJT'])
I thought that when I create system variable using os.environ it is created exacly like when I do it in system settings. Is it possible to create system variable from python code and access it even when I restart computer?
You need to call the system (with admin privileges) to create a system variable, you can use subprocess:
import subprocess
subprocess.run(['setx', 'BJT', '"HELLO"', '/M'])
Prior to python3.5 you will need to use process.call instead
There is a misunderstanding with what environment is. It is just a mapping of (string) variables that a process can pass to its children. Specifically a process can change its own environment (which will be used by its future children) but this will not change its parent's environment, nor even the environment of its already existing children if any.
In addition, Windows provides system and user environment variables which are used as the initial environment of any process. This are not changet by os.environ nor by putenv but only from the Windows API or the shell command setx.
I have a few problems regarding the use of .bash_profile and Pycharm. I am using mac OS X. I created a new project on pycharm with new environment using virtualenv with base interpreter /usr/local/bin/python3.5.
STEP 1:
I then accessed the .bash_profile from my mac OS terminal and exported 2 variables: DB_USER and DB_PASS as my_db_user and my_db_pass respectively.
STEP 2:
Using Pycharm, I imported os and then proceeded to print out the 2 variables using os.environ.get(). Running the .py file using pycharm (F10) returns my_db_user and my_db_pass.
As I decided to create 2 new variables test user and test pass in the virtual environment, I proceeded to activate my venv (venv/bin/activate) in the shell of pycharm. Then, I removed the changes I did in STEP 1.
However, running the .py using pycharm (F10) STILL returns my_db_user and my_db_pass instead of test user and test pass (I have already removed my_db_user and my_db_pass so I have no idea where it is coming from!). On top of that, when I run the python file on the shell using python test.py, it returns (None, None) instead of my desired test user and test pass.
I need help to sort this out so that os.environ.get() returns my desired output. One possible reason is that I might be confused on how pycharm, shell in pycharm and terminals are interacting. Please help thanks!
import os
user = os.environ.get('DB_USER')
password = os.environ.get('DB_PASS')
print(user,password)
If I read your post correctly, you have already cleared out the .bash_profile file, so what remains is just running
unset DB_USER DB_PASS
in each shell window that still has the environment variables.
Inside of my scons script I execute another python script:
fs = env.Command('fs', None, 'python updatefs.py')
AlwaysBuild(fs)
Depends(fs, main)
In python script I am trying to access an environment variable:
import os
mode = os.environ['PROC_MODE']
The variable was previously set up in the shell:
export PROC_MODE='some_mode'
Python complain:
KeyError: 'PROC_MODE'
What is the proper way to propagate environment to an external script?
This is covered in lightly in the FAQ:
FAQ
Basically SCons constructs a clean reproducible set of environment variables so that differences in any user's environment won't break a build.
So if you want to propagate a particular variable from your shell you can explicitly do it as such:
env['ENV']['MY_VARIABLE']=os.environ['MY_VARIABLE']
If you wanted to progagate all environment variables you'd do this:
env['ENV'] = os.environ
Where env is your Environment()
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