Feb-21-2024, 05:07 PM
(This post was last modified: Feb-21-2024, 06:55 PM by deanhystad.)
You cannot do this:
file repository.py
from microservice.service.repository import clients, secrets, alerts clients = ...some code using the data...This code creates a variable named "clients" in the local module. If you want to modify clients in microservice.service.repositiory you need to do something like this.
import microservice.service.repository as repository repository.clients = ...some code using the data...This is a scope problem, not a path problem. Assignment creates local variables unless you force Python to make them elsewhere. In a function you can use "global" to tell python that a vairable inside a function is a module variable. There is no "global" for variables in other modules, so you need to use the module object when assigning the variable. You can see this more easily in my simplified example:
file repository.py
x = 5 def pprint(): print(x)file service.py
import repository from repository import x, pprint x = 42 pprint() repository.x = 42 pprint()
Output:5
42