Output identifiers in python program - python

I am running a python script using cygwin Terminal in Windows 7. I am able to get the output in terminal window but I am unable to find out which identifiers or data elements of the program are responsible to display output on the terminal window. I have also used checkpoints like print statement in between the programs but haven't got anything useful out of it. Anyone has any idea how I shall understand the control flow of the program? Thnx

Based on the information you gave us, all I can tell you is get a good IDE and learn to use the debugger. I recommmend eclipse with the pydev plugin.

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...

Where is the output area in Atom?

Total beginner here. I have just installed Atom for use as an IDE for python along with some python packages. This may seem like a very basic question, but I am unable to find the output window.
For example, I type
print ("Hello World")
and I would expect the output to be
Hello World
which is the case for any other Python compiler i can find online. However, I see "Hello World" nowhere in Atom when I run it. The only thing closest to an output would be the space below that just shows the time it took to complete running. Does python not come with an in-built output window?
I have installed packages that seemed to be what I want such as script_runner and output_panel but all to no avail. Can someone point me to the right direction?
Script_runner does not work for me. I use the Script package. I go to the Packages menu item and click on the Script option in that menu. One of the options is to run the script. When I run a script this way a terminal/Console pane opens at bottom of the editor window with output written there. If you are not getting output your script may not be running.
Be sure your file name ends with .py.
For Atom, one of the easiest ways to run python is atom-python-run
All you need to do is press F5 or F6 in the python file

VScode run code selection

I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

PyDev: Running code to interactive console

As my problem is quite simple I'll try to make this question simple aswell. The problem I'm having concerns the PyDev interactive console. I can bring up the console just fine without problems, and even use it as an interactive shell, just as I would be able to with IDLE. However, when I try to run my code that I've written in my project module, it won't run to the interactive console, but to the Eclipse default console. The default console displays the program just fine, but since it's not an interactive shell, I can't do anything afterwards.
With that being said, my question is: How do I get my code to run to the PyDev interactive console, and not the Eclipse default one? Thanks in advance!
For making the symbols of the current editor available in the interactive console, use Ctrl+Alt+Enter (which will do a runfile in the interactive console context).
Also, make sure you read: http://www.pydev.org/manual_adv_interactive_console.html
First it is possible. Second, off top of my head it might be you need to change in preferences where the debug perspective points to? Look through pydev preferences too. Sorry not to be more helpful. Away from computer.
Run the program from the code window. Try hitting F9 whike cyrsor in your code window. Results and bugs should show up in console.

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

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.

Categories