Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda