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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:05:11+00:00 2026-06-12T15:05:11+00:00

I have a main file, called ‘main.py’ and two subfolders. One is called ‘externals’

  • 0

I have a main file, called ‘main.py’ and two subfolders. One is called ‘externals’ and contains ‘easygui.py’ and the other one is called ‘modules’ and contains a file called ‘gui.py’.

The program is started via main.py

I import easygui in main.py with import externals.easygui. The same way I import modules.gui

But when I call the welcome-function, It does not know easygui, I have to import it in the function. As I want to call more easygui-functions, I don’t want to import easygui in every def.

How can this be solved?

Thanks in advance!

Steffen

Examples (for readability without try/except and comment stuff):

main.py:

#!/usr/bin/env python

import externals.easygui
import modules.gui

main():
    gui.welcome()

gui.py:

def welcome():
    msgbox("Welcome!", ok_button="Ok")
  • 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-06-12T15:05:12+00:00Added an answer on June 12, 2026 at 3:05 pm

    Assuming that welcome is in easygui.py, you want:

    def main():
       externals.easygui.welcome()
    

    As these things can get tedious to type, it’s often customary to import subpackages under an abbreviated name:

    import externals.easygui as eg
    def main():
        eg.welcome()
    

    Alternatively, if you can make the whole thing a package by adding __init__.py, and then you can control the namespace which gets imported from there …


    As far as sideways imports go, here’s a test directory structure I set up:

    steffen
      |- __init__.py
      |- main.py
      |- easygui
         |- __init__.py
         |- gui.py
      |- external
         |- __init__.py
         |- welcome.py
    

    Now, (for simplicity) each __init__.py simply imports the files/modules contained in that directory. So, in steffen:

    #steffen.__init__.py
    import main
    import easygui
    import external
    

    and in external

    #steffen/external/__init__.py
    import welcome
    

    and so forth.

    for the actual code:

    main.py:

    import easygui
    def main():
        easygui.gui.welcome()
    

    easygui/gui.py:

    import steffen.external as se
    def welcome():
        se.welcome.hello()
    

    external/welcome.py

    def hello():
        print "Hello"
    

    Now I can use all of this. In the parent directory of steffen (just to make sure the package steffen is on PYTHONPATH), I can:

    import steffen
    steffen.main.main()
    

    Phew! Now, it’s a little silly to have steffen.main.main(). If you want to refer to the function as just steffen.main(), you can set that up in steffen.__init__.py. Just change it to:

    #steffen.__init__.py
    from main import main
    import easygui
    import external
    

    So, if you would call a function by foo.func() in __init__.py, you’ll call it as steffen.foo.func() in a script that imports steffen. Likewise, if you would call the function as foo() in __init__.py, you’ll call it as steffen.foo() in a script that imports steffen. Hopefully that makes sense. There’s a lot to digest in this simplest working example I could come up with. The upside, if you can work through all of this and understand it, then you know almost everything there is to know about writing python packages (we haven’t talked about relative imports which could be used here too, or about writing a setup.py to actually install your package, but those are fairly easy to understand once you understand this stuff).

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

Sidebar

Related Questions

I have a file called main.py and a file called classes.py main.py contains the
I have a main branch with a file called app.js . I made changes
We are installing out program via inno setup. We have one main exe file,
So, I am using IBM Worklight where I have the main file called file1.html
Okay, so let's say I have a class file called Orange, and then two
Okay, I have a main source called main.c , a header file called test.h
I have a file called main.c. Whats the simplest Makefile I can have to
I have a script that includes a config file (called accountsconfig.php) that contains a
gcc 4.4.2 c89 I have a file called main.c. I want the result of
I have my main.cpp file, and in that folder is another folder called src,

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.