Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use python -c on windows?

Tags:

python

https://docs.python.org/3/using/cmdline.html

This is the option documentation. but it doesn't provide me any useful message

I want to execute code in this way

python -c "def hello():\n    print('hello world')"

the error message

PS C:\Users\Administrator> python -c "def hello():\n    print('hello world')"
  File "<string>", line 1
    def hello():\n    print('hello world')
                                         ^
SyntaxError: unexpected character after line continuation character

it works on Linux, but not windows.

Hope you can give a full fixed command ~~~

Another question connected with this one \n problem when using javascript to exec "python -c "

like image 972
somewheve Avatar asked Mar 02 '20 22:03

somewheve


People also ask

How do I run Python on Windows?

Go to your Start menu (lower left Windows icon), type "Microsoft Store", select the link to open the store. Once the store is open, select Search from the upper-right menu and enter "Python". Select which version of Python you would like to use from the results under Apps.


1 Answers

Try backtick instead of backslash.

Error:

PS C:\Users\me> python -c "def hello():\n    print('hello world')"
  File "<string>", line 1
    def hello():\n    print('hello world')
                                         ^
SyntaxError: unexpected character after line continuation character
PS C:\Users\me>

Ok:

PS C:\Users\me> python -c "def hello():`n    print('hello world')"
PS C:\Users\me>

Useful:

PS C:\Users\me> python -c "def hello():`n    print('hello world')`nhello()"
hello world
PS C:\Users\me>

Just echoing to see it:

PS C:\Users\me> echo "def hello():`n    print('hello world')`nhello()"
def hello():
    print('hello world')
hello()
PS C:\Users\me>

See PowerTip: New Lines with PowerShell

like image 134
Kelly Bundy Avatar answered Oct 11 '22 14:10

Kelly Bundy