Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda