Unable to open my source code a second time with `python -i` - python

When I first start bash I can open my code like so:
$ python -i index.py
That file is open, but when I try again this happens:
>>> python -i index.py
File "<stdin>", line 1
python -i index.py
^
SyntaxError: invalid syntax
If I close bash and start again it works. What am I doing wrong?

You can't run terminal commands from the Python REPL.
You can tell you're in the REPL when you see >>> as opposed to $. This means you can run Python code there, but not shell/terminal commands (like the python command).
To exit the REPL, use Ctrl + Z or type exit() and press enter. This will bring you back to the regular terminal.
In addition, I'd recommend running just python index.py rather than python -i index.py in most cases.
The added -i means that you'd like to stay in the REPL to inspect the results after running the index.py file. It allows you to continue running additional Python code after the index.py file has finished its execution.

It looks by the three >>> that you are in the python console not in bash itself. If you type exit() you should get back to bash, and then you can try the code again.

Related

how to remove std redundant output of python program in vs code ide?

I'm using vs code for python development. I need to unclutter some std output of path whenever i run my python code.
Here below i run "Hello world" in a file any2.py - is there way to remove all that lengthy path before the actual output?
PS C:\Users\erjan\Desktop\kkkk> c:; cd 'c:\Users\erjan\Desktop\kkkk'; &
'C:\python38\python.exe' 'c:\Users\erjan\.vscode\extensions\ms-python.python-2022.12.1\pythonFiles\lib\python\debugpy\adapter/../..
\debugpy\launcher' '55841' '--' 'c:\Users\erjan\Desktop\kkkk\any2.py'
Hello world!
I dont know what this /debugpy/..adapter//launcher is all about, it is sometime tiring to read unrelated output
\debugpy\launcher'
You are using debug mode:
You can try Run Python File, in front of command, only the path of Python interpreter will be displayed.
You can also try code-runner extension.
Then change the settings:
add the following code to this setting.json:
"python": "python",
Here is the result in terminal:

Shell script: time and python into a file

I need to write a bash script, that executes a python program and i need to output the time of execution and the results in the same file.
I CAN NOT edit the python code.
As there are multiple tests I want to execute them in background.
I've tried this
#!bin/bash
$(time python3 program.py file1 > solAndTimeFile1.txt &)
but it didn't work at all, it only outputs the python program results in the solAndTimeFile1.txt and the time is shown in the terminal.
I've also tried this:
#!bin/bash
$(time python3 program.py file1 > solAndTimeFile1.txt >> solAndTimeFile1.txt &)
Same output and makes even less sense to me.
Put your command into curly braces so it is run in a subshell and you can capture its output. To redirect both stdout and stderr to a file use &>file. See man bash for further information.
{ time python3 program.py file1; } &>solAndTimeFile1.txt &

R equivalent to `python -i`

Typing python -i file.py at the command line runs file.py and then drops into the python terminal preserving the run environment.
https://docs.python.org/3/using/cmdline.html
Is there an equivalent in R?
I may be misinterpreting what python -i file.py does, but try:
From inside R, at the terminal, you can do:
source('file.R')
and it will run file.R, with the global environment reflecting what was done in file.R
If you're trying to run from the command line, review this post

Python Command Output Terminal

This question is most likely on here somewhere, and anyone who can redirect me, that would be great.
But I can't find it - most likely not sure which appropriate key terms to use as everything gives me the python command line interpreter.
But I simply want to be able to use the output from a python as the input to another program from the command line. For example:
./program `python print 'A' * 100`
However, I get an error of:
python: can't open file 'print': [Errno 2] No such file or directory
What is the proper way to do this?
the python executable with no switches expects no arguments(for an interactive shell) or a *.py file to run
you can use the -c switch to pass in code
./program `python -c "print 'A' * 100"`
python -c "print 'A' * 10" | ./program

python script to send commands to vim (mvim)

I wrote a very simple vim plugin and python script trying to test some communication between the two. My vim-script looks like this:
function! HelloWorld()
silent :!python helloworld.py
endf
nmap <C-P> :call HelloWorld()<CR>
then my python script looks like this:
import os;
os.system( 'mvim --servername VIM -u NONE -U NONE --remote-send \"<C-\\\\><C-N>:echo \'Hello World!\'<CR>\"' )
If I am in vim and press , use the ":call HelloWorld()" command, or just type ":!python helloworld.py" from the same or another mvim or vim instance, nothing happens. However, if I call the script from the command line separately, mvim responds appropriately: shows "Hello World!" along the bottom.
Does anyone have any idea why it is not working when called from vim?
Try replacing
silent :!python helloworld.py
with
silent :!(sleep 0.5s && python helloworld.py) &
redraw!
(the point is in returning to vim before remote command arrives). If it works, then the problem is in processing remote commands while receiving shell output. You can also try another workarounds:
call system('python helloworld.py')
,
call system('python helloworld.py &')
and
pyfile helloworld.py
(Note that the last one requires vim compiled with +python feature and also alters the state of python interpreter used by vim).
By the way, use system() call instead of ! when you don't want to see the script output. Also use redraw! after silent !.

Categories

Resources