简体   繁体   中英

Python Package Level Global Variable

I have following package structure. start.py is where user begin, and it will open a python interactive prompt. My goal is to make all objects created from different function calls in start.py to be available in namespace called vars which can be tab completed in python prompt. It works if I uncomment vars = global() in start.py but can't find the objects created in other modules

MyPackage
   __init__.py
   start.py
   tree.py

init .py

vars = globals()              # I thought declaring vars will make it avaible for all modules 

start.py

import rlcompleter
import readline
import code 

from tree import Tree
#vars = globals()                #Un-comment this line and vars can be recognized in start 
vars.update(locals())

class root:
    pass

root = root()

A = Tree()

readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")
code.InteractiveConsole(vars).interact(banner="",exitmsg="Good Bye!")

tree.py:

class Tree:
    B = 2
    #vars = globals()          #Uncommenting helps to get but I want vars shared between all module               
    vars.update(locals())

I had to add from __init__ import vars in both modules to resolve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM