Vim for python and R scripting (in Screen or not) - python

I've been trying to find this information online but I'm not getting the answer.
I've used RStudio and Geany for editing files before. Now I'm trying to use ViM to edit python and R files (I know there's RPy, but nothing to do with my problem).
I would like to know how can I have 3 terminals (could also be vim buffers, or screen windows) with one running ViM and the others running R and Python. When I execute a Python script, the terminal (window or buffer) with python shows the output. The same when I run R scripts.
I would appreciate insight on this as this is something that's keeping me from using ViM regularly. I would also consider a solution with terminator terminal multiplexer or guake terminal. Any information about sending code for scripting from one instance to another is welcome.

Are you looking for a way to have a REPL inside Vim? If so, Vim wasn't really designed with that in mind, though there are some plugins that try. Conque is an example.
Some things I use to have a quicker code/run/test iteration with Python:
IPython's %edit feature, which starts editing a script with $EDITOR and will run the script after you exit.
vim-ipython which can send/execute/recieve code via an IPython interpreter.
tmux which allows you to have multiple shells side by side, but with little interaction between them.

Vim-slime is a general-purpose solution to this I'm pretty happy about, it will send blocks of code to any tmux pane, meaning it works for any language.
https://github.com/jpalardy/vim-slime

Your requirements for online information may not have been spelled out in enough detail, since I seem to find a wealth of information on using ViM as an IDE for both R and Python:
R:
http://www.r-bloggers.com/r-with-vim/
http://www.vim.org/scripts/script.php?script_id=2628
http://www.vim.org/scripts/script.php?script_id=1048
Python:
http://wiki.python.org/moin/Vim
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/

Have a look at vim-ipython, a plug-in for Vim.
You need to download the source (linked above), and run the Vim command :source path/to/file/ipy.vim.
Start by running a new IPython session (e.g. using IPython qtconsole or IPython notebook) and then type :IPython into Vim. Your Vim is now connected to the IPython instance you just opened.
You can press F5 to run the whole python script in your Vim, or Ctrl+s to run the current line. Ctrl+s will also run whatever is selected if you're in visual (i.e. 'select') mode.

Related

Open Python interactive session and editor

I am looking for some pieces of advices in order to accomplish a tiny task regarding Python. If someone would ask to provide a pic of a 'started interactive session of Python with your favorite editor with a Python script', what would you show to this person? Should it be a void script? How do you interpreter 'started interactive session'? How about your own favorite editor (I mean that you would suggest for Windows 10)?
Sorry for the triviality of my question,but I have just started with beginners' Python course
Just to make sure I am on the right way, if I have to submit to someone else a started interactive session and your favourite editor with a Python script, will be it sufficient to show the following windows as in the picture?
If you want to do interactive things, you probably just want to use jupyter notebook: https://jupyter.org/install#jupyter-notebook
You can always just type python at your terminal prompt if you have python installed, this will start an interactive session in your terminal, but jupyter is definitely easier to use once you get it set up.
Edit: regarding favourite editor, this is very much opinionated but I love sublime text. https://www.sublimetext.com/
Note that you probably would use one or the other: you would use a text editor to write scripts that could be run in a terminal, for example
# hello_world.py
print("Hello, world!")
then in your terminal
python hello_world.py
whilst you would use a jupyter notebook for example for quick experimentation or for demonstrating usage of your software to others.
In terms of interactivity, you should really check out Jupyter Notebooks. It's industry standard for a lot of tasks, widely used and with great performance and support. Also, Jupyter has an in-built code editor than can be run via localhost in your browser.
However, for a code editor, I will never stop recommending VSCode. Huge game changer, light-weighted and with support for pretty much any language. Jupyter notebooks can be run directly from VSCode, and the latest features that Microsoft introduced in this regard make using Jupyter inside VSCode really easy and intuitive. Also, extensions are a positive point as well.
If you're only planning on programming with Python, maybe checking out PyCharm is also a good idea, but I have working mainly in Python for the past 3 years and I have never missed anything on VSCode, even though I've tried PyCharm several times.
The simplest answer: go with the IDLE IDE, it comes bundled with Python by default. It starts with an interactive session, so you just type at the prompt (>>>):
print ("Hello, world!")
and your task is done, if it prints out your "Hello, world!" line.
In general, you start your python interactive session (python REPL, python terminal, python console, there are several terms for the same thing) and, since it's an interpreted language, everything you type in, Python will interpret and execute, if valid. Nevertheless, except for atomic examples, you'll want to use a code editor or IDE.
As a beginner, I'd avoid jupyter for the time being, it could get cumbersome. Stick with the default IDLE editor (you open a new file and type away) until you feel confident, then make the switch toward some editor or IDE that you fancy - Visual Studio Code is popular and has python debugger, vim is old as Bible, can run on a calculator, but it's a modal editor, best used with touch typing, Notepad++ is also good for coding...

nvim for python, ipython

I am learning python and new to nvim.
Using iterm2 is there way to send lines of code to the second tab for run with opened ipython?
As far as I know there is a plugin vim-slime but I am not sure if it is the solution to my question. Thanks.
It looks like you can do this directly.
https://coderwall.com/p/k-in2g/vim-slime-iterm2
If you run into any problems you could always run screen||tmux in iterm2 and pipe to those. Python's indentation has a slight complication in passing to the REPL. However, the git repo for vim-slime has directions on what to add to your vimrc to make this work seemlessly.

Python Workflow like RStudio?

I use Python as my main programming language, sometimes switching to R for quick data-analysis tasks due to it’s huge library for statistical programming purposes (although pandas for python is great!). I guess that most R-Users go for R-Studio, which is like an IDE for R. There are probably hundreds of IDE’s for Python, but i’d like to stick with VIM and IPython. Although there is the vim-ipython-plugin, i can’t recreate the R-Studio workflow, which is mainly:
Write code in the source-editor
"push" or execute it within the console window via CTRL-Enter
after that, the results get displayed in the console window or the plotting window
When using the vim-ipython plugin, you can execute vim-lines via CTRL-S, but the results in the running IPython-Kernel/QTconsole are not updating. Instead, the output goes directly to a VIM window (<leader>s opens it, but the window closes all the time and isn´t able to display inline-graphics, like the ipython-qtconsole does).
I’m sure this isn’t a new "problem", but is there any setup for VIM and (I)Python, which mimics the RStudio behavior and integrates both the editor and the interactive interpreter like modern IDE’s do? Copying text from vim to the interactive interpreter can’t be the optimal solution, right?
The canonical way of doing this in vim is to run vim inside a gnu-screen or tmux session with the REPL in a second screen in the session. You can then use vim-slime to send code from vim to the REPL.

Command Prompt Python

What is the benefit of running code through the command prompt/terminal vs an ide?
I've noticed recently when using the progressbar module of python that the progress text is updated on the same line in the command prompt window while the ide prints each text on the next line. Why are these different? Are they not running though the same interpreter?
The IDE adds an extra layer of software between the program and the python interpreter.
What you are seeing is probably that the IDE's output window is not a complete terminal emulator, and doesn't understand or ignores the commands that the progressbar module uses. to keep the output on the same line.
Have a look at ipython. It is a very nice environment for testing and running python code.
Each IDE is infact interacting via the command line and redirecting streams into it's implementation of showing those outputs, Each IDE has it's own way of doing this, command prompt is more powerful if you are expeirienced and easy to try one off scripts, try ipython which is great for beginners and learners alike for fast access to the programming environment and trying out module.

Cleanest way to run/debug python programs in windows

Python for Windows by default comes with IDLE, which is the barest-bones IDE I've ever encountered. For editing files, I'll stick to emacs, thank you very much.
However, I want to run programs in some other shell than the crappy windows command prompt, which can't be widened to more than 80 characters.
IDLE lets me run programs in it if I open the file, then hit F5 (to go Run-> Run Module). I would rather like to just "run" the command, rather than going through the rigmarole of closing the emacs file, loading the IDLE file, etc. A scan of google and the IDLE docs doesn't seem to give much help about using IDLE's shell but not it's IDE.
Any advice from the stack overflow guys? Ideally I'd either like
advice on running programs using IDLE's shell
advice on other ways to run python programs in windows outside of IDLE or "cmd".
Thanks,
/YGA
For an interactive interpreter, nothing beats IPython. It's superb. It's also free and open source. On Windows, you'll want to install the readline library. Instructions for that are on the IPython installation documentation.
Winpdb is my Python debugger of choice. It's free, open source, and cross platform (using wxWidgets for the GUI). I wrote a tutorial on how to use Winpdb to help get people started on using graphical debuggers.
You can easily widen the Windows console by doing the following:
click the icon for the console window in the upper right
select Properties from the menu
click the Layout tab
change the Window Size > Width to 140
This can also be saved universally by changing the Defaults on the menu.
Year 2017-2022 Answer:
Try Visual Studio Code, it has great support for Python debugging, auto completion and more!
See this link for details:
https://code.visualstudio.com/docs/languages/python#_debugging
However, I want to run programs in
some other shell than the crappy
windows command prompt, which can't be
widened to more than 80 characters.
Click on the system box (top-left) in the command prompt and click properties. In the layout tab you can set the width and height of the window and the width and height of the screen buffer. I recommend setting the screen buffer height to 9999 so you can scroll back through a long output.
I use eclipse with pydev. Eclipse can be sluggish, but I've become attached to integrated svn/cvs, block indent/unindent and run as unittest features. (Also has F5 run)
If your comfortable in emacs though I don't see any reason to make such a major change.
I suggest instead that you replace your 'crappy command prompt' with powershell. It's not as crappy.
(As mentioned by Soviut and The Dark - you can increase the buffer width/window size to more than 80 by title-bar>right-click>Properties>Buffer Width/Window Size edit even in crappy cmd)
Wing IDE is awesome. They also have a free version.
If you ever graduate to vim, you can just run the following command to start the program you're currently editing in an interactive shell:
:!python -i my_script.py
I edit my Python programs in EditPlus. I've configured a user tool that allows me to run my program and have its output appear in a frame below my editor window. My configuration will even let you double click on an exception lines in the output to jump directly to the file and line where the error occurred. You can grab the user tool configuration I use from here on the EditPlus wiki.
Perhaps there is similar functionality you can configure that allows you to run your program and display its output in your Emacs editor!?
I replaced cmd with Cygwin and Poderosa. May be a little overkill though, if the only problem you have with cmd is that it's a pain to resize.
Although you use Emacs instead of Vim, so I guess you're into overkill... ;-)

Categories