3

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.

Argyll
  • 8,591
  • 4
  • 25
  • 46
  • try to use python shell to run the code, or with idle. – Brij Raj Singh - MSFT Mar 11 '14 at 06:01
  • I have only used the shell (in cmd again) for single functions. How do you run a program in the shell please? – Argyll Mar 11 '14 at 06:04
  • Wait. `password_score` is a function inside `a06q1.py`? And then you want to call it be piping the function name into the script? – Hyperboreus Mar 11 '14 at 06:08
  • Yes? Is that canonically bad? – Argyll Mar 11 '14 at 06:13
  • if you have idle installed, you may like to run it from there, it'll run in the python shell – Brij Raj Singh - MSFT Mar 11 '14 at 07:07
  • Hi guys, thank you for your input. I have learned from your response much more than the rather boring assignment I have to do itself. Taking your advice, I will likely never write a program that replies on command prompt interactive inputs and contains many of such functions to be called at the same time. It's just this one that has specific requirements on the code. – Argyll Mar 11 '14 at 07:19

2 Answers2

1

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()"
msvalkon
  • 11,887
  • 2
  • 42
  • 38
  • Appreciate it. Although what if I don't know which function I will want to call at the time of writing the program ? In the past, this has been very frequent for me. – Argyll Mar 11 '14 at 06:29
  • 1
    @Argyll: `python -c "from a06q1 import whatever; whatever()"` – jfs Mar 11 '14 at 06:30
  • @Sebastian: A huge number of errors for various reason. For example, the in program print fails to work somehow. It complains about another file I included in the program on the top. etc etc – Argyll Mar 11 '14 at 06:35
  • @Argyll if your program is meant to be run from the command line, you can look at [argparse](http://docs.python.org/3.4/library/argparse.html). But for developing, just change the function in the if-block if you want to run another one.. You could evaluate the contents of `sys.argv` but that sounds risky. – msvalkon Mar 11 '14 at 06:40
  • @Argyll: `whatever()` is not a real function name. You should provide the name that corresponds to a function in the `a06q1` module. Also, make sure that importing `a06q1` doesn't run any unnecessary code e.g., move it to `main()` function and call it only then the file is executed as a script: `if __name__ == "__main__": main()` – jfs Mar 11 '14 at 06:45
  • @Sebastian: I did change the names. It doesn't work with `echo`, at least seemingly, but work with `printf` as you suggested below. Thanks – Argyll Mar 11 '14 at 06:50
1

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
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Ic. I need to pass in the arguments in the command. Thank you. There is however one more problem. The task I have to do actually asks for multiple raw_input(). How would I deal with that? (\n is not enough to separate them.) – Argyll Mar 11 '14 at 06:45
  • @Argyll: each `raw_input()` requires a new line in the input. If there is no `printf` utility on Windows then just write the input into a file and provide it at the command line e.g., `python -c "from a06q1 import password_score as f; f()" < input_file.txt` – jfs Mar 11 '14 at 06:51
  • There is. And printf works except it considers all lines together as a single string. I wonder if you know a way around it? – Argyll Mar 11 '14 at 06:58
  • @Argyll: you could try `\r\n`. What do you see if you run just `printf "a\nb"`? – jfs Mar 11 '14 at 06:59
  • printf works. When entering multiple arguments though, \n has to be followed by ', but not preceded by ' in cmd. (There are probably other syntax that work) I am adding this to your answer. – Argyll Mar 11 '14 at 07:04
  • btw, if you care, 'z' is taken as the z' in this context. (which is why I thought printf wasn't working initially.) – Argyll Mar 11 '14 at 07:25