This question already has answers here:
set environment variable in python script
(3 answers)
Set shell environment variable via python script
(4 answers)
Closed 4 years ago.
I tried both
os.environ['TemporaryVar'] = "Apple"
and
os.putenv("TemporaryVar", "Apple")
but after running the python script, when i run the command env or set in the command line, i do not see variable as
TemporaryVar=Apple
I also have a sleep(50) in the python script so that the script does not terminate while i am checking for set or env in command line.
What i want is to write a python script that adds a variable to the environment variable. (I should be able to see it when i type env)
This is not possible.
Environment variables are defined in the scope of the job that sets it. That's why when you define an environment variable in one terminal session ,you won't see it in other sessions. The same thing is happening with your script. A job runs your python script but you are trying to read variables from another terminal session(another job), so you can't see them. Depending on your goal, you have to find a workaround.
Related
This question already has answers here:
How do I run a Python program in the Command Prompt in Windows 7?
(24 answers)
Closed 1 year ago.
I want to do is create a new python script so that I can start coding in python.
I am new to using windows command prompt. How can I access and create a new python script using the command prompt window? I have tried using cd (location of already made script). I have also just used data\scripts which is where the rest of the python scripts are located.
I have also attached a screenshot of what I attempted.
To access this in the CMD, you just need to type:-
cd data\scripts
Then you will be in your directory.
If you just want shell access, type:-
python
in the interface.
This question already has answers here:
Environment variable used in shell script appear blank in log file when run by cron
(3 answers)
Closed 1 year ago.
I have set my environment variables in /etc/profile and ~/profile. When I run an echo $EVNAME it returns correctly but running it as a Cron job returns None.
I'm running it on Raspberry Pi 4 and script using Python3.
Any help with this one?
The proper path for the profile file is /etc/profile. If you're intending to set it to a specific user, it's ~/.bashrc.
See here: https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/#persistent-environment-variables
This question already has answers here:
How to run a Python script portably without specifying its full path
(4 answers)
Closed 3 years ago.
I am looking for a way to run a python script from anywhere.
I have seen this solution but as it is based on Linux I am looking for a way on Windows (10).
Basically what I want to do is, execute a script with a given parameter. Because it could be disturbing for the user, I am using a .pyw-file extension to hide the console.
Things that came to my mind:
a PATH-Variable
PowerShell-Script
Batch-file
Sadly I am not familiar/experienced with neither of those, so I can't really tell if these ideas even provide a way to do that.
Any answer is appreciated.
Edit: I would like to make the command as short as possible for the client so it is not necessary to write a novel to exec one simple task.
Another Edit:
I want the user/client to open its cmd/ps and use a command which executes my python-script, which is not in this directory he is, when opening the cmd. So the script is somewhere on his computer, but shall be executable by command from anywhere.
try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.
This question already has answers here:
Pycharm: set environment variable for run manage.py Task
(16 answers)
Closed 5 years ago.
I have an environment variable exported in bash. Project is in localhost. OS is Debian 9 Stretch.
When I run, in command line:
python -c "import os; print(os.environ.get('EMAIL_PASSWORD'))"
I get the variable value.
But, inside Django, calling os.environ.get('EMAIL_PASSWORD') returns None.
In settings:
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')
Already find n similar questions, but in general the problem is that the variable wasn't exported to environment.
What I am missing?
Update:
Django virtual server is called directly from Pycharm
export $EMAIL_PASSWORD will not work. First remove the $ and define the variable value in the shell, not only the variable name. It should look like this: export EMAIL_PASSWORD='value'
Edit
Don't put the ''around the var when you call it. Django will think its a string, not a variable.
This question already has answers here:
How do I set the working directory of the parent process?
(8 answers)
Change directory of parent process after exit [duplicate]
(2 answers)
Closed 9 years ago.
Observe the following:
$ pwd
/home/username
$ python
>>> import os
>>> os.chdir("/")
# Ctrl + D
$ pwd
/home/username
But I want to be in the / dir after exiting the python interpreter, is that possible using python?
I would like to know because I want to make a platform independent script(using python) where an optional convenience command cd's the user into a certain directory.
But I want to be in the / dir after exiting the python interpreter, is that possible using python?
It is not possible. Neither by using Python or any other "acceptable" way. By acceptable, I mean "without outrageously hacking your system (with gdb for example)" ;)
More seriously, when the user launch an executable from a shell, the child process will run in its own environment, which is mostly a copy of its parent environment. This environment contains "environment variables" as well as the "current working directory", just to name those two.
Of course, a process can alter its environment. For example to change its working directory (like when you cd xxx in you shell). But since this environment is a copy this does not alter the parent's environment in any way. And there is no standard way to access your parent environment.