Python 3 Anaconda:Syntax Error on inexistent line#

  • Thread starter WWGD
  • Start date
  • #1
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322

Summary:

Jupyter keeps pointing out an error in a line that does not exist (it did exist but was deleted).

Main Question or Discussion Point

Ok, I wrote a small program P, initially of 16 lines in Jupyter Anaconda in Python 3.7
I deleted the last 4 lines, ending up with 12 lines in P2.
Now, I run P2 and I'm told there is a syntax error in (the inexistent) line 14.
I restarted the kernel, nothing. I opened another notebook, pasted into it, nothing, same message. Refreshed kernel, nothing.
??????
Actual message:
File "<ipython-input-1-c14a518e8a8c>", line 14
SyntaxError: 'break' outside loop
 

Answers and Replies

  • #2
33,079
4,782
Summary:: Jupyter keeps pointing out an error in a line that does not exist (it did exist but was deleted).

Ok, I wrote a small program P, initially of 16 lines in Jupyter Anaconda in Python 3.7
I deleted the last 4 lines, ending up with 12 lines in P2.
Now, I run P2 and I'm told there is a syntax error in (the inexistent) line 14.
I restarted the kernel, nothing. I opened another notebook, pasted into it, nothing, same message. Refreshed kernel, nothing.
??????
Actual message:
File "<ipython-input-1-c14a518e8a8c>", line 14
SyntaxError: 'break' outside loop
Are you certain that the file you deleted the lines from is actually the same file that is being processed? Just because you edited file P to get file P2 doesn't mean that your IDE is reading and processing P2.
 
  • #3
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
Are you certain that the file you deleted the lines from is actually the same file that is being processed? Just because you edited file P to get file P2 doesn't mean that your IDE is reading and processing P2.
Thanks, but I even opened up a new Jupyter notebook and pasted P2 into it. Would my memory still have P in cache? EDIT: Wouldn't restarting the kernel clear up the cache or do I need to restart my PC?
 
  • #4
33,079
4,782
Thanks, but I even opened up a new Jupyter notebook and pasted P2 into it. Would my memory still have P in cache?
It seems like it must have the old, unedited version in memory if you're getting an error for a line that doesn't exist any more. After you pasted the new version into the notebook, you did save the file, right?
EDIT: Wouldn't restarting the kernel clear up the cache or do I need to restart my PC?
I don't know what you mean by "restarting the kernel." You shouldn't need to do anything so drastic as restarting the computer.

For C++ and assembly stuff I use an IDE, Visual Studio, but for my small python programs I don't use anything fancy. Nothing more than Notepad or Notepad++, and I run python.exe from a command prompt.
 
  • #5
1,322
1,129
@WWGD If your program is an independent one (i.e. one that does not depend on any file except standard libraries), try running it on an online python compiler, like this one:
https://www.onlinegdb.com/online_python_compiler

This will separate out your program from the compiler on your PC, and any "transient" error can therefore be avoided.

Let us know what you get.
 
  • #6
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
I don't know what you mean by "restarting the kernel." You shouldn't need to do anything so drastic as restarting the computer.
Jupyter Notebook is an IDE from Anaconda, it has its own kernel, which you may restart:

1578792535314.png
 
  • #7
pbuk
Science Advisor
Gold Member
1,235
262
Jupyter Notebook is not really an IDE, it is according to its own web site "an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text". This has many implications, two of which that are relevant here (noting that you are in a Windows context) are:
  • The source "files" for a project are not individual files, they are stored in a single project file with the .ipynb extension. So when you want to save an edit, the Jupyter process that you have asked to save must be able to gain write permission to the project file which it can only do if no other process has a write lock.
  • You use a browser to interface with the Jupyter process; if you close the browser tab the Jupyter process is still running - or even worse, may subsequently crash, making it very difficult to detect that it still has a write lock on the open project file.
The easy way to fix this is to restart Windows, and if you experience weird things going on with Jupyter that should always be your first step.

