Python Forum
Puzzling import issue that I have no idea how to solvr
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Puzzling import issue that I have no idea how to solvr
#4
You cannot do this:
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
Reply


Messages In This Thread
RE: Puzzling import issue that I have no idea how to solvr - by deanhystad - Feb-21-2024, 05:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Puzzling error PythonNewbee 1 1,387 Dec-10-2021, 05:51 PM
Last Post: deanhystad
  import psycopg2 issue bhuvneshdogra 1 3,025 Dec-27-2018, 04:03 PM
Last Post: Larz60+
  crypto import issue saisankalpj 2 7,905 Dec-20-2018, 06:08 AM
Last Post: saisankalpj
  Win10 Import math issue Oldsquid 2 3,102 May-12-2018, 11:08 AM
Last Post: Oldsquid

Forum Jump:

User Panel Messages

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