###### tags: `Python`, `Debugging` # Python Common Errors <h3> Flow of Data </h3> <p> Remember, when debugging you want to consider the flow of data. Where is the data going and coming from? </p> <p> In the diagram below, we are seeing the "flow of data". When setting up our project/assignment. When the user submits a "GET" Request it is going to travel from the HTML (view) to our Routes located in our Controllers -> Models -> Database. Once our database has provides us the data we want to display it will send it back to our Models -> Controllers -> HTML </p> <img src="https://i.imgur.com/07XjWvq.jpg)" width="500px" /> ### The MVC <img src="https://i.imgur.com/tDuFZzR.png)" /> Check out these article from [Real Python](https://realpython.com/the-model-view-controller-mvc-paradigm-summarized-with-legos/) and [Geeks For Geeks](https://www.geeksforgeeks.org/mvc-framework-introduction/)for more information! --- **Tuple index out of range** [insert image here] Your *getone()* or *getbyid()* or *getbyemail()* method contains code like this: ``` 1. > xxx = cls(result[0]) 2. > … 3. > return xxx ``` Or this: ``` 1. > return cls(result[0])``` ***Query is SUCCESSFUL:*** If your SELECT query runs successfully, it returns a list containing one or more dictionaries. ***Query is FAILED:*** If your SELECT query fails, it returns “False”, and you would get an error that says bool’ object something something…. ***WHY*** If you didn’t get that error, your SELECT query runs correctly, but the query returned 0 results. In other words, result is equal to an empty list. result = [] So when you ask for result[0], you are using index value 0 to select the first item in an empty list. Because there is no first item in an empty list, you get an error that says tuple index out of range.
 One thing I can’t explain is why it doesn’t say list index out of range instead of tuple, but you are getting this error because your code is asking for index 0 of an empty list, which doesn’t exist. It’s probably because the id or email you are using in the WHERE clause is not found in the database. ***How to fix?*** GOES HERE --- **Bool value** [insert image here] **‘Bool’ value not iterable: is what you get from ** ``` for row in result when result = False ``` **‘Bool’ value not subscriptable is what you get from ** ``` result[0] when result = False. ``` Any error that starts with ‘bool’ object something something.... means your SQL query probably failed, instead of returning the list with one or more dictionaries in it [ {…}, {…} ], it returned False, which is a boolean value. Look in your terminal for the message something went wrong. On the same line you will find information about the error. Use that information to begin searching for the reason why your SQL query failed. It's almost always one of two things; the syntax of the query failed, or the data was wrong/incomplete. To check the query, copy-paste it from the terminal to Workbench and make sure that it works as written in Workbench. To check the data, use print() statements to view the data in the terminal. If the data is wrong, trace it back to where it came from, using print statements each step of the way. PS - if you have this line in the code for your .get() method or something like it: ``` if len(result) == 0: return False ``` That will may also show this error when the SQL query succeeds but returns 0 rows. If that doesn't help you find the answer, let me know. (edited) --- **Not Found: The requested URL was not found on the server. If you entered it manually, please check your spelling again ** <img src="https://i.imgur.com/pYfYGuo.png" /> If you received this error there are a few places to check: 1. Check your server.py file to see if you imported your controllers 2. Check your __init__.py is spellled correctly. Check for double underscore, spaces, periods instead commas, etc. 3. Check if your __init__.py has the code needed to run 4. Check the spelling of your routes in your controller file 5. Check the spelling of your controller names and that matches the spelling in your server.py file Here is an example of how to solve this error: <img src="https://i.imgur.com/k1T6RHt.png" /> Notice we are missing our imports that give our server the ability to pass on our routes to our browser **How to Fix?** We need to make sure **all** our controllers are imported into the server.py file <img src="https://i.imgur.com/zlc23Ii.png" /> Refresh the browser, and see if that fixed the problem --- **ERROR -> no attibute called "create_user()"** <img src="https://i.imgur.com/BVQDIN2.png" width="500px"/> **WHY?** If you are receiving this error it's because Python is not recognizing this method. **How to Fix?** 1. Check the spelling of this route in your models 2. Check the indentation of your methods --- **ERROR -> No module named 'pymysql' 'flask'** <img src="https://i.imgur.com/MciyJjp.png" width="500px"/> <img src="https://i.imgur.com/SwWShtx.png" width=500px/> Why? Your Flask app is not recognizing your dependencies Please, check 1. Is your virtual environment activated? 2. Did you install the dependencies 3. Check your pipfile <img src="https://i.imgur.com/uQXG4FB.png" /> If there is a missing package you will notice it here How to fix? Install dependencies like this: ``` pipenv install flask pipenv install pymysql pipenv install flask-bcrypt OR pipenv install flask pymysql flask-bcrypt ``` --- **KeyError** WHY? In your project, there is a "key" --- **OperationalError pymsql.err.OperationalError: (1049, unknown database)** <img src="https://i.imgur.com/JPmV6Nw.png" /> Why? Your Flask app is not recognizing your database How to Fix? 1. Check the spelling of your DB in your models and database. Please, be sure it's **exact** matches 2. Check to make sure you made a database <img src="https://i.imgur.com/niq6Xkj.png" /> Like so: <img src="https://i.imgur.com/LqxuS0i.png" /> --- **Template Not Found** Why? Flask is not recognizing your HTML templates <img src="https://i.imgur.com/Qc74903.png"/> How to fix? 1. Please, check the spelling of your templates in the render_template() function 2. Please, check the spelling of your templates in the templates folder 3. Please, be sure to check the spelling of **"templates"** it should be spelled exactly 4. Please, check your __init__.py and that it's spelled correctly 5. Please, check your folder structure --- --- --- ---