Question

NOTE: Many of the same questions have been asked about python raw_input() in sublime text. This question is NOT about sublime. The python code is called in Windows command prompt which unlike the sublime terminal does support interactive inputs.


I have a python program that takes user input with the built-in function raw_input(). See below.

def password_score():
    pwd = raw_input('Enter a password: ')
    gname = raw_input('Enter your first name: ')
...

I call the program in cmd by

echo password_score()|python -i a06q1.py

where a06q1.py is the file name. The path of the python directory has been added to system variable %PATH% temporarily. I am in the directory of the file. My operating system is Windows 7. I am using python 2.6. The same command has worked until now.

Then cmd returns

File "<stdin>", line 1, in <module>
File "a06q1.py", line 27, in password_score
    pwd = raw_input(p_prompt)
EOFError: EOF when reading a line

Is there a way to get around it within cmd?

EDIT: I just tried in on an iOS terminal too. With the same command as in cmd (with quotes), it returns the same error. Is there anything wrong about the command line I used? Thank you!


EDIT: Sebastian's answer solves the problem. The command should adapt to windows as follows.

printf "a06q1.password_score()\n'arg1\n'arg2"|python -i -c "import a06q1"

The single quotes succeeding \n can be replaced by spaces. They separate multiple inputs.

Was it helpful?

Solution

EOF means that there is no more input. And it is true, the only line is consumed by -i option:

$ echo "f()" | python -i -c "def f(): print('x'); input('y\n')"
>>> x
y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in f
EOFError: EOF when reading a line
>>> 

Provide more input:

$ printf "f()\n'z'" | python -i -c "def f(): print('x'); print(input('y\n')*3)"
>>> x
y
zzz
>>> 

As you said: it is "canonically bad" to specify a function to run in such manner. If you don't know in advance, what function you want to run then as an alternative, you could run it as:

$ python -c "from a06q1 import password_score as f; f()" < input_file.txt

OTHER TIPS

What you're trying to do is not the way to call a specific function from the command line. You need an if __name__ == "__main__"-block in your code.

At the end of your file:

`if __name__ == "__main__"`:
     password_score()

And now run the program by:

python a06q1.py

If you run a python file from the command file, the __name__-variable will be "__main__". Notice that if you import a06q1 to some other python file, the name will equal the module name and thus the if __name__ block evaluates to False.

From python docs:

This module represents the (otherwise anonymous) scope in which the interpreter’s main program executes — commands read either from standard input, from a script file, or from an interactive prompt. It is this environment in which the idiomatic “conditional script” stanza is run


As J.F Sebastian writes in the comments, you can also execute a specific python command by providing the -c switch. The following will import the a06q1 and run function_name:

python -c "from a06q1 import function_name; function_name()"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top