Noob question here, Neovim throws an error when running a script using input() in the command-line window, while the same script runs in vim 8.0. eg
print('Enter your name:')
myName = input()
:! python % <- ex command used
Nvim output:
myName: Traceback (most recent call last):
File "x.py", line 2, in <module>
myName = input()
EOFError: EOF when reading a line
shell returned 1
I prefer the way neovim runs it's scripts in it's own bottom window as opposed to vim outputting to the command line, but I have to switch to vim for any scripts using input().
Is there a nvim.init setting or a different command I can use to succeed here, or is this a known flaw in neovim? I'm on wsl using the latest vim and nvim.
I am not sure why you're getting this error. But, since you are using neovim, have you tried using the built-in terminal emulator? The below command can be used to run the program within newovim in a new split window:
:vsplit term://python3 %
python3 is the name of program, which can be substituted with any other program. Where % is the current file's path. See :h terminal-start for more information.
The same can be done using Vim (8.0 or above) by using the command:
:term python3 %
this will again open a new split for the program running. See :h terminal for more information.
A further optimization to the workflow would be to add a filetype specific mapping. In our case that would be(unix like systems) in ~/.vim/after/ftplugin/python.vim
nnoremap <leader>r :vsplit term://python3 %<cr>
Related
I'm trying to use Jupyter-book to create automatical PDF from my output, but after installing it using the 'pip install -U jupyter-book' command (which runs successfully), it doesn't recognize jupyter-book when I try to run a command:
Input:
jupyter_book create Jupyter_Book_Name
Output:
SyntaxError: invalid syntax (Temp/ipykernel_16888/3781613275.py, line 1)
File "C:\Users\MAXIME~1.DUS\AppData\Local\Temp/ipykernel_16888/3781613275.py", line 1
jupyter_book create Jupyter_Book_Name
^
SyntaxError: invalid syntax
I already try to add the init jupyter_book function in the sys.path but I get the same error, and the error arises for any command. I am working in Python 3.9.7 with Jupyter Notebook in Visual Studio Code on Windows 10.
Thank you in advance, any help would really help me.
Looks like you're confusing a shell (bash / ...) with a Python REPL.
You're running a shell command in a Python interpreter, a bit like if you try to start Python by typing python inside a Python interpreter:
$ python # Here I'm in bash, I run Python
Python 3.9.7 (default, Sep 3 2021, 06:18:44)
>>> python # Here I'm in the Python interpreter
# typing python again...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
# Yup, calling `python` here make no sense, Python looks for a variable
# named `python` not a program named `python`, and won't find it.
So get out of a Python interpreter, find a shell (depending on your OS), and run the command again.
Oh while I'm here, it's probably not:
jupyter_book create Jupyter_Book_Name
but:
jupyter-book create Jupyter_Book_Name
I have just started learning Hadoop. I tried to run a simple mapreduce job on it, but before that I tried to check it locally. But its returning error. Kindly suggest any solution to it. I am using Ubuntu 12.04 LTS.
SO the code is written in gedit, and is ad follows.
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print '%s\t%s' %(word,1)
Then I write the below command in terminal to check if mapper is working fine
maitreyee#bharti-desktop:~$ echo "foo faa" | /home/maitreyee/Documents/mapper.py
and the terminal returns the following error:
/home/maitreyee/Documents/mapper.py: line 1: import: command not found
/home/maitreyee/Documents/mapper.py: line 5: syntax error near unexpected token `line'
/home/maitreyee/Documents/mapper.py: line 5: `line = line.strip()'
You are missing the shebang line at the top of your script. Add something like this (whichever python makes sense for your machine):
#!/usr/bin/python
Here I use the system python under /usr/bin/python
The shebang line is needed because you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment's $PATH.
If you want more to know about writing map reduce code in python, you can follow this
tutorial!
I am using the windows7 command prompt and have opened the python interpreter and changed to the directory where the file is located. The instructions I have say to get into the directory and type
./keyboardControl.py 192.168.1.108
where keyboardControl.py is the name of the file and the ip address is for a robot.
I get the error:
File "", line 1
.\keyboardControl.py 192.168.1.108
SyntaxError: invalid syntax
(with the carrot under the . before )
I have also tried:
python keyboardControl.py 192.168.1.108
I get the same error with the carrot now under the l in Control.
Any help would be greatly appreciated.
It sounds like you've launched the Python interpreter and are typing these commands into the REPL. This is not what you should be doing. The commands should be run directly at the cmd prompt, e.g.:
C:\Users\me>keyboardControl.py 192.168.1.108
If that does not work (file associations might not be set correctly - Windows does not handle the #! "shebang") the form would be.
C:\Users\me>python keyboardControl.py 192.168.1.108
Python is seeing some problem with how I am opening a file with the code below
if __name__ == "__main__":
fileName = sys.argv[1]
with open(fileName, 'r') as f:
for line in f:
print line
It is producing the error
./search.py: line 3: syntax error near unexpected token `('
./search.py: line 3: ` with open(fileName, 'r') as f:'
Am I missing an import? What could be the cause of this?
EDIT: OS - CentOS, Python version 2.6.6
Not sure how I installed, I am running an image from a .edu openstack site. Not sure of the distribution, binaries, ...
You must add import sys in order to use sys.argv. Check this out.
I have tried this:
chmod u+x yourfile.py
./yourfile.py
and it gives me:
./jd.py: line 4: syntax error near unexpected token `('
./jd.py: line 4: ` with open(fileName, 'r') as f:'
If you are doing ./search.py file then add at the beginnig of your file #!/usr/bin/env python. Otherwise, use python file.py input
The problem is that you aren't running your program with Python at all! When you do ./script (assuming that script is a text script, and not a binary program), the system will look for a line at the top of the file beginning with the sequence #!. If it finds that line, the rest of the line will be used as the interpreter of that script: the program which runs the script. If it doesn't find that line, the system defaults to /bin/sh.
So, basically, by omitting the magic line #!/usr/bin/python at the top of your script, the system will run your Python script using sh, which will produce all sorts of incorrect results.
The solution, then, is to add the line #!/usr/bin/python (or an equivalent line, like #!/usr/bin/env python) to the top of your Python script so that your system will run it using Python. Alternately, you can also always run your program using python search.py, instead of using ./search.py.
(Note that, on Linux, filename extensions like .py mean almost nothing to the system. Thus, even though it ends with .py, Linux will just execute it as if you wrote /bin/sh search.py).
Either:
the first line of search.py should be a #! comment specifying the path to locate the python executable, usually [#!/usr/bin/env python](Why do people write #!/usr/bin/env python on the first line of a Python script?
on-the-first-line-of-a-python-script). Usually this is #!/usr/env/bin python . Don't use a hardpath e.g. #/opt/local/bin/python2.7
or else you can invoke as python yourfile.py <yourargs> ...
PREVIOUS: If import sys fails, post more of your file please.
Maybe your install is messed up.
Can you import anything else successfully, e.g. import re?
What are your platform, OS and Python version? How did you install? source? binaries? distribution? which ones, from where?
I was about to test the ftpmirror builtin script (python322, winXP 32bits) from the cmd windows default shell and get this :
File "C:\Program Files\python322\Tools\Scripts\ftpmirror.py", line 161
print('Skip pattern', repr(pat), end=' ')
^
SyntaxError: invalid syntax
I tested the print() line directly in the python shell, trough cmd, and with idle (and in blender also) : this work obsiously.
I reproduce the error with a coucou.py file like this :
#! /usr/bin/env python3
pat = 'toto'
print("Skip pattern", repr(pat), end=" ")
when directly called from a cmd prompt :
C:\Program Files\python322\Tools\Scripts>coucou.py
same error than with ftpmirror
but :
C:\Program Files\python322\Tools\Scripts>python coucou.py
is ok
and my environment is ok I can execute py scripts directly from the windows ui by double-clicking a .py file, and I got working scripts working fine when called from .bat
I don't get it, it looks specific to the print() end argument, what did I not read yet about the way to execute python3 from the windows cmd shell ?
thanks,
Jerome
Try checking if you are running the same python interpreter when you double click or you run python from the command-line.
Save this in a .py file with this content and try running it with both methods:
import sys
print sys.version_info
I bet you are using different interpreters in each case.