This is a dumb question but I'm new to Python and couldn't figure out why my codes only works with Jupyter notebook and not in other IDEs or Text Editors. For example, I realized I always have to use the "Print" function to get my output in PyCharm and Sublime Text3 while I can just run values without the print function in Jupyter Notebook.
Here's a simple example:
In Jupyter Notebook, I can simply run the codes without the print function
x = 10
y = 20
x+y
if i run this, i still get my output, which is 30.
But if I do the same thing in PyCharm or Sublime Text3, I don't get the output. It just says [Finished in X.Xs] without printing my output and I always have to use the print function to get the output.
x=10
y=20
print(x+y)
I wonder what causes the difference. At first, I thought it was because of the type of software I was using and realized both Pycharm and Jupyter Notebook are IDEs. Do I need to change settings in order to make my codes work in Pycharm or Sublime Text3?
Thank you.
Jupyter Settings is like that. It evaluates your last line of code, if it is 'None' prints nothing but if not None, would just get printed. Only the last uncommented expression or line gets printed nothing intermediate.
Python essentially operates in two modes - script execution mode and interactive mode, otherwise known as REPL (Read, Evaluate, Print, Loop). Jupyter is set up by default to run in REPL mode, where the results of operations are printed to stdout (see this question to change this behavior).
In contrast, PyCharm and Sublime Text execute scripts straight through in non-interactive mode, and only print to stdout when explicitly directed to do so, such as with a call to print(). PyCharm has a REPL mode, as far as I know, and in Sublime you can use the SublimeREPL plugin, available from Package Control.
Related
I have installed spyder v3.0.2 as part of Anaconda (in Windows 7), and have started working through the tutorial.
However, the python console does not seem to be responding correctly to input, either directly in the console or from a script via runfile.
There seem to be two things going on:
(1) The prompt appears too quickly so that it cuts into output
(2) Each time my script (which defines a hello function including one call to print, and calls it) is run, I do not see the expected output until I press Enter. From then on the console seems to need me to press Enter another time to produce the normal response you would expect from Enter. This leads to unexpected behaviour, particularly around the regular (>>>) and continuation (...) prompts. It increments each time the script is re-run, so that after running three times you need to press Enter an additional three times. For example:
Am I missing something obvious? My guess would be that it has something to do with line ending encodings but I can't figure out how to stop it. Also to my mind that still wouldn't explain issue (1).
Note that the IPython console seems to work without these issues (which of course I can use but I don't like the fact that there is clearly something amiss more generally...). Also, I tried installing the Mac version on my MacBook and don't have the same problem.
When reading a book or just coding on terminal/IDLE it's common to make typo, forgot brace or comma etc. After I got error and all what I wrote before is lost.
Then I have to write down code again..
Is there any way/option to return back all what write before and just edit mistake and continue to code?
In Idle (at least my version, Python 2.7.10 on windows), you can simply copy paste your code. In the python interpreter, you can't afaik, however you can use the up/down arrow keys to recall lines you previously "submitted" (i.e. typed and pressed enter).
If I understood correctly, IDLE is a GUI (graphical user interface - a visual representation of a program rather just through text) made to have a bit more features for programming in Python. You can use IDLE interactively, like in Terminal (a.k.a command line), or use it to write your script rather than in a separate text editor. Then once you save your script/program you can do neat things like run it directly from IDLE. There's nothing more special about the Terminal, you just have to do some more work.
Furthermore, all the code you have written on your GUI is on the cache memory which is used in system to store information recently accessed by a processor. So, I suggest you write again your code you can't recover them without saving.
To avoid these kind of problems use Git!
Git is a version control system that is used for software development and other version control tasks.
IDLE's Shell window is statement rather that line oriented. One can edit any line of a statement before submitting it for execution. After executing, one may recall any statement by either a) placing the cursor anywhere on the statement and hitting Enter, or b) using the history-next and history-prev actions. On Windows, these are bound, by default, to Alt-p and Alt-p. To check on your installation, Select Options => IDLE preferences on the menu. In the dialog, select the Keys tab. Under Custom Key Bindings, find the 'histor-xyz' actions in the alphabetical list.
For short, one-off scripts, I have a scratch file called tem.py. Since I use it often, it is usually accessible via File => Recent files.
I am entirely new to this (this website as well as programming), so this is probably horribly worded. I should also note that I am using python on an RPi2 so I can't paste anything. The problem is that python ignores the assignment operator when I type it into the editor, but understands it when typed into the shell. For example, if I type
x = 5
x
into the shell, the shell will respond with
5
Whereas if I type the same thing into the editor window and run the module, the shell responds with the nothing, just the restart bar and then the three arrows.
==========RESTART==========
.>>>
I can't find any information on this and I never encountered this problem using python on my desktop.
When you just type x in your program, you basically do nothing. If you want to print it, use print x to explicitly tell python to print it. Note that having a plain x in your module would be a valid python statement (though if you were using pyflakes or pylint or other some such tool, it would cry out loud that you are not doing anything in that statement).
In the shell, this typing x works simply because it is the shell which supports this, it is the shell's feature.
I'm using python.el
If I choose 'debugger' from the menu, and enter 'python -m pdb myfile.py', gud starts, and in a split frame I see the (Pdb) prompt in one, and my python code in the other with a caret on the first line, indicating that it's ready to go. For example 'n' steps to the next line and the caret moves accordingly.
If instead I enter 'python -m ipdb myfile.py', the frame splits, and one split is labeled gud, but there's no ipdb console evident. In other words, this way of starting ipdb doesn't seem to work. Ipdb works just fine if I manually insert a breakpoint into my python code using ipdb.set_trace(), except that it does not use the gud interface. Is this intentional so that ipdb's stack trace will work nicely?
If so, that's fine, but is there a way to start ipdb from emacs without manually adding a set_trace() command?
The basic problem here is that gud is looking for a (Pdb) prompt and ipdb doesn't prompt this way. There are three ways to fix this: fix ipdb to give a (Pdb) prompt, fix gud not to need to look for (Pdb) or (my favorite) use something else either on the gud side or on the ipdb side.
The problem with fixing up gud is that it is rather old and to my mind a bit creaky using global variables and not making use of Emacs Lisp data structures available other than lists and cons cells. A total rewrite of gud is called realgud, it is currently in MELPA and ELPA. And ipdb is supported.
The last option is to use something else, so let me suggest the Python trepan debugger which is already integrated into realgud (but not gud since I consider that a dead end). Although the backtraces it gives are not exactly like ipdb's, it does colorize them and the source code.
And recent versions of trepan3k backtraces will even show, on demand, you where in the line you are. So if you had say two calls of a function, like fib() it would distinguish which of the calls function was the one in progress.
When I am working with a Python Interpreter, I always find it a pain to try and copy code from it because it inserts all of these >>> and ...
Is there a Python interpreter that will let me copy code, without having to deal with this? Or alternatively, is there a way to clean the output.
Additionally, sometimes I would like to paste code in, but the code is indented. Is there any console that can automatically indent it instead of throwing an error?
Related
Why can I not paste the output of Pythons REPL without manual-editing?
IPython lets you show, save and edit your command history, for example to show the first three commands of your session without line numbers you'd type %hist -n 1 4.
WingIDE from Wingware will let you evaluate any chunk of code in a separate interpreter window.
IPython will let you paste Python code with leading indents without giving you an IndentationError. You can also change your prompts to remove >>> and ... if you wish.
I have a vim macro to "paste while cleaning interpreter prompts and sample output [[==stuff NOT preceded by prompts" and I'll be happy to share it if vim is what you're using. Any editor or IDE worth that name will of course be similarly easy to program for such purposes!
Decent text editors such as Notepad++ can make global search and replace operations that can replace >>> with nothing.