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?
#1
I'm new to python and am trying to migrate my flask app to a package structure.

I have run.py sitting in a directory alongside a /myproject directory.:
from myproject import app

if __name__ == '__main__':
    app.run()
When I run python run.py, the debugger confirms that the import runs first on line 1 from myproject import app (as I would expect), and then again when app.run() is executed (as I would not expect). This is a problem because one of the imported modules declares a global object that is only allowed to be instantiated once..

/myproject contains the modules along with an __init__.py, which imports routes.py:

from flask import Flask
app = Flask(__name__)
from myproject import routes
I apologize if this should be obvious, but why is the import happening twice, and can it be prevented?
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
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 602 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  code keeps running if i use from threading import timer? birddseedd 3 3,501 Jan-25-2019, 05:00 AM
Last Post: stullis
  Running pytest gives " 'cannot import name 'session' " error jasonblais 2 4,411 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