Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How do relative imports work?

I have a directory structure like this

project/
        __init__.py
        greetings.py
        script.py

greetings.py

def hello():
    print('Hello')

script.py

from . import greetings

greetings.hello()

When I execute python script.py following error occurs

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    from . import greetings
ImportError: cannot import name 'greetings'

If I just use import greetings in script.py everything works. But why does the relative import give error? And why does it work when I use from . import models in views while working in django.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Please follow Module vs Package concept in python.

Here you created a package so you have to import package as:-

from project import greetings
about 4 years ago · Santiago Trujillo Report

0

Here relative import will work as

from .greetings import *

OR

import greetings

greetings should be on same folder

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!