<p dir="auto">Hi, I seem to be running into a Flask specific issue, here are the details:</p>
<p dir="auto">operating system:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:    14.04
Codename:   trusty"><pre class="notranslate"><code class="notranslate">$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:    14.04
Codename:   trusty
</code></pre></div>
<p dir="auto">structure of package:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ tree pantry
pantry
├── db.py
├── __init__.py
├── models
│   ├── food.py
│   └── __init__.py
└── run.py"><pre class="notranslate"><code class="notranslate">$ tree pantry
pantry
├── db.py
├── __init__.py
├── models
│   ├── food.py
│   └── __init__.py
└── run.py
</code></pre></div>
<p dir="auto">The <code class="notranslate">__init__.py</code> files are empty.</p>
<p dir="auto">error received:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ pwd
/home/sam

$ python3.4 -m pantry.run
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
Traceback (most recent call last):
  File &quot;/home/sam/pantry/run.py&quot;, line 2, in &lt;module&gt;
    from pantry.models.food import Food
ImportError: No module named 'pantry'"><pre class="notranslate"><code class="notranslate">$ pwd
/home/sam

$ python3.4 -m pantry.run
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
Traceback (most recent call last):
  File "/home/sam/pantry/run.py", line 2, in &lt;module&gt;
    from pantry.models.food import Food
ImportError: No module named 'pantry'
</code></pre></div>
<p dir="auto">db.py:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">flask</span>.<span class="pl-s1">ext</span>.<span class="pl-s1">sqlalchemy</span> <span class="pl-k">import</span> <span class="pl-v">SQLAlchemy</span>

<span class="pl-s1">db</span> <span class="pl-c1">=</span> <span class="pl-v">SQLAlchemy</span>()</pre></div>
<p dir="auto">food.py</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from pantry.db import db

class Food(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    amount = db.Column(db.Integer, unique=False)

    def __repr__(self):
        return '&lt;Food %r&gt;' % self.name"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">pantry</span>.<span class="pl-s1">db</span> <span class="pl-k">import</span> <span class="pl-s1">db</span>

<span class="pl-k">class</span> <span class="pl-v">Food</span>(<span class="pl-s1">db</span>.<span class="pl-v">Model</span>):

    <span class="pl-s1">id</span> <span class="pl-c1">=</span> <span class="pl-s1">db</span>.<span class="pl-v">Column</span>(<span class="pl-s1">db</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">name</span> <span class="pl-c1">=</span> <span class="pl-s1">db</span>.<span class="pl-v">Column</span>(<span class="pl-s1">db</span>.<span class="pl-v">String</span>(<span class="pl-c1">80</span>), <span class="pl-s1">unique</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">amount</span> <span class="pl-c1">=</span> <span class="pl-s1">db</span>.<span class="pl-v">Column</span>(<span class="pl-s1">db</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">unique</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)

    <span class="pl-k">def</span> <span class="pl-en">__repr__</span>(<span class="pl-s1">self</span>):
        <span class="pl-k">return</span> <span class="pl-s">'&lt;Food %r&gt;'</span> <span class="pl-c1">%</span> <span class="pl-s1">self</span>.<span class="pl-s1">name</span></pre></div>
<p dir="auto">run.py</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from flask import Flask, jsonify
from pantry.models.food import Food
from pantry.db import db
import json

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db.init_app(app)

@app.route('/food', methods=['GET'])
def list_food():
    items = []
    for food in Food.query.all():
        item = {
            &quot;name&quot;: food.name,
            &quot;amount&quot;: food.amount,
        }
        items.append(item)
    return jsonify(items)

if __name__ == '__main__':
    app.run(debug=True)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>, <span class="pl-s1">jsonify</span>
<span class="pl-k">from</span> <span class="pl-s1">pantry</span>.<span class="pl-s1">models</span>.<span class="pl-s1">food</span> <span class="pl-k">import</span> <span class="pl-v">Food</span>
<span class="pl-k">from</span> <span class="pl-s1">pantry</span>.<span class="pl-s1">db</span> <span class="pl-k">import</span> <span class="pl-s1">db</span>
<span class="pl-k">import</span> <span class="pl-s1">json</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">Flask</span>(<span class="pl-s1">__name__</span>)
<span class="pl-s1">app</span>.<span class="pl-s1">config</span>[<span class="pl-s">'SQLALCHEMY_DATABASE_URI'</span>] <span class="pl-c1">=</span> <span class="pl-s">'sqlite:////tmp/test.db'</span>
<span class="pl-s1">db</span>.<span class="pl-en">init_app</span>(<span class="pl-s1">app</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">'/food'</span>, <span class="pl-s1">methods</span><span class="pl-c1">=</span>[<span class="pl-s">'GET'</span>])</span>
<span class="pl-k">def</span> <span class="pl-en">list_food</span>():
    <span class="pl-s1">items</span> <span class="pl-c1">=</span> []
    <span class="pl-k">for</span> <span class="pl-s1">food</span> <span class="pl-c1">in</span> <span class="pl-v">Food</span>.<span class="pl-s1">query</span>.<span class="pl-en">all</span>():
        <span class="pl-s1">item</span> <span class="pl-c1">=</span> {
            <span class="pl-s">"name"</span>: <span class="pl-s1">food</span>.<span class="pl-s1">name</span>,
            <span class="pl-s">"amount"</span>: <span class="pl-s1">food</span>.<span class="pl-s1">amount</span>,
        }
        <span class="pl-s1">items</span>.<span class="pl-en">append</span>(<span class="pl-s1">item</span>)
    <span class="pl-k">return</span> <span class="pl-en">jsonify</span>(<span class="pl-s1">items</span>)

<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-s1">app</span>.<span class="pl-en">run</span>(<span class="pl-s1">debug</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<p dir="auto">See <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="22412877" data-permission-text="Title is private" data-url="https://github.com/Kozea/WeasyPrint/issues/133" data-hovercard-type="issue" data-hovercard-url="/Kozea/WeasyPrint/issues/133/hovercard" href="https://github.com/Kozea/WeasyPrint/issues/133">Kozea/WeasyPrint#133</a></p>
<p dir="auto">What’s going on is:</p>
<ul dir="auto">
<li><code class="notranslate">python -m weasyprint.navigator</code> is run</li>
<li><code class="notranslate">[sys.executable] + sys.argv</code> is <code class="notranslate">['…/python', '…/weasyprint/navigator.py']</code></li>
<li>The reloader spawns a subprocess that way</li>
<li>The child process does not have <code class="notranslate">-m</code>, so Python add’s the .py file’s parent directory to <code class="notranslate">sys.path</code></li>
<li>Werkzeug tries to import stdlib’s <code class="notranslate">html.entities</code></li>
<li>This incorrectly imports <code class="notranslate">weasyprint/html.py</code></li>
<li>Stuff breaks</li>
</ul>
<p dir="auto">Nobody here is doing anything obviously wrong. How do you suggest dealing with this situation? What do you think of having <code class="notranslate">…/weasyprint/navigator.py</code> (which is expected to be mostly used with <code class="notranslate">python -m</code>) remove its parent directory from <code class="notranslate">sys.path</code> if it’s there?</p>