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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:50:16+00:00 2026-06-09T17:50:16+00:00

Python imports. Again… I have this file structure: [test] start.py (from scripts import main)

  • 0

Python imports. Again…

I have this file structure:

[test]
    start.py (from scripts import main)
    [scripts]
        __init__.py (empty)
        main.py (from . import install)
        install.py (from scripts import main # or # from . import main)

I get import error:

vic@wic:~/projects/test$ python3 start.py 
Traceback (most recent call last):
  File "start.py", line 2, in <module>
    from scripts import main
  File "/home/vic/projects/test/scripts/main.py", line 1, in <module>
    from . import install
  File "/home/vic/projects/test/scripts/install.py", line 1, in <module>
    from scripts import main
ImportError: cannot import name main
vic@wic:~/projects/test$

I don’t understand: first time from scripts import main worked (by “worked” I mean it didn’t fail with ImportError), and second time the same code gives ImportError: cannot import name main

What is going on?

UPDATE:

My question is not about circular imports. I am confused by the fact that exactly the same code from scripts import main first time works ok and then second time fails.

I guess there is some internal import mechanism which i don’t understand.

First time a statement imports a module, second time exactly the same code tries to import a name from a module. How this works?

  • 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-09T17:50:18+00:00Added an answer on June 9, 2026 at 5:50 pm

    You created a circular import. You can’t do that. Avoid importing main from install.

    What is happening is that a module, while being imported, is incomplete until the whole top level has been executed. If during that execution it imports another module that (directly or indirectly) tries to import the original module again, this will fail. The original module was not yet done importing yet.

    In other words, you are creating a circular graph:

    start -> scripts.main -> scripts.install
                  ^                |
                  |                |
                   ----------------
    

    You need to re-arrange your code to not need to import from main from within your scripts package.

    See What are the “best practices” for using import in a module? for some tips on how to handle this.

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

Sidebar

Related Questions

Let's assume I have a main script, main.py, that imports another python file with
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
I have the following script for sending mails using python import smtplib from email.mime.multipart
Assume the following code structure: #### 1/hhh/__init__.py: empty #### 1/hhh/foo/__init__.py: from hhh.foo.baz import *
I have a python module that imports a module generated with swig. When I
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
I have a small GTK python application that imports a package (Twisted) that may
Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like
I have a Python script that uses built-in modules but also imports a number
I tried to run the following code from http://docs.python.org/library/mmap.html import mmap # write a

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.