On 2014-08-26, Terry Reedy <tjre...@udel.edu> wrote: > On 8/26/2014 12:03 PM, Jon Ribbens wrote: >> Flask suggests the following file layout: >> >> runflaskapp.py >> flaskapp/ >> __init__.py >> >> runflaskapp.py contains: >> >> from flaskapp import app >> app.run(debug=True) >> >> flaskapp/__init__.py contains: >> >> from flask import Flask >> app = Flask(__name__) > > Unless there is something else in flaskapp, this seems senseless. Why > not runflaskapp.py: > > from flask import Flask > app = Flask(__name__) > app.run(debug=True)
Because that's not how Flask apps work. I am showing a minimal test case, obviously for any real app not only would __init__.py contain more code, but there would be other files inside flaskapp/ too. Then when deployed, 'runflaskapp.py' would either be changed or go away entirely and the web server would just be pointed at 'flaskapp'. >> Running this with 'python3 runflaskapp.py' works fine. > > You are either giving this in directory 'x' containing runflaskapp.py or > given a longer pathname. In either case, directory 'x' get prepended to > sys.path, so that 'import flaskapp' finds flaskapp in x. Well, as I understand it actually the empty string is in sys.path, which is taken by Python to mean 'the current directory'. >> However it seems to me that a more Python3onic way of doing this >> would be to rename 'runflaskapp.py' as 'flaskapp/__main__.py' >> and then run the whole thing as 'python3 -m flaskapp'. > > In what directory? In the same directory as above, i.e. the one containing 'flaskapp'. It clearly does find 'flaskapp' initially, otherwise I would get a different error message "/usr/bin/python3: No module named flaskapp". > > Unfortunately this doesn't work: > > Because x does not get added to sys.path. No, but the current directory does (effectively). > Or put flaskapp in site_packages, which is on the import search path . That's no use for development though. The important part of my question is "why is running __main__.py from inside flaskapp/ somehow different to running runflaskapp.py from the parent directory?" It's probably a fairly Flask-specific question. -- https://mail.python.org/mailman/listinfo/python-list