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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:24:48+00:00 2026-06-17T04:24:48+00:00

I have package p that has modules a and b . a relies on

  • 0

I have package p that has modules a and b. a relies on b:

b.py contents:

import a

However I want to ensure that b imports my a module from the same p package directory and not just any a module from PYTHONPATH.

So I’m trying to change b.py like the following:

from . import a

This works as long as I import b when I’m outside of p package directory. Given the following files:

/tmp
    /p
       a.py
       b.py
       __init__.py

The following works:

$ cd /tmp
$ echo 'import p.b' | python

The following does NOT work:

$ cd /tmp/p
$ echo 'import b' | python
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "b.py", line 1, in <module>
    from . import a
ValueError: Attempted relative import in non-package

Why?

P.S. I’m using Python 2.7.3

  • 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-17T04:24:49+00:00Added an answer on June 17, 2026 at 4:24 am

    After rereading the Python import documentation, the correct answer to my original problem is:

    To ensure that b imports a from its own package its just enough to write the following in the b:

    import a
    

    Here is the quote from the docs:

    The submodules often need to refer to each other. For example, the
    surround module might use the echo module. In fact, such references
    are so common that the import statement first looks in the containing
    package before looking in the standard module search path.

    Note: As J.F. Sebastian suggest in the comment below, use of implicit imports is not advised, and they are, in fact, gone in Python 3.

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

Sidebar

Related Questions

I have several modules that are within a Python package: # my_package contents __init__.py
I have an SSIS package that queries data from a view using an SQL
I have written an SSIS package that essentially takes data from multiple sources and
I have an SSIS (2005) package that transforms some XML data and then imports
I have to build an application using Maven for PHP that has multiple modules.
I have a Perl program that has a GTK2 GUI (via the Gtk2 package).
I have a quite huge Java class that has several imported packages and libraries
We have an ETL package that writes data to the SQL Server. Data is
I have a basic hosting package that gives me access to create a MySQL
I have a function in a package that returns a REF CURSOR to 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.