I am generating command passed to python -c like this
'python -c "import '+impMod+'; help('+module+'.'+method+') if \''+method+'\' in dir('+module+') else from '+impMod+' import '+method+' help('+method+')"'
and get output like this:
python -c "import os; help(os.path.pathconf) if 'pathconf' in dir(os.path) else from os import pathconf help(pathconf)"
even if i try
python -c "import os; help(os.path.pathconf) if 'pathconf' in dir(os.path) else from os import pathconf; help(pathconf)"
but don't know why I get SyntaxError: invalid syntax
Any help will be appreciated,
Regards.
You are mixing up statements and expressions. The from .. import .. syntax is a statement, and cannot appear inside an expression, but you are using it inside a ... if ... else ... expression. Also, you can use newlines inside a shell string.
python -c "import os
if 'pathconf' in dir(os.path):
help(os.path.pathconf)
else:
from os import pathconf
help(pathconf)"
To do that in Python, you might want to use triple quotes.
Related
So as an example, I know that embedding python in bash as follows work:
python -c "import os
dict_name[\"var1\"]=1
dict_name[\"var2\"]=2
"
However, when I do the same exact thing in tcsh, I get the following error:
ERROR = Unmatched ".
Would anybody happen to have any insight on this? Would I have to python - c "" every line?
Thank you so much for your time.
Try this
python -c 'import os\
dict_name = {}\
dict_name["var1"]=1\
dict_name["var2"]=2\
'
I am trying below commands
python -c 'import sample; sample.Functionname()'
python -c 'import sample; sample.printFxn("helloWorld")'
Both of these work well but when I pass a variable as an argument, I get the following error.
File "<string>", line 1 import sample; sample.printFxn($filename) SyntaxError: invalid syntax
What is the proper way to pass a variable as an argument to a python function from bash?
Don't interpolate string variables into the command; pass the value as an argument to the Python script.
python -c 'import sys, sample; sample.printFxn(sys.argv[1])' "$fileName"
Single quotes (') prevent the shell from parsing a string an evaluating variables. You can use double quotes instead so the shell will evaluate your variable:
python -c "import sample; sample.printFxn('$fileName')"
I am trying to write this small python program to execute with python through the -c option:
python -c "import sys;if 2==sys.version_info.major: raise RuntimeError('Must use python3')"
However, this is raising a syntax error:
File "<string>", line 1
import sys;if 2==sys.version_info.major: raise RunTimeError('Must use python3')
^
SyntaxError: invalid syntax
is there a way to write this such that it does work in the above? And if it's invalid, is there a canonical reference to what syntaxes are allowed in -c executed code?
I am doing this in a Makefile.
Literal newlines are perfectly valid inside single-quoted strings in POSIX shells:
python -c '
import sys
if 2 == sys.version_info.major:
raise RuntimeError("Must use python3")
'
This means you aren't dependent on having bash, ksh93 or zsh with the $'' extension.
If this is in a Makefile:
define python_script
import sys
if 2 == sys.version_info.major:
raise RuntimeError("Must use python3")
endef
test:
python -c "$$python_script"
You can use \n.
python -c "import sys"$'\n'"if 2 == sys.version_info.major:"$'\n'" raise RuntimeError('Must use python3')"
...this assumes you're using bash or some closely related shell. But otherwise obviously you can still just have newlines in the string, especially if you're calling python -c from a program using exec or something.
I am trying to use python -c command line option but cannot seem to make it work.
What is the right way to use it?
Sometime it is really useful to store the whole command and one line and make an alias for it then going into the interactive mode.
The following gives no output
-bash-3.2$ python -c "
import hashlib
hashlib.md5('test').hexdigest()"
But of course following works
-bash-3.2$ python
>>> import hashlib
>>> hashlib.md5('test').hexdigest()
'098f6bcd4621d373cade4e832627b4f6'
>>>
you've got to print what you want to see if in non-interactive mode.
python -c "import hashlib
print hashlib.md5('test').hexdigest()"
the interactive mode always prints the return values, but this is just a gimmick of the CLI
python -c "import hashlib; print(hashlib.md5('test').hexdigest())"
>python -c "import hashlib; print hashlib.md5('test').hexdigest()"
098f6bcd4621d373cade4e832627b4f6
You are missing the print, which is why you don't see anything.
For a Bash completion script I need to get all the variables from an installed Python module that match a pattern. I want to use only Python-aware functionality, to avoid having to parse comments and such.
You can use python -c to execute a one-line Python script if you want. For example:
bash$ python -c "import os; print dir(os)"
If you want to filter by a pattern, you could do:
bash$ python -c "import os; print [x for x in dir(os) if x.startswith('r')]"
['read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'rmdir']