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

  • Home
  • SEARCH
  • 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 6171441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:12:58+00:00 2026-05-23T23:12:58+00:00

I am breaking up a large monolothic python file into six separate submodules. Originally

  • 0

I am breaking up a large monolothic python file into six separate submodules.

Originally in onebigfile.py, I had

conn = MySqldb.connect()
c = conn.cursor()

and then a function would use it as so:

def getFromDB():
     c.execute(sql)

Now, I have restructured my module to

NewModule/
  __init__.py
  users.py
  accounts.py
  sixmoreofthese.py

What I’m puzzling over is what to do with semi-scarce resources like my MySQL connection.

Is there a way to access a parent namespace, e.g. conn. and c. could be put in __init__.py ? I don’t want to instantiate a whole bunch of connections to MySQL. Just dumping them there and calling them as if they’re part of the global namespace doesn’t work.. That is:

__init__.py:
   conn = MySqldb.connect()
   c = conn.cursor() 

> import NewModule
> NewModule.users.login('a','b')
--- login function calls the Mysql c. from the global namespace and can't find it. 

To anticipate one suggestion: it makes sense to split these files — there’s roughly 50-75k worth of python, and a group of people that need to work with the code, plus there are pretty clear conceptual groupings of functionality.

  • 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-23T23:12:59+00:00Added an answer on May 23, 2026 at 11:12 pm

    You can certainly share the connection object between python modules, and your idea to have the connection be a module-scoped variable will accomplish this nicely (with a few exceptions, Python modules are singletons, so even if they are imported multiple times from different files, they are only loaded once). You probably do not want to share a single cursor object between modules, especially if you are using a multi-threaded environment (e.g. running a web application in mod_wsgi or similar).

    One suggestion might be to move the connection into a database-specific module, like db.py, and initialize/access it with a get_connection() method. This will allow your application to bootstrap and load any configuration it needs to connect (username, password, hostname) gracefully. This might be as simple as:

    # db.py
    connection = None
    def get_connection():
        global connection
        if not connection:
            connection = MySqldb.connect() # possibly with configuration vars passed in
        return connection
    

    Your other code which uses the databse could look like:

    # other_module.py
    import db
    curs = db.get_connection().cursor()
    # do stuff
    curs.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginning to break apart a large JSP file into a some smaller
In the book Clean Code the author recommends breaking large methods into small functions
I've been breaking apart a large VS C# project into smaller projects, and while
In this question, I asked about breaking a dataset into subsets, and got a
I would like to know the best method of breaking up large Java Swing
My company is going to be breaking ground on a (large!) ASP.NET application in
I want to insert a large chunk of html into a pre-existing <td> .
What I mean is if I am breaking a programming project estimate down into
Hi there I'm reading a string and breaking each word and sorting it into
I have a large amount of python code that tries to handle numbers with

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.