I'm trying to add input to my python code
isAge = input("Enter your age: ")
and it shows me the following error:
enter your age: Traceback (most recent call last):
File "test2.py", line 3, in <module>
isAge = input("enter your age: ")
EOFError: EOF when reading a line
I use "w !python" to run the code.
I tried w !python 18" Where 18 is the age number, but it didnt work
If you use :w !python from Vim, it will start a Python interpreter and pipe the script to the interpreter, which means the Python interpreter's standard input will be connected to this pipe through which Vim will send it the script, and not to the console.
So Python functions such as input() will not work to read input from the user, since the interpreter does not have its standard input connected to the console.
To solve this, instead of having Vim pipe the script to a Python interpreter, have it invoke the Python interpreter with the script as an argument. So instead of :w !python, use:
:w
:!python %
This assumes you're editing a file and not an unnamed buffer. If you have an unnamed buffer, then save it to a *.py first.
Related
I am trying to take input() from keyboard but EOFError is received.
Does anyone know how to solve this issue?
test.py
import sys
if sys.stdin.isatty():
print("keyboard input is working")
else:
print("keyboard input is not working")
# read all input from stdin
data = sys.stdin.read()
print(data)
r = input("Enter something here:")
print(r)
Now, create a dummy text file and pass it to python
#dummy.txt
dummy text goes here.
I then called the python script on Windows using this command:
C:\> test.py < dummy.txt
This is the error I received:
keyboard input is not working
dummy text goes here.
Enter something here:Traceback (most recent call last):
File "C:\test.py", line 11, in <module>
r = input("Enter something here:")
EOFError: EOF when reading a line
C:\>
I kind of know how to solve this issue in Linux by open a file descriptor on /dev/tty and pass it back to sys.stdin but how do I archive this on Windows?
I notice function msvcrt.getch() could work, but there should be a better solution that I can utilize input() function for cross-platform compat.
Please help, anyone!
I did a very simple program :
a = input("Enter a number A : ")
b = input("Enter a number B : ")
print("Below 2 strings concateneted :")
print(a + b)
When i run it from bash, i write :
python3 <my_program.py>
It works well
When i run it from vim, i open "my_program.py" in vim and then write :
:w !python3
It crashes and gives me this message :
Enter a number A : Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line
when i run it from a terminal opened from vim, i write :
:terminal
then
python3 <my_program.py>
This works.
Why i can't execute python3 programs from vim ?
EDIT : it's linked with the input function. If i directly assign variables it works. More information are welcome.
It's because of the input function in my python script.
Because i want to keep this function. No choice, open a separate terminal.
By the way, it's be possible to open the terminal in VIM (for convenience) writing in VIM normal mode :
:vert term
I'm using Win10 & Scite with utf-8 enabled output window.
The file is saved as UTF-8 with BOM
Script:
print('ダイスキ from python 3')
The script can be run on cmd prompt without error. But when run on Scite it will produce error:
Output:
>pythonw.exe -u "test.py"
Traceback (most recent call last):
File "test.py", line 12, in <module>
print('\u30c0\u30a4\u30b9\u30ad from python 3')
File "D:\BIN\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-2: character maps to <undefined>
>Exit code: 1
How to properly print ダイスキ to stdout using python3 with Scite?
Updates:
I've edited Scite Global Options file, to support utf-8.
code.page=65001
I've tested C, Lua, old Python 2.7, and it can print utf-8 strings (on Scite output window).
Seems to be Scite configuration error or a maybe Scite bug, because the Scite output terminal window works on Lua & C, but fail only on Python3.
Scite involves popen() / piping the STDOUT.
Python 3.7 need env variable "PYTHONIOENCODING" to be set manually. So you need to add environment variable "PYTHONIOENCODING" set to "utf_8"
Result:
Try to do this:
print(u'ダイスキ')
I'm having trouble with the call function
I'm trying to redirect the output of a program to a text file by using the '>'
This is what I've tried:
import subprocess
subprocess.call(["python3", "test.py", ">", "file.txt"])
but it still displaying the output on the command prompt and not in the txt file
There are two approaches to solving this.
Have python handle the redirection:
with open('file.txt', 'w') as f:
subprocess.call(["python3", "test.py"], stdout=f)
Have the shell handle redirection:
subprocess.call(["python3 test.py >file.txt"], shell=True)
Generally, the first is to be preferred because it avoids the vagaries of the shell.
Lastly, you should look into the possibility that test.py can be run as an imported module rather than calling it via subprocess. Python is designed so that it is easy to write scripts so that the same functionality is available either at the command line (python3 test.py) or as a module (import test).
I want to execute following command on linux terminal using python script
hg log -r "((last(tag())):(first(last(tag(),2))))" work
This command give changesets between last two tags who have affected files in "work" directory
I tried:
import subprocess
releaseNotesFile = 'diff.txt'
with open(releaseNotesFile, 'w') as f:
f.write(subprocess.call(['hg', 'log', '-r', '"((last(tag())):(first(last(tag(),2))))"', 'work']))
error:
abort: unknown revision '((last(tag())):(first(last(tag(),2))))'!
Traceback (most recent call last):
File "test.py", line 4, in <module>
f.write(subprocess.call(['hg', 'log', '-r', '"((last(tag())):(first(last(tag(),2))))"', 'work']))
TypeError: expected a character buffer object
Working with os.popen()
with open(releaseNotesFile, 'w') as file:
f = os.popen('hg log -r "((last(tag())):(first(last(tag(),2))))" work')
file.write(f.read())
How to execute that command using subprocess ?
To solve your problem, change the f.write(subprocess... line to:
f.write(subprocess.call(['hg', 'log', '-r', '((last(tag())):(first(last(tag(),2))))', 'dcpp']))
Explanation
When calling a program from a command line (like bash), will "ignore" the " characters. The two commands below are equivalent:
hg log -r something
hg "log" "-r" "something"
In your specific case, the original version in the shell has to be enclosed in double quotes because it has parenthesis and those have a special meaning in bash. In python that is not necessary since you are enclosing them using single quotes.