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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:31:00+00:00 2026-06-17T17:31:00+00:00

I have three python files one.py , two.py , three.py one.py from one.py I

  • 0

I have three python files

one.py, two.py, three.py

one.py

from one.py I call

import two as two  
    two.three()

in two.py I have

def two():
    "catch the error here then import and call three()"
    import three as three

in three.py I have

def three():
    print "three called"

so naturally I am getting:

AttributeError: ‘function’ object has no attribute ‘three’

My question is:

Is there a way to have two.py capture the error then import three.py and then call three()?

______________edit________________V
I can call it like this:

two().three()

def two():
    import three as three
    return three

but I would like to call it like so:

two.three()

So essentially it would auto exec def two():

  • 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-17T17:31:01+00:00Added an answer on June 17, 2026 at 5:31 pm

    Here’s the solution I came up with. I confess that I was inspired by your question to try to figure this out, so I don’t completely understand it myself. The magic happens in two.py where the attempt to access and then call the three method of two is handled by the __getattr__ method of the method_router class. It uses __import__ to import the indicated module by name (a string), and then imitates the from blah import blah by calling getattr() again on the imported module.

    one.py

    from two import two
    two.three()
    

    two.py

    class method_router(object):
        def __getattr__(self, name):
            mod = __import__(name)
            return getattr(mod, name)
    
    two = method_router()
    

    three.py

    def three():
        print("three called")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi, there. I have two files: a.py: print('in a') import b print('var') VAR =
I have two boto.cfg files, one for QA and the other for Production. I
I have two python scripts, lets call them file1.py and file2.py , where I
I'm working on a Python script to go through two files - one containing
I have two Python packages where one needs to be imported by the other.
I am running Cygwin Python version 2.5.2. I have a three-line source file, called
django 1.4,python 2.6.6. I have an app jobs. my files tree: djproject/ |-- djproject
I have two servers, and one updates with a DNSBL of 100k domains every
new guy here and I'm slowly getting the hang of python, but I have
I'm using Python 3. I've written two programs. One loops through a csv file

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.