the proper method for making a DB connection available across many python modules

前端 未结 3 700
甜味超标
甜味超标 2020-12-29 12:35

I want to make a single database object available across many python modules.

For a related example, I create globl.py:

DOCS_ROOT=\"c:\\docs         


        
3条回答
  •  被撕碎了的回忆
    2020-12-29 12:44

    You suspect wrongly. The code will only be executed once - subsequent imports just refer to the module via sys.modules, and don't re-run it.

    (Note that this is the case as long as you always use the same path to import the module - if you do from globl import cursor in one place, and from my.fullyqualified.project.global import cursor in another, you probably will find the code is re-executed.)

    Edit to add as S.Lott says in the comment, this is a perfectly good way to handle a global object.

提交回复
热议问题