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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:06:44+00:00 2026-05-17T00:06:44+00:00

I have a package with two modules in it. One is the __init__ file,

  • 0

I have a package with two modules in it. One is the __init__ file, and the other is a separate part of the package. If I try from mypackage import separatepart, the code in the __init__ module is run, which will run unneeded code, slowing down the importing by a lot. The code in separate part won’t cause any errors, and so users should be able to directly import it without importing the __init__ module.

Since I can’t figure out a way to do this, I thought I should include a function in the __init__ file that does everything so nothing would be done directly, but in order to do this, I would need to have any variables set to be global. Is there any way to tell Python that all variables are global in a function, or to not run the __init__ module?

  • 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-17T00:06:44+00:00Added an answer on May 17, 2026 at 12:06 am

    dthat I know of, there is not way to specify that all variables are global but you can import the module while you are in the module. just make sure that you do it in a function that isn’t called at the top level, you are playing with infinite recursion here but a simple use should be safe.

    #module.py
    
    foo = bar = 0 # global
    
    def init()
        import module as m
    
        m.foo = 1
        m.bar = 2 # access to globals
    

    if init was called at the top level, then you have infinite recursion but it sounds like the whole point of this is to avoid this code running at the top level, so you should be safe. Since you want to do this in the __init__.py file, just import the top level of the package.

    It occurred to me on a walk that there’s no problem with recursion here because the top level code will only run once on initial import.

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

Sidebar

Related Questions

I have these two modules : package G1; sub new { my $class =
In Eclipse I have two projects with the same package structure. In one of
Consider this scenario: I have a project with two modules and one common module
Is it possible to paqckage multiple rpms into one file. I have got two
I have two Python packages where one needs to be imported by the other.
I have an SSIS Package with two steps: 1) FTP in to a server
I have two java classes as follows App1 without a package: class App1 {
I have two classes. package utilities; public class PostCaller { public String getUrl() {
I have two projects with similar starting splash and menu activities, but the package
I'd like to be able to have two protected classes in my package. That

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.