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

The Archive Base Latest Questions

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

Here’s a mysterious python problem: I’m developing a python package that occasionally reports import

  • 0

Here’s a mysterious python problem:

I’m developing a python package that occasionally reports import errors looking like ImportError: cannot import name …. The modules it cannot import generally

  • are importable
  • do not have any circular import issues (that I can detect).

I have been able to reproduce a similar effect with this simple example:

mypkg/__init__.py:

    from . import module_a
    yarg  ## cause import error

mypkg/module_a.py:

    print "imported module_a"

Now I will attempt to import the package twice. Notice that the error changes on the second import:

>>> import mypkg
Module A imported
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mypkg/__init__.py", line 2, in <module>
    yarg
NameError: name 'yarg' is not defined
>>> import mypkg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mypkg/__init__.py", line 1, in <module>
    from . import module_a
ImportError: cannot import name module_a

What gives?

Note:

  • the problem goes away if I use an absolute import instead
  • if I delete the key sys.modules['mypkg.module_a'] after the first import, then the second import gives me back the original error message
  • 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:57:46+00:00Added an answer on June 12, 2026 at 3:57 pm

    I can illustrate what is causing the difference between each import, but I’m not expert enough on Python’s import process to be able to explain the why very well.

    >>> import sys
    >>> before_import = set(sys.modules.keys())
    >>> import mypkg
    imported module_a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "mypkg\__init__.py", line 2, in <module>
        yarg  ## cause import error
    NameError: name 'yarg' is not defined
    >>> after_import = set(sys.modules.keys())
    >>> after_import.difference(before_import)
    set(['mypkg.module_a'])
    

    When you import mypkg, it successfully imports module_a and adds it to sys.modules. Then mypkg errors and doesn’t get added itself to the sys.modules dictionary. Deleting the entry allows you to reimport with the same error:

    >>> import sys
    >>> del sys.modules['mypkg.module_a']
    >>> import mypkg
    imported module_a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "mypkg\__init__.py", line 2, in <module>
        yarg  ## cause import error
    NameError: name 'yarg' is not defined
    

    Now, what I think is happening is:

    1. import mypkg starts the import process for mypkg

    2. As it’s processing mypkg, it successfully imports module_a as
      a subpackage of itself and adds it to sys.modules

    3. When it hits the error, the import process for mypkg fails and no
      entry for mypkg is left in sys.modules

    4. The conjunction of the package failing but the subpackage succeeding
      conflicts with subsequent imports

    That’s about the best I can fathom, sorry. Python’s import process is something of a black art.

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

Sidebar

Related Questions

Here's a coding problem for those that like this kind of thing. Let's see
Here is the problem that I am trying to solve. I have two folders
Here's the code I have. It works. The only problem is that the first
Here is an object that I'd like to use with ng-repeat, but it's not
Here is another spoj problem that asks how to find the number of distinct
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
Here's what my table TheTable looks like ColA | ColB | ColC ------+-------+------ abc
Here is my problem : I have a post controller with the action create.

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.