Some may suggest that your next step should be to use a different tool set - I use Visual Studio Code as my primary coding tool for all languages, as an alternative for Python I would also recommend PyCharm (which I see now has a special edition for Anaconda which I haven't used). Both of these will work with Jupyter Notebooks if you still need to do that for collaborative reasons (again I haven't used this in PyCharm so can't comment, but VS Code works fine), but if you don't need to then it is much better IMHO to create your Python projects in a more traditional way with seperate source files in a single folder per project. This also means you can use a Version Control System (VCS) more effectively, which is a must for developing any software with more than a trivial level of complexity.
 
  • #8
1,322
1,129
as an alternative for Python I would also recommend PyCharm (which I see now has a special edition for Anaconda which I haven't used)
PyCharm is what I use, but not with Anaconda. I have Python installed by itself; I don't like the fact that while Python installation is perhaps a few hundred MB's, Anaconda is few GB's (at least I remember so). And for other languages, like Java, there is always our favourite NetBeans.
 
  • #9
942
464
You might try ctrl F5 (that clears the browser cache and then reloads the page).
 
  • #10
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
Thank you all, I am kind of tied down to the Anaconda/Jupyter format at this point, since I was told it is part of the reqs (at least for the interview) for a job I will be applying to , but I am looking into your recommendations.
 
  • #11
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
@WWGD If your program is an independent one (i.e. one that does not depend on any file except standard libraries), try running it on an online python compiler, like this one:
https://www.onlinegdb.com/online_python_compiler

This will separate out your program from the compiler on your PC, and any "transient" error can therefore be avoided.

Let us know what you get.
Thanks, Wichik, program ran:
1578871057983.png


May not be neither the most elegant nor shortest way but by now I am just glad it actually runs; will worry about other issues when I have this down cold.
 
  • #12
pbuk
Science Advisor
Gold Member
1,235
262
Python:
if password != 'Fish'
    ...
elif password == 'Fish'
    ...
Sorry, you haven't got the job :(
 
  • #13
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
How so? I tried the two options: entering an incorrect password 5 times, and it exited. Then entering the correct one, and it gave me access. At this point I am not seeking to optimize, just to have the program run, but I am open to comments, suggestions.
 
  • #14
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
Problem with using different IDEs or other means is that I had installed both 2.x and 3.x with Anaconda on my PC. Each has a front end and a back end, which is a server. When a request was made from either of the clients 2.x or 3.x , I would get an error message because it was not clear which server would serve the request. @pbuk had suggested some way of avoiding that but I have not yet gotten myself to implement it, so I'll wait until I do to try something else.
 
  • #15
Ibix
Science Advisor
Insights Author
5,718
4,193
Try
Python:
if password == "Fish":
  ...
else:
  ...
Your elseif branch achieves the same as an else and is a maintenance problem waiting to happen.

Not that I haven't seen your approach in production code, but it's Wrong.
 
  • #16
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
Try
Python:
if password == "Fish":
  ...
else:
  ...
Your elseif branch achieves the same as an else and is a maintenance problem waiting to happen.

Not that I haven't seen your approach in production code, but it's Wrong.
Wrong in what sense? It did what I designed it to do: After I enter an incorrect password 5 times, I get an error message and I am given access when I enter the correct one. In what way is it wrong? EDIT: I welcome input, I am a newbie after all, some 4 months' experience. I am just happy to have it run; it took me like 2 days to figure it out. But I will look into your comment; not sure what you mean it being a maintenance problem waiting to happen.
EDIT3: Thanks both for the input, will look it up.
 
  • #17
Ibix
Science Advisor
Insights Author
5,718
4,193
Because your elseif does the same as an else. Except
  • If someone tells you the password is now "swordfish" you have to change it in two places. It's easy to forget one and you've double the typo chance.
  • It's harder to understand - I have to wonder why you didn't use the simpler else. Did you mean the condition to be something else? Or did it used to be something else and then circumstances changed? Or did you just not realise your two conditions were logical negations of each other?
  • With complicated conditions, doing the negation correctly can be non-trivial
 
  • #18
pbuk
Science Advisor
Gold Member
1,235
262
How so? I tried the two options: entering an incorrect password 5 times, and it exited. Then entering the correct one, and it gave me access. At this point I am not seeking to optimize, just to have the program run, but I am open to comments, suggestions.
Almost every line of what is written below is different to what you have written, in each case for a reason. Come back with any specific questions.
Python:
PASSWORD = 'fish'
tries = 0
success = False

while (tries < 5 and not success):
    tries += 1
    success = input('Enter the password ') == PASSWORD

if not success:
    print ('Sorry, goodbye')
    exit(1)

print('Got it')
 
  • #19
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
Because your elseif does the same as an else. Except
  • If someone tells you the password is now "swordfish" you have to change it in two places. It's easy to forget one and you've double the typo chance.
  • It's harder to understand - I have to wonder why you didn't use the simpler else. Did you mean the condition to be something else? Or did it used to be something else and then circumstances changed? Or did you just not realise your two conditions were logical negations of each other?
  • With complicated conditions, doing the negation correctly can be non-trivial
I just used a kludge because I spent almost 2 days to make it work. For now, as a novice, I am just aiming to make it run, and then finesse it later on. Thanks for comments.
 
  • #20
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
Has anyone used Python within SQL Server Dev 2017?
 
  • #21
pbuk
Science Advisor
Gold Member
1,235
262
I just used a kludge because I spent almost 2 days to make it work. For now, as a novice, I am just aiming to make it run, and then finesse it later on. Thanks for comments.
It's not about finessing, its about doing things right instead of wrong. If you asked in the DIY forum about putting up shelves and suggested using a hammer to fix screws you would expect to be told that was wrong, it's no different here.

I don't have time to retype all your code, but just in the first few lines...
Code:
Password='fish'
We use ALL_CAPS for constants, and put spaces around operators so this should be
Code:
PASSWORD = 'fish'
Code:
r=0
What does 'r' mean? And again we use spaces so...
Code:
tries = 0
 
  • #22
33,079
4,782
Problem with using different IDEs or other means is that I had installed both 2.x and 3.x with Anaconda on my PC. Each has a front end and a back end, which is a server. When a request was made from either of the clients 2.x or 3.x , I would get an error message because it was not clear which server would serve the request.
For such simple programs as this one, just invoke python directly from the command prompt. That would have eliminated the problem you've spent about a week on
WWGD said:
Wrong in what sense?
Already mentioned, but bears repeating. You should never write code like this, in any language:
Python:
if x == 1:
     <do something>
elif x != 1:
    <do something else>
You should never have a condition and its negation when there are only two options. As already mentioned, if the first condition needs to be changed, it's easy to overlook the negated condition, and forget to change it as well.
I just used a kludge because I spent almost 2 days to make it work. For now, as a novice, I am just aiming to make it run, and then finesse it later on.
Sure, but you would be more productive if you simplified the system you're running. @Wrichik Basu's suggestion of running the code against a different interpreter is a good one. I would consider uninstalling V 2.x, and maybe even going with a different system entirely, and spending the time working with the python language rather than futzing around with a balky IDE.
 
  • #23
pbuk
Science Advisor
Gold Member
1,235
262
Has anyone used Python within SQL Server Dev 2017?
I'm not sure this is relevant - conventions like ALL_CAPS for constants and spacing around operators and coding habits like how to write a while loop properly are common across many languages (at least Python, javascript, C(++/#), Java, PHP...).
 
  • #24
WWGD
Science Advisor
Gold Member
2019 Award
5,124
2,322
I'm not sure this is relevant - conventions like ALL_CAPS for constants and spacing around operators and coding habits like how to write a while loop properly are common across many languages (at least Python, javascript, C(++/#), Java, PHP...).
Yes, sorry, it is OT, unrelated to my program. Just curious as to whether someone has tried it and believes it makes sense to learn to do the whole data scene within SQL Server instead of in other settings.
 
  • #25
pbuk
Science Advisor
Gold Member
1,235
262
Yes, sorry, it is OT, unrelated to my program. Just curious as to whether someone has tried it and believes it makes sense to learn to do the whole data scene within SQL Server instead of in other settings.
In the development phase? Absolutley none - at best this is premature optimization, at worst a dead end. In the implementation phase? Possibly, but if it is worthwhile doing then it is worthwhile spending $$$ (or in my case £££, or in a research environment hhh) on getting someone who lives and breathes this stuff to do it once you have proved the concept in Py or whatever.
 
Last edited:

Related Threads for: Python 3 Anaconda:Syntax Error on inexistent line#

  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
7
Views
765
  • Last Post
Replies
8
Views
3K
  • Last Post
Replies
3
Views
2K
Replies
6
Views
4K
  • Last Post
Replies
4
Views
2K
Replies
2
Views
9K
Replies
17
Views
1K
Top