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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:22:57+00:00 2026-06-18T11:22:57+00:00

When one tries to import a module foo while being in the source directory,

  • 0

When one tries to import a module foo while being in the source directory, one gets an rather confusing ImportError message: ImportError: No module named foo.

How can I easily catch this case and return a more informative message, e.g. ‘Please do not load module foo from the source directory’?

Having the __init__.py, I would start with:

try:
    from _foo import *
except ImportError:
    ## check whether in the source directory...

So I would like to distinguish the different causes for an ImportError (e.g. because a module named foo is not installed at all), and detect the case in which the setup.py is located in the current directory. What would be a elegant way of doing this?

  • 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-18T11:22:58+00:00Added an answer on June 18, 2026 at 11:22 am

    ImportError: No module named foo actually means the module foo.py or package foo/__init__.py could not be found in any of the directories in the search path (sys.path list).

    Since sys.path usually contains . (the current directory), that’s probably what you meant by being in the source directory. You are in the top-level directory of package foo (where the __init__.py file is) so obviously you can’t find foo/__init__.py.

    Finally, you’ve answered your own question, more or less:

    try:
        from _foo import *
    except ImportError:
        raise ImportError('<any message you want here>')
    

    Alternatively, you could check the contents of sys.path, the current directory and, if known, the expected package directory and produce an even detailed and context-aware message.

    Or add .. to the PYTHONPATH environment variable (on Unix) to allow you to run from your source directory. Might even work on Windows, but I wouldn’t know.

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

Sidebar

Related Questions

I tries to fill webgrid using one answered question and an article , But
I tried to copy and paste a component from one data module into another
I'm having a similar problem to what this person was having: Android NDK import-module
When I try to compile this: module Main where import qualified Data.Vector.Unboxed.Mutable as MV
Import maven plugin configuration by composition rather than inheritance. Can it be done with
I am trying to install the Java::Import module on my Win32 system for Perl
I would like to be able to import a python module which is actually
Background I've grown tired of the issue with pylint not being able to import
I tried one of the examples here to load the content in the div,
How does one handle ties when ranking results in a mysql query? I've simplified

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.