Python 3 Anaconda:Syntax Error on inexistent line#

In summary: 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.
  • #1
WWGD
Science Advisor
Gold Member
7,006
10,453
TL;DR 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
 
Technology news on Phys.org
  • #2
WWGD said:
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.
 
  • Like
Likes WWGD
  • #3
Mark44 said:
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
WWGD said:
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?
WWGD said:
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.
 
  • Like
Likes WWGD and jim mcnamara
  • #5
@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.
 
  • Like
Likes WWGD and FactChecker
  • #6
Mark44 said:
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
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 separate 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.
 
  • Like
Likes WWGD
  • #8
pbuk said:
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
You might try ctrl F5 (that clears the browser cache and then reloads the page).
 
  • Like
Likes WWGD
  • #10
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
Wrichik Basu said:
@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
WWGD said:
Python:
if password != 'Fish'
    ...
elif password == 'Fish'
    ...
Sorry, you haven't got the job :(
 
Last edited by a moderator:
  • #13
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
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
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
Ibix said:
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
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 realize your two conditions were logical negations of each other?
  • With complicated conditions, doing the negation correctly can be non-trivial
 
  • Like
Likes WWGD
  • #18
WWGD said:
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')
 
  • Like
Likes WWGD
  • #19
Ibix said:
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 realize 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
Has anyone used Python within SQL Server Dev 2017?
 
  • #21
WWGD said:
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
 
  • Like
Likes WWGD
  • #22
WWGD said:
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.
WWGD said:
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
WWGD said:
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
pbuk said:
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
WWGD said:
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:
  • #26
WWGD said:
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.

Developers would shun this approach for the code maintenance issue it causes. You have the password text in two places.
 
  • Like
Likes WWGD
  • #27
WWGD said:
Thanks, Wichik, program ran:
Happy to know about that. So there should be a problem with the interpreter on your PC. If restarting doesn't work, find out where the IDE stores temporary files and obliterate those files. This often solves the problem.
WWGD said:
Problem with using different IDEs or other means is that I had installed both 2.x and 3.x with Anaconda on my PC.
Python 2.x has reached its end of life on January 1, 2020. You should forget 2.x and concentrate on 3.x only. Note that Python is not backwards compatible.

In addition, there are some points you should note:
  • Other members have already pointed out how you should be using if-elif-else. @Ibix has pointed out in post #17 what the better code is. But one thing that has not been pointed out is this: if you continue using elif instead of else, you will face serious trouble in recursion. Recursion is the process of a function calling itself repeatedly until a specified condition ("base case") is satisfied, after which the call ends. In recursion, you are needed to have a condition that will be executed when all other conditions are unsatisfied. Even though your program is logically correct to humans, the machine will want you to specify an else condition. Even if you are not using recursion now, it is a good habit to use else instead of replacing it by elif.

  • Do not put passwords into variables. That is highly unsafe. This approach might be okay for a beginner, but as time progresses, you will have to make your program safer. The better way is to store an encrypted form of the password into the variable. When the user enters the password, you encrypt tat using the same algorithm, and compare the encrypted forms rather than the unencrypted forms. Python has the cryptography package for this.
 
  • Like
Likes WWGD
  • #28
Wrichik Basu said:
if you continue using elif instead of else, you will face serious trouble in recursion. Recursion is the process of a function calling itself repeatedly until a specified condition ("base case") is satisfied, after which the call ends. In recursion, you are needed to have a condition that will be executed when all other conditions are unsatisfied. Even though your program is logically correct to humans, the machine will want you to specify an else condition. Even if you are not using recursion now, it is a good habit to use else instead of replacing it by elif.
I'm not sure what you are trying to say here - there is no need to have an else clause and I don't see what this has to do with recursion. Actually many people would prefer this
Python:
def factorial(n):
    if n > 1:
        return n * factorial(n - 1)
    return 1
to this
Python:
def factorial(n):
    if n > 1:
        return n * factorial(n - 1)
    else:
        return 1

Wrichik Basu said:
Do not put passwords into variables. That is highly unsafe. This approach might be okay for a beginner, but as time progresses, you will have to make your program safer. The better way is to store an encrypted form of the password into the variable. When the user enters the password, you encrypt tat using the same algorithm, and compare the encrypted forms rather than the unencrypted forms. Python has the cryptography package for this.
That's a good point, although I didn't know cryptography could do one-way hashing. I would use bcrypt or maybe scrypt.
 

What is Python 3 Anaconda?

Python 3 Anaconda is a distribution of the Python programming language that includes commonly used libraries and tools for data science and machine learning.

What is a syntax error?

A syntax error is an error in the structure of a program that prevents it from being properly executed. In Python, this can occur when code is written in an incorrect format or contains invalid characters.

Why am I getting a syntax error on an inexistent line?

This can occur when there is a mistake in the code on a previous line, causing the error to be detected on a line that does not actually exist. It is important to carefully review the code and check for any typos or incorrect syntax.

How do I fix a syntax error in Python?

To fix a syntax error in Python, you will need to identify the line where the error occurs and then make the necessary corrections to the code. This may involve checking for missing parentheses or quotation marks, or correcting indentation.

Can I prevent syntax errors in Python?

While it is not possible to completely prevent syntax errors, there are steps you can take to minimize their occurrence. These include writing clean and organized code, using an IDE with syntax highlighting and error detection, and regularly testing and debugging your code.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
7K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
10K
Back
Top