This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Python subprocess/Popen with a modified environment
(9 answers)
Closed 3 months ago.
Basically, os.system() can execute shall commands, such as:
os.system("echo $A")
It will work well and output value environment variable A.
But this seems to not work:
os.system("A=b echo $A")
It won't output "b" as expected.
How Python can execute this type of command command?
It won't output "b" as expected.
The expansion $A to empty string is done by the shell earlier, before running echo.
Try this:
os.system("A=b; echo $A")
Related
This question already has answers here:
Environment variables set from python not visible in shell script
(2 answers)
Closed 7 months ago.
I defined a shell script with a python script inside on the fly. Getting variables from shell to Python works fine, but the other way round does not work as expected. My Idea was to create an env variable os.environ["NEW_VAR"] = "test", but when trying is echo it, it is just None.
#!/bin/bash
args=("$#")
GIT_PASSWORD=${args[0]}
export GIT_PASSWORD=$GIT_PASSWORD
python - << EOF
import os
print(os.environ.get("GIT_PASSWORD"))
os.environ["NEW_VAR"] = "test"
EOF
echo $GIT_PASSWORD
echo $NEW_VAR
echo "Back to bash"
Why does that not work and 2. What is the correct way to pass a variable here?
Thank you!
Written in the comments, inheriting environment variables only works from parent to child processes, not the other way round.
if you need shell variables set from a python script you can create the output in your shell script and eval the results:
eval "$(python -c 'print("HELLO=WORLD")')"
echo "$HELLO"
yields WORLD
This question already has answers here:
How to run .sh on Windows Command Prompt?
(11 answers)
Why does "pip install" inside Python raise a SyntaxError?
(7 answers)
Closed 10 months ago.
I am trying to run a shell script in Spyder (Python 3.8).
I have tried the following and all this is giving me the same error - Invalid Syntax
bash ./filename.sh
bash filename.sh
sh filename.sh
sh ./filename.sh
shell ./filename.sh
shell filename.sh
import subprocess
subprocess.run(.\filename.sh)
source filename.sh
9.
import subprocess
subprocess.run(filename.sh)
The last one gives the error:
name 'filename' doesn't exist.
Note: I have rechecked my pwd.
Add quotes
subprocess.run("filename.sh")
Also, I would like to point out that unless you are using some package to add the syntax bash, sh, etc... you would get the syntax error since Python doesn't know what those keywords mean.
This question already has answers here:
How to execute Python inline from a bash shell
(4 answers)
Closed 2 years ago.
In a shell, I can execute AppleScript command-line code like so:
osascript -e "tell application \"Finder\" to activate"
Is the same thing possible in Python, e.g.:
python --execute "print('hello world!')"
Something like this should work for you!
python -c 'print("Hi")'
Probably a duplicate of How to execute Python inline from a bash shell
This question already has answers here:
How to store standard error in a variable
(20 answers)
Closed 7 years ago.
Need to check the version of python so for that i am hitting this command in a script.
I am trying to store the output of the given command in a variable "x".So that I can use this x in further script.
But when I am trying to print x it is showing null value(no value).
[bin]$x=`/path/thirdparty/python/2.7/bin/python2.7 -V`
Python 2.7.8
[bin]$echo x
Please help me to store the value of the command in a variable.
python -V writes to the standard error, not to the standard output. So you have to redirect STDERR (2) to STDOUT (1).
$ x=$({python -V} 2>&1)
$ echo $x
Python 2.7.6
Hm... the output is possibly written to stderr, not stdout. Try this:
x=`/path/thirdparty/python/2.7/bin/python2.7 -V 2>&1`
This question already has answers here:
Run text file as commands in Bash
(5 answers)
Closed 8 years ago.
I have multiple commands in a file in the following format
python dosomething.py arg1 arg2
python dosomethingelse.py arg1 arg2
I want to execute one by one.
What is the elegant way of doing it.
Thanks
If you have those lines in a file (say, run-python-scripts.sh), you can run it with sh:
sh run-python-scripts.sh