Python syntax switching between 2 and 3 - python

I've recently started having a weird bug with my python scripts. It seems like it's syntax switches between python 2 and 3 randomly when running the script. For instance, I'll write a script using the input() command, and it'll work fine for a couple hours, then start giving me an error even if I don't change a single thing in the code, and want me to switch input() to raw_input() in order for it to work again.
And the same will happen in reverse. I'll write a code with raw_input() since it doesn't like input(), and a few hours later it'll make me switch it. It does this with all syntax, not just the inputs. The other main one is the print command. It'll switch between wanting parenthesis around the print statement, or not.
But when I run python --version, it always returns only Python 3.5.1. Anyone have a fix for this? It's getting annoying having to change my scripts every few times I run them.

Related

Using IDLE to display script output like in Shell

I hope this makes sense. When using the IDLE python shell and typing the commands one by one, there is an output or response to most lines of code typed.
When writing a script and then running that script in IDLE I don't get to see the same output in the shell, is there a way of enabling it, or a line of code to add to my script so it displays?
Thanks
A feature of both standard interactive mode and the IDLE Shell (and presumably of other Python IDEs) is that the value of expressions entered in response to the >>> or other interactive prompt is echoed on a line below.
When running a script, the value of an expression statement is not printed. This is because such expressions are often included in files for their side effects, and because output may not be wanted. One must explicitly print() or sys.stdout.write() to see the result . So
>>> 2+3
5
must be written, for instance, as print(2+3) in a program executed all at once, whether from any editor or from a command line. (In other words, this question is not really IDLE specific, although it is a common one from beginners, who often use IDLE.)
If this is not your problem, we need much more information to help.

Python Console lost '>>>' before colon in PyCharm

Newbie here. I noticed that the >>> that usually precedes the colon in the Python Console has been replaced by In[#], in which # increments with each command.
I can still run code, but would like to know why this changed, whether it matters, what they mean, etc.

Constantly getting '...:' instead of 'Out [#]:' in Python 3.7 (Spyder)

About 95% of the time I input something into my console (function of something, defined object, etc.) I don't get an output, instead, I get 3 dots, with a colon, as if it wants me to input something.
Often times I can just spam the variable into the console, and eventually instead of '...:', I'll get 'Out [###]:(thing I called)', but this is absolutely ridiculous.
I've never had this problem in 2.7. I've always used Spyder as my IDE.
I've done my research on here, asked around, but cannot solve this.
Thanks in advance :)
Screenshot of my problem
Another shot
I figured out the solution a couple months ago, I figured I would post it here for those who may be experiencing the same issue. Simply press Shift+Enter instead of just Enter, when inputting into Console. Stupidly simple solution. Thank you all again.
The python interpreter is expecting additional lines of code. Either you typed the wrong command or you made a mistake in what you typed. This will happen, for example, if you type part of a command and hit return. The interpreter is expecting the rest of the command.

How to execute my code block by block?

I am new to Python, have some experience in MatLab and r. My question is: Is it possible to run part of the code in .py block by block (line by line)?
In r or Matlab, I can first have some data and variables loaded in the memory first. Then experimentally I can run a line or two to try out the syntax... this is particularly useful for new learners I believe. I know there is something called the iPython which can execute Python code line by line however this is not what I am after. Thanks.
Since ipython has already been discounted, I'm not sure this answer will be better. But I will tell you the two things that I do.
I drop into the debugger at the point where I want to "try out" something, so the code will run up to that point, and then drop me into the debugger. You do this simply by inserting this code at that point:
import pdb; pdb.set_trace()
Once you've done what needs to be done, you can either press q to quit, or c to continue running the process.
I use the -i option to python. This enters interactive mode at the end of your python code. This is useful if you want to set up a bunch of data structures, and try out some code on it, instead of typing all of it into a python shell first. (that might be why you rejected ipython?)
I think what you need is a debugger.
You can use the pydev plugin for Eclipse which has a debugger.
Another option is pdb as already suggested but it's not very easy to use.

Can I stop the REPL locking up when using Emacs and Python?

If I'm using a python interpreter under EMACS, then I've got various ways of evaluating code, for instance just typing it in at the REPL (Inferior Python) If that code produces too much output, then EMACS freezes solid and has to be killed.
This will do it, for instance.
[[[False] * 200 for i in range(3)] for j in range(200)]
There's no problem at all if python's running in a terminal. It just prints out False 120000 times and gives me the prompt back.
Is there any way to either limit the amount of output the inferior python process produces, or to get EMACS not to explode when faced with large outputs?
In clojure, for example, I can use *print-length* and *print-level* to solve this exact same problem.
Repeated calculations by comint-mode are considered the cause.
As for python-mode.el checked in a solution WRT
https://bugs.launchpad.net/python-mode/+bug/1253907
which makes it work from inside a print in source-buffer, not from shell.
It comes with a couple of new commands prefixed "py-fast-".
Alternatively new option py-fast-process-p must be `t'.
Maybe give it a try:
https://launchpad.net/python-mode
However, with python-mode.el and M-x IPython RET, it doesn't occur, runs nearly as fast as in console.
BTW ipython.el should not be needed.

Categories