Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77048 > unrolled thread
| Started by | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| First post | 2014-08-26 16:03 +0000 |
| Last post | 2014-08-26 18:31 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Flask import problem with Python 3 and __main__.py Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2014-08-26 16:03 +0000
Re: Flask import problem with Python 3 and __main__.py Terry Reedy <tjreedy@udel.edu> - 2014-08-26 14:03 -0400
Re: Flask import problem with Python 3 and __main__.py Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2014-08-26 18:31 +0000
| From | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| Date | 2014-08-26 16:03 +0000 |
| Subject | Flask import problem with Python 3 and __main__.py |
| Message-ID | <slrnlvpbto.otf.jon+usenet@frosty.unequivocal.co.uk> |
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__)
Running this with 'python3 runflaskapp.py' works fine. 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'. Unfortunately this doesn't
work:
$ python3 -m flaskapp
* Running on http://127.0.0.1:5000/
* Restarting with reloader
Traceback (most recent call last):
File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
from flaskapp import app
ImportError: No module named 'flaskapp'
Does anyone know why and how to fix it?
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-08-26 14:03 -0400 |
| Message-ID | <mailman.13474.1409076223.18130.python-list@python.org> |
| In reply to | #77048 |
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) > 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. > 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? > Unfortunately this doesn't work: Because x does not get added to sys.path. > $ python3 -m flaskapp > * Running on http://127.0.0.1:5000/ > * Restarting with reloader > Traceback (most recent call last): > File "/home/username/src/flaskapp/__main__.py", line 1, in <module> > from flaskapp import app > ImportError: No module named 'flaskapp' > > Does anyone know why and how to fix it? Since flaskapp/__main__.py is found and run, make the change suggested above that eliminates the flaskapp import. Or put flaskapp in site_packages, which is on the import search path . Pip, and I presume other installers, typically puts startup scripts in a directory that is on the system path. For Windows, this is pythonxy/Scripts. But this is more than I would do for most local apps. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| Date | 2014-08-26 18:31 +0000 |
| Message-ID | <slrnlvpkkn.otf.jon+usenet@frosty.unequivocal.co.uk> |
| In reply to | #77062 |
On 2014-08-26, Terry Reedy <tjreedy@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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web