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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:25:53+00:00 2026-05-30T12:25:53+00:00

I have a package of the following form: $ ls folder entry_point.py hello.py __init__.py

  • 0

I have a package of the following form:

$ ls folder
entry_point.py  hello.py  __init__.py  utils.py

This is a package, and I can treat it as such:

$ python2.7
>>> import folder.utils
>>>

I want to use relative imports between these Python modules.

$ cat folder/entry_point.py 
from hello import say_hello

if __name__ == "__main__":
    say_hello()
$ cat folder/hello.py 
from .utils import say

def say_hello():
    say()
$ cat folder/utils.py
def say():
    print "hello world"

I know I can’t use relative imports at the entry point, where I call the interpreter. However, I still get ImportError from my imported files:

$ python2.7 folder/entry_point.py 
Traceback (most recent call last):
  File "folder/entry_point.py", line 1, in <module>
    from hello import say_hello
  File "/tmp/folder/hello.py", line 1, in <module>
    from .utils import say
ValueError: Attempted relative import in non-package

This is rather counterintuitive, it is a package, it’s just not treated as one due to entry_point.py having __name__ set to __main__ (in line with PEP 328).

I’m surprised that hello.py has a __name__ of hello rather than folder.hello. This stops me using relative imports in hello.py.

How do I use relative imports in this package? Am I forced to move hello.py and utils.py to a libs subpackage?

  • 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-30T12:25:54+00:00Added an answer on May 30, 2026 at 12:25 pm

    If you want folder to be a module inside a bigger project and you want to be able to run entry_point.py for using your folder module – move entry_point.py one level up:

    from folder.hello import say_hello
    
    if __name__ == "__main__":
        say_hello()
    

    Import paths – the right way?

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

Sidebar

Related Questions

I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
I am using Ubuntu 9.04 I have installed the following package versions: unixodbc and
I have an SSIS package that does the following: Selects the connection strings from
I have the following code but the lon/lat seems to be returning null; package
Say I have a package mylibrary. I want to make mylibrary.config available for import,
I have a form (form2) and I implemented the following PUBLIC method: function ShowInterface(i:integer):boolean;
I have created a package which contains following files to install a package. setup.exe
I have the following grammar (i'm showing only the important sectons) packageDeclaration : PACKAGE
i have the following in my app-servlet.xml <mvc:annotation-driven /> <context:component-scan base-package=com.merc.myProject.web.controllers/> <context:component-scan base-package=com.merc.myProject.web.forms/> what

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.