0

I am getting 500 Internal Server Error when running flask with the following error message:

NameError: name 'self' is not defined

Yesterday my code worked fine and I didn't make any changes. The error message lists python files which were already imported in the distribution code. Maybe something changed in the background?

192.168.234.116 - - [10/Jul/2019 11:15:56] "GET / HTTP/1.0" 500 -
Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 303, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 291, in execute
    application_iter = app(environ, start_response)
  File "/home/ubuntu/environment/pset8/finance/application.py", line 13, in <module>
    app = Flask(__name__)
  File "/usr/local/lib/python3.7/site-packages/cs50/flask.py", line 54, in _after
    self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1)
NameError: name 'self' is not defined
Richard-Degenne
  • 2,649
  • 2
  • 21
  • 41
  • 2
    Please add code snippets with your questions along with stack-traces – Yayati Sule Jul 10 '19 at 11:29
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] to show us. – Some programmer dude Jul 10 '19 at 11:31
  • Even if this doesn't seem to be related to your code, you should post a *mcve* anyway: the minimum parts of *application.py* that when ran, the error becomes visible, otherwise you risk the question being closed for not following the guidelines (even if the question brings up a valid point). – CristiFati Jul 10 '19 at 12:24
  • @CristiFati thank you, I will use your advice. –  Jul 11 '19 at 08:01

2 Answers2

1

It's a CS50 bug (regression).

I've submitted [GitHub]: cs50/python-cs50 - Added the 1st (required) argument (self) to flask.Flask's initializer, which was closed (because it was incomplete - as I was too rushed when submitting it, and missed one spot), and [GitHub]: Fix missing self arguments in Flask __init__ replacement was created and merged. Not sure though when will it be available on the market (PyPI, so you can simply pip install it), but you could download the sources from GitHub and overwrite yours.

As an alternative, you could download the patch, and apply the changes locally. Check [SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer) (Patching utrunner section) for how to apply patches (basically, every line that starts with one "+" sign goes in, and every line that starts with one "-" sign goes out).
Or (given the fact that the change is more than trivial), you could:

  1. Open the flask.py file (in your case: "/usr/local/lib/python3.7/site-packages/cs50/flask.py") in a text editor
  2. Go to line #54 (in your case, in mine it's 52)
  3. Replace the current content (do not alter leading SPACEs):
    • For the current line (def _after(*args, **kwargs):) by: def _after(self, *args, **kwargs):
    • For the next one (_before(*args, **kwargs)) by: _before(self, *args, **kwargs)

@EDIT0:

As @kaczifant already experienced, the fix is already available for download: pip3 install cs50 --upgrade

CristiFati
  • 28,721
  • 9
  • 41
  • 63
0

The bug was fixed by the CS50 staff.

Select CS50 IDE > Log Out and then log back in at ide.cs50.io. Alternatively, you can run:

sudo pip3 install cs50 --upgrade