Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77062

Re: Flask import problem with Python 3 and __main__.py

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'startup': 0.05; 'subject:Python': 0.06; 'importerror:': 0.07; 'puts': 0.07; 'python3': 0.07; 'rename': 0.07; 'restarting': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'run,': 0.09; 'windows,': 0.09; 'jan': 0.12; '__init__.py': 0.16; 'finds': 0.16; 'fine.': 0.16; 'path.': 0.16; 'prepended': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:import': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'app': 0.19; 'module': 0.19; 'seems': 0.21; 'import': 0.22; 'header:User-Agent:1': 0.23; 'subject:problem': 0.24; 'skip:" 40': 0.26; 'suggested': 0.26; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; "doesn't": 0.30; "skip:' 10": 0.31; 'url:127': 0.31; 'work:': 0.31; 'anyone': 0.31; 'file': 0.32; 'run': 0.32; 'running': 0.33; '(most': 0.33; 'url:non-standard http port': 0.33; 'subject:with': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'doing': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'received:71': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'more': 0.64; 'url:0': 0.67; 'containing': 0.69; 'received:fios.verizon.net': 0.84; 'eliminates': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Flask import problem with Python 3 and __main__.py
Date Tue, 26 Aug 2014 14:03:26 -0400
References <slrnlvpbto.otf.jon+usenet@frosty.unequivocal.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-71-175-90-87.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <slrnlvpbto.otf.jon+usenet@frosty.unequivocal.co.uk>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13474.1409076223.18130.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409076223 news.xs4all.nl 2968 [2001:888:2000:d::a6]:38364
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77062

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web