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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:42:25+00:00 2026-06-14T05:42:25+00:00

I have following project hierarchy: project_dir lib __init__.py … some_script.py … agent __init__.py …

  • 0

I have following project hierarchy:

project_dir
  lib
    __init__.py
    ...
    some_script.py
    ...
    agent
      __init__.py
      ...
      errors.py
      some_agent_script.py
      ...

There is the SomeException class definition in lib/agent/erros.py
I use following code to import them inside lib/agent/some_agent_script.py:

from errors import SomeException

Also I use following code to import in lib/some_script.py

from agent.errors import SomeException

The problem is when I raise a SomeException in lib/agent/some_agent_script.py then lib/some_script.py can not catch it in an except block:

try:
    # Here comes a call to lib/agent/some_agent_script.py function
    # that raises SomeException
except SomeException, exc:
    # Never goes here
    print(exc)
except Exception, exc:
    print(exc.__class__.__name__) # prints "SomeException"

    # Let's print id's
    print(id(exc.__class__))
    print(id(SomeException))
    # They are different!

    # Let's print modules list
    pprint.pprint(sys.modules)

I can see in sys.modules that erros module was imported twice: the first is with ‘agent.errors’ key and the second is with ‘lib.agent.errors’ key

The following code goes right, but it’s not a beautiful solution:

agent_errors = sys.modules.get('agent.errors')
from agent_errors import SomeException

try:
    # Here comes a call to lib/agent/some_agent_script.py function
except SomeException:
    print('OK')

What should I do to make this module not to import twice?

  • 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-14T05:42:27+00:00Added an answer on June 14, 2026 at 5:42 am

    You should always use fully qualified imports.

    from lib.agent.errors import SomeException
    

    Do this in every module that uses it. Then it will always have the same package name.
    You should probably also change the top-level package name. The name “lib” is too generic.

    This will also save you some headaches if you happen to name a module the same as a base, or “stock”, module. For example, suppose you created a module lib/agent/socket.py and in lib/agent/some_agent_script.py you wrote import socket. You would not actually get your module, but the stock one.

    So it’s better to get in the habit of always using fully qualified package names, off a common root if you can.

    An alternative is to use absolute imports.

    from __future__ import absolute_import
    
    import .errors
    

    Note the leading dot. This explicitly imports from the current package. It should also fix your problem, but I admit I haven’t tried it.

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

Sidebar

Related Questions

I have got following class hierarchy in my ASP MVC 3 project. Some entry
I have a maven project with the following hierarchy as created with NetBeans: root/
I have the following project solution: There is a ASP.NET MVC Web Application where
I have the following project structure: /Views/Shared/_Layout; /Areas/Area1/Views/ControllerName/Index; ... /Areas/AreaN/Views/ControllerName/Index. Is there any way
I have the following project-tree: I can access the 290.gif file in the path:
I have the following project. The main form has control in it. This control
I have created the following project structure for my new asp.net mvc project any
I have the following situation Project A - Uses Castle Windsor v2.2 - Uses
I have following Enum in my project public enum CameraAccessMethod { Manual = 0,
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;

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.