Invoking the Interpreter

Hello, I have absolutely no experience with coding but decided to start studying all the available information in Python Docs. I just started reading The Python Tutorial and I am already in need of help understanding! In Section 2.1 Invoking the Interpreter it states:

unnamed

I tried typing that into my Windows Command Prompt, but it says: name 'command' is not identified

Then I tried quoting command a few different ways with the following error results:
python -c “command” [arg] …
name 'command' not identified
python “-c command” [arg] …
command unexpected indent
“python -c command [arg] …”
python is not recognized as an internal or external command, operable program, or batch file

I know there are other ways to launch the interpreter but I want to know/learn everything and exactly how it works or why it wont work for me. I tried the other options the tutorial gave to launch the interpreter and had no issues.

I’m hoping someone could tell me how to use python -c command [arg] … exactly to launch the interpreter. I am using Windows and the Windows Command Prompt so I’m thinking maybe that code is for Unix users? Any replies with your ideas and/or solutions will be greatly appreciated!

Let’s start by ignoring the [args] (square bracket means optional). So it is instead python -c command.

Here command is not to be taken literally. It’s a placeholder of the command you want to write—a better word for command is code[1]. For example:

$ python -c "print('Hello, World!')"

  1. It is a bit out of style, in my opinion. ↩︎

2 Likes

It is correct, you can pass command (code, as you pointed it) and argument(s) for that code.
Try this: python -c "import sys; print(sys.argv)" one "t w o" three

2 Likes

python -c command
outputs an error stating name ‘command’ is not identified

"python -c command"
outputs an error stating: python -c command is not recognized as an internal or external command, operable program, or batch file

python "-c command"
outputs an error stating: command IndentationError: unexpected indent

this code did not launch my Python interpreter but it do something:

image

If you just want to launch the interpreter and get an interactive prompt, you don’t need any arguments at all. Just python

1 Like

I understand that, its just going to bug me if I don’t figure it out, lol! The Python Tutorial says you can launch the interpreter with that argument so there must be a way and I will learn it!

That code did launch an interpreter. However, what you were expecting is for the Python interpreter to be ran in interactive mode (aka Python REPL). See the next section in that tutorial.

2 Likes

Oh, so the interpreter and interactive mode are different?

python is the interpreter itself: it is a program that interprets the python language you feed to it, and executes the code. So “launching the interpreter” is just a way of saying “running the python executable”

2 Likes

What is the exact error message?

If it is NameError: name 'command' is not defined, it’s because the doc means that command is a placeholder, it represents actual real python code that you can type (like my example with an import and a print, with a semicolon to separate them because that’s easier than a newline).

General rule; to get help, always show the exact code and exact error message.

Here the quotes make one big string out of all you typed, so you’re effectively asking your shell to find a program named “python SPACE -c SPACE command”, which does not exist. You have to call the python program, with additional arguments added after spaces. The Python tutorial says a little about that: 2. Using the Python Interpreter — Python 3.12.0 documentation – but not too much, as it’s not its responsibility to teach general shell usage.

Here the quotes mean that the python program receives one argument, which is -c SPACE command.
That’s not a valid form: the doc in your screenshot shows clearly that -c is one thing, then the command (python code, valid python statements), then arguments for that command, all separated with spaces.

When you install Python on Windows, the installer has a checkbox to add the path to the Python folders to the PATH variable. You have two possibilities:

Python not in PATH: You must use the py command (C:\Windows\py.exe). In a command or terminal window, using the py command starts Python in interactive mode, and using py script.py will have Python run the script.py program.

Python in PATH: You may use the py command as described above. You can also use the python command to enter interactive mode or the python script.py command to have Python run the script.py program.

Linux and MacOS do not have a py command so they always uses the python or python3 commands.

If you want to use py -c “code or python -c “code, on Windows, you must use the double quote characters around the entire code string. Embedding double quote characters in the code string is difficult, so it is better to try to use single quotes or triple single quotes between the double quotes instead.

On Linux or MacOS, you can use either the single or double quote character (depending on shell quoting requirements for code).

Okay so I haven’t stopped asking around and trying to figure out this ‘problem’. I have come to a conclusion about this issue and I couldn’t have done it without all the comments and messages received so thank you everyone! Here it is:

To my understanding, paragraph 1 states that when it’s connected to a TTY device, THATS when it is able to execute commands.
The code I’ve been asking about is a command statement.

I then started researching TTY and found out that:

image

Which I have Windows 10 so I have been trying to make this code work for me and I dont even have a TTY!

Conclusion: The second way to launch the interpreter is a ‘hack’ for Unix users and user must have TTY device. Therefore, there is no way (at the moment) for myself to successfully execute this command. Oh well, I’m glad I know now though!