Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:14:56+00:00 2026-05-29T05:14:56+00:00

I created a package in python. In the init file I placed a variable

  • 0

I created a package in python. In the init file I placed a variable that must be available in all the modules of this package.

Example:

# main script    
from _Framework.STB import DB
DB.environment = 'lab'
channel = DB.Channels.ChannelStatic.getChannelByNumber(416)
...

# Channels.py module in the package
from _Framework.DB.__init__ import cursor
from _Framework.DB.__init__ import environment
from time import *
...

The problem is that everdy call to

from _Framework.DB.__init__ import xy

overwrites my “global” variables

How can I solve?

Thanks

EDIT:

init.py:

all = [ 'Events', 'Channels', 'Genres', 'Subgenres','EPGSections'] 
try: 
    conn = MySQLdb.connect(host,user,passwd,db) 
    cursor = conn.cursor(MySQLdb.cursors.DictCursor) 
except: 
    cursor = None 
    environment = 'live'

I import the DB package and I set the “environment” variable, but when the Channels module is imported, I think, it makes a new call to init.py and reexecutes its code so “environment” is overwritten. I’m looking for a clean way to share a variable between modules of the same package

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:14:57+00:00Added an answer on May 29, 2026 at 5:14 am

    I’m still not sure but if I understand correctly. YOu should just use the edit on your question and post correctly formatted code:

    __init__.py:
    
    all = [ 'Events', 'Channels', 'Genres', 'Subgenres','EPGSections']
    try:
        conn = MySQLdb.connect(host,user,passwd,db)
        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    except:
        cursor = None
        environment = 'live
    

    Now in Channels.py you want to change _Framework.DB.__init__.cursor and _Framework.DB.__init__.environment and this changes should be seen all around your program?

    If this is what you want you should know that when you do in a import:

     from _Framework.DB.__init__ import cursor
     from _Framework.DB.__init__ import environment
    

    What happens is that you get two variables cursor and environment in your Channels.py module namespace and any change you do will only be visible in that module. If you want to achieve your result try this:

    import _Framework.DB
    
    _Framework.DB.cursor = new_cursor
    _Framework.DB.environment = new_environment
    

    This way you actually change the module variables. Now in the rest of your program, when you want to see the changes took effect you could either:

    1. Acces the varibles in the same way you changed them:

      import _Framework.DB

      _Framework.DB.cursor        #changes made in other module will have effect
      _Framework.DB.environment   #changes made in other module will have effect
      
    2. import like you did, BUT make sure the changes you want are done BEFORE you do any imports.

      As an negative example for the second case:

      #module Vars
      a = 1
      
      --------------------------
      
      #module Do_change
      import Vars
      Vars.a = 2
      
      --------------------------
      
      #module A
      from Vars import a
      
      
      --------------------------
      
      #module B
      from Vars import a
      
      --------------------------
      
      #some random module
      import A
      import Do_change
      import B
      

    Now if you are in this order of imports when you import A it will create a variable a in A’s namespace with the value of Vars.a. The Do_change will edit the variable accordingly, but A will have no knowledge of this. B however who is imported after will see the correct Vars.a = 2.

    That’s why I really reccomend the first option as it is the correct approach in my opinion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a Python script that uses a package that I've created. I need
I've created a package that contains a stored procedure that I plan to invoke
I've created an SSIS package that needs to execute a MySQL SPROC with an
python manage.py dumpdata modelName > file.json . created an empty database for a user
I have several Python projects and they all have a conf package: /some_folder/project_1/ conf/
This is a follow-up question to import python modules with the same name ,
I created a DTS package. Someone else wants to be able to save it
I have created an SSIS package and it works great on my dev machine.
I have created an installation package using Wix which installs a Windows service on
I am very new to SSIS, looking at a package already created by someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.