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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:55:41+00:00 2026-05-15T01:55:41+00:00

I have a canonical file structure like that (I’m giving sensible names to ease

  • 0

I have a “canonical file structure” like that (I’m giving sensible names to ease the reading):

mainpack/

  __main__.py
  __init__.py 

  - helpers/
     __init__.py
     path.py

  - network/
     __init__.py
     clientlib.py
     server.py

  - gui/
     __init__.py
     mainwindow.py
     controllers.py

In this structure, for example modules contained in each package may want to access the helpers utilities through relative imports in something like:

# network/clientlib.py
from ..helpers.path import create_dir

The program is runned “as a script” using the __main__.py file in this way:

python mainpack/

Trying to follow the PEP 366 I’ve put in __main__.py these lines:

___package___ = "mainpack"
from .network.clientlib import helloclient 

But when running:

$ python mainpack 
Traceback (most recent call last):
  File "/usr/lib/python2.6/runpy.py", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.6/runpy.py", line 34, in _run_code
    exec code in run_globals
  File "path/mainpack/__main__.py", line 2, in <module>
    from .network.clientlib import helloclient
SystemError: Parent module 'mainpack' not loaded, cannot perform relative import

What’s wrong? What is the correct way to handle and effectively use relative imports?

I’ve tried also to add the current directory to the PYTHONPATH, nothing changes.

  • 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-05-15T01:55:42+00:00Added an answer on May 15, 2026 at 1:55 am

    The loading code seems to be something like this:

        try:
            return sys.modules[pkgname]
        except KeyError:
            if level < 1:
                warn("Parent module '%s' not found while handling "
                     "absolute import" % pkgname, RuntimeWarning, 1)
                return None
            else:
                raise SystemError, ("Parent module '%s' not loaded, cannot "
                                    "perform relative import" % pkgname)
    

    which makes me think that maybe your module is not on sys.path. If you start Python (normally) and just type “import mainpack” on the prompt, what does it do? It should be able to find it.

    I have tried it myself and got the same error. After reading a bit I found the following solution:

    # foo/__main__.py
    import sys
    mod = __import__('foo')
    sys.modules["foo"]=mod
    
    __package__='foo'
    from .bar import hello
    
    hello()
    

    It seems a bit hackish to me but it does work. The trick seems to be making sure package foo is loaded so the import can be relative.

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

Sidebar

Related Questions

I have an MVC3 app written in C# that I'd like to generate rel=canonical
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
If I have a string that resolves to a file path in Windows, is
I have a method defined in application_helper.rb that returns a canonical URL based on
Assume that we have a canonical island genetic algorithm with the ring topology. I
Have a matrix report now that has Position, Hours and Wages for a location
EDIT 1 Hmm, I accept the answers that tar respects an empty file... but
Git, Mercurial, and Bazaar all seem to have similar formats for their ignore file
My question is regarding Inheritance modeling in Relational Database Systems. I have canonical data
I have a subversion repository, and I would like to create a branch, but

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.