Python Forum
Why is this import running twice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this import running twice?
#3
Hi Dean, thanks for your reply!

Here's what routes.py looks like:
import os, subprocess
from flask import render_template, request, redirect, url_for, jsonify
from myproject import app, db
from myproject.models import Session, Utterance
from myproject.mymodule import myfunction  #this module contains a global that can only be instantiated once
Note that my previous post abbreviated __init__.py for clarity. The entire script also instantiates a db as so:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
# configure the SQLite database, relative to the app instance folder
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

from myproject import routes
I was thinking that since __init__.py defines app before it import routes, it would at that point be safe to import app from inside routes.py (without it becoming circular).

ie, the interpreter sees app in __initi__.py, then import routes. Inside routes.py, when it sees import app, it says, 'ok, I already know about app' and all is well...No?
Reply


Messages In This Thread
Why is this import running twice? - by extan1 - Feb-13-2023, 02:14 PM
RE: Why is this import running twice? - by extan1 - Feb-13-2023, 05:45 PM
RE: Why is this import running twice? - by extan1 - Feb-13-2023, 09:07 PM
RE: Why is this import running twice? - by extan1 - Feb-13-2023, 10:46 PM
RE: Why is this import running twice? - by extan1 - Feb-13-2023, 10:49 PM
RE: Why is this import running twice? - by extan1 - Feb-14-2023, 09:31 PM
RE: Why is this import running twice? - by extan1 - Feb-17-2023, 02:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  code keeps running if i use from threading import timer? birddseedd 3 2,689 Jan-25-2019, 05:00 AM
Last Post: stullis
  Running pytest gives " 'cannot import name 'session' " error jasonblais 2 3,693 Oct-10-2018, 05:02 PM
Last Post: jasonblais

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020