I need to interpret a python program line by line. I am using -c option to python and have makefile like this.
all:
python -c
"print 'aa'
print 'bb'"
When I run it with make I get
python -c "print 'aa'
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [all] Error 2
when I take out the same python lines and run from bash, it works fine. What could be the problem?
If your Makefile truly is
all:
python -c
"print 'aa'
print 'bb'"
I would expect to see more errors. With that makefile, make will first attempt to run python -c, which should generate errors like:Argument expected for the -c option. It will then abort and not even try to run the shell command "print 'aa'. You need line continuations and semi-colons.
all:
python -c \
"print 'aa'; \
print 'bb'"
The semi-colon is necessary because make strips all the newlines and passes the string python -c "print 'aa'; print bb'" to the shell (whatever SHELL is set to).
Every line of a make rule is executed in a different shell instance. You need to escape the newlines (with \) or put it all on one line.
Also the makefile snippet as given should be giving you an error about unexpected arguments to -c. Your error indicates that your snippet is actually:
all:
python -c "print 'aa'
print 'bb'"
Not that that changes anything.
Have a look at this question. I think your problem is that your program is spanning multiple lines, but your makefile isn't interpreting it that way. Adding slashes should clear that up.
Multiline bash commands in makefile
Related
I have a simple shell script where i want to be able to pass variables to some inline python i will write. For example like this
funny=879
echo $funny
python -c "
print(f"hello {$funny}")
"
However this prints
879
File "<string>", line 2
print(fhello
^
SyntaxError: unexpected EOF while parsing
(pipeline) $
Any thoughts on what i could be doing wrong? I know i am setting the variable correct because when i do echo it prints out the variable so it is definitely set correct but for some reason python script is not able to use it.
It's because you're using outer double quotes.
python -c "print(f"hello {$funny}")"
Gets turned into:
python -c print(fhello {879})
So python is passed 2 separate strings.
The inner double quotes would need to be escaped in order to get passed through to python.
$ funny=879; python3 -c "print(f\"hello $funny\")"
hello 879
Instead of messing around with quoting - if you export your variables you can access them from python using the os.environ dict.
$ export funny=879; python -c 'import os; print(os.environ["funny"])'
879
You can use the var=value command syntax and omit the export (note the lack of a semicolon)
$ funny=879 fonny=978 python3 -c 'import os; print(os.environ["funny"], os.environ["fonny"])'
879 978
As the question states, I would like to know how to execute arbitrary python code in a windows terminal (or Anaconda Prompt). Please note that I am on Windows. In general, terminal execution is something like:
>>python -c "print('Some Text to Print')"
But what happens if I have multiple lines with indents and indent-indents. Here is an example of things I have tried (please note that I am forcing two lines here in some way, the solution "for i in range(10): print(i)" is not appropriate to the question since this only has one line of python code):
>>python -c "for i in range(10):; print(i)"
>>python -c "for i in range(10):\nprint(i)"
>>python -c "for i in range(10):\n print(i)"
>>python -c "for i in range(10):\n\tprint(i)"
Now clearly I can do this:
>>python -c "exec('for i in range(10):\n\tprint(i)')"
and this solution works. But this solution feels wrong. Is there a more pythonic way? In particular, why do the newline and tab characters work for "exec()" but not for "python -c"?
Have you already tried with ^ ?
In this case if you will not be careful with spaces, you will obtain an IndentationError: unexpected inden
python -c ^
More? "for i in range(2): ^
More? print(i)"
[EDIT]
There are also another way:
python -c ^
More? "for i in range(2):^ print(i)"
0
1
^ is the EOL character used as the new line character in most other non-Unix operating systems, including Microsoft Windows and Symbian OS.
You can do
> echo "for i in range(2):
>> print(i)
>> " | python
0
1
I want to get result of a Python function in terminal.
I tried to run the command:
$ python3 -m uuid uuid.uuid4().hex
And I expect to see the output be something like: '78cbf0fadaa34ff7ac3f7b965965e207'
Unfortunately I get error:
-bash: syntax error near unexpected token `('
You were close.
The flag to run a single command is -c and not -m.
You also need to import uuid so you can use it.
You also need to use print() to actually see some output.
Finally the whole passed command has to be in quotes.
$ python3 -c "import uuid; print(uuid.uuid4().hex)"
8e79508445db4aca91bb0990529fdd89
This question is most likely on here somewhere, and anyone who can redirect me, that would be great.
But I can't find it - most likely not sure which appropriate key terms to use as everything gives me the python command line interpreter.
But I simply want to be able to use the output from a python as the input to another program from the command line. For example:
./program `python print 'A' * 100`
However, I get an error of:
python: can't open file 'print': [Errno 2] No such file or directory
What is the proper way to do this?
the python executable with no switches expects no arguments(for an interactive shell) or a *.py file to run
you can use the -c switch to pass in code
./program `python -c "print 'A' * 100"`
python -c "print 'A' * 10" | ./program
I have this command as part of a bash script
$(python -c "import urllib, sys; print urllib.unquote(sys.argv[0])", "h%23g")
But when I run it, I get this:
-bash: -c: command not found
As though bash has missed reading the python, and is thinking -c is the name of the command. Exactly the same happens when using backticks.
How can I make bash recognise the python?
the Python command is returning the string "-c" from your $(...) structure, which bash then tries to execute.
for example
python -c "import urllib, sys; print urllib.unquote(sys.argv[0])"
prints "-c", so you are essentially asking bash to interpret $(-c), for which the error is valid.
I think you want something like the following:
$(python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" "h%23g")
This will result in h#g, if this is all you have on a line then it will also attempt to run a command called h#g, so I'm assuming you are actually using this as a part of a larger command.
The issue with your version is that sys.argv[0] is the -c from the command, and urllib.unquote('-c') will just return '-c'.
From the documentation on sys.argv:
If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'.
Combining that with info from the man page (emphasis mine):
-c command
Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).
So, when you use -c, sys.argv[0] will be '-c', the argument provided to -c is the script so it will not be included in sys.argv, and any additional arguments are added to sys.argv starting at index 1.