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 6330031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:47:24+00:00 2026-05-24T17:47:24+00:00

My application has a structure similar to this one: myapp.py basemod.py [pkg1] __init__.py mod1.py

  • 0

My application has a structure similar to this one:

myapp.py
basemod.py
[pkg1]
    __init__.py
    mod1.py
[pkg2]
    __init__.py
    mod2.py

myapp.py:

import pkg1
import pkg2

if __name__ == '__main__':
    pkg1.main()
    pkg2.main()

basemod.py:

import pkg1


def get_msg():
    return pkg1.msg

pkg1/__init__.py:

import mod1

msg = None


def main():
    global msg
    mod1.set_bar()
    msg = mod1.bar

pkg1/mod1.py:

bar = None


def set_bar():
    global bar
    bar = 'Hello World'

pkg2/__init__.py:

import mod2


def main():
    mod2.print_foo()

pkg2/mod2.py:

import basemod

foo = basemod.get_msg()


def print_foo():
    print(foo)

If I run myapp.py I get:

None

While in my mind I’d expect:

Hello World

My goal is to keep the two packages completely independent from each other, and only communicating through basemod.py, which is a sort of API to pkg1.
I’m starting to think that I have not completely understood how imports among packages work, what am I doing wrong?

Thank you!

  • 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-24T17:47:24+00:00Added an answer on May 24, 2026 at 5:47 pm

    Took me a while to read through all that code, but it looks like your problem is in pkg2/mod2.py. The line foo = basemod.get_msg() is executed the first time that file is imported, and never again. So by the time you change the value of mod1.bar, this has already executed, and foo is None.

    The solution should simply be to move that line into the print_foo function, so it is only executed when that function is called – which is after the code that sets the relevant value.

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

Sidebar

Related Questions

My Java application has got a package structure similar to this: src/com/name/app src/com/name/app/do src/com/name/utils/db
I have a project which has a calling structure similar to this: main project/application
I have a Struts based web application that has a structure similar to the
Hi I have big xml file which has a structure similar to the one
My application has an MVC structure. Is it sufficient to only cache the model
Application has an auxiliary thread. This thread is not meant to run all the
Our application has a file format similar to the OpenDocument file format (see http://en.wikipedia.org/wiki/OpenDocument
My application needs a data structure similar to a queue: put data in it,
What is the common pattern to structure web application where part of it has
my application has the following structure: public class Transaction { public int TransactionID {

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.