In Python, importing twice with class instantiation?
Question: In `models.py` I have:

    
    
    ...
    db = SQLAlchemy(app)
    
    class User(db.Document):
    ...
    

In my app both `serve.py` and `models.py` call:

    
    
    from models import User 
    

Is that double import going to instantiate the db twice and potentially cause
a problem?


Answer: > Is that double import going to instantiate the db twice and potentially
> cause a problem?

No it will not. Once a module is imported it remains available regardless of
any further imports via the `import` statement.

The module is stored in `sys.modules` once imported.

If you want to **reload the module** you have to use `reload(module)`.

**Example:** `bar.py`

    
    
    xs = [1, 2, 3]
    

**Importing twice:**

    
    
    >>> from bar import xs
    >>> id(xs)
    140211778767256
    >>> import bar
    >>> id(bar.xs)
    140211778767256
    

Notice that the **identities** are identical?

**Caching:**

    
    
    >>> import sys
    >>> sys.modules["bar"] is bar
    True
    

