简体   繁体   中英

How do I pass arguments to a script piped to the Python interpreter on stdin?

I'm installing poetry using the get-poetry.py script, and I want to specify the version to install. To install the latest version I do

GET_POETRY_URL=https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
curl -sSL $GET_POETRY_URL | python

The get-poetry.py script takes an argument --version , but how do I pass that through?

curl -sSL $GET_POETRY_URL | python --version 1.1.4

prints the installed python version, rather than passing the argument through to the get-poetry.py script. I could save the script to a file and call it that way, but I'm doing this in a docker image and I don't want to deal with cleaning it up afterwards.

curl -sSL $GET_POETRY_URL | python - --version 1.1.4

Using - as the script location when you call python will cause it to read from stdin. As usual, any arguments after the script location are passed to the script:

$ echo "import sys; print(sys.argv)" | python - --version 1.1.4
['-', '--version', '1.1.4']

Using - to mean stdin also works for many other unix tools:

$ echo hello | cat -
hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM