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

495
Visualizações
Proper relative imports: "Unable to import module"

I have a project structured like this:

.
└── myapp
    ├── app.py
    ├── models
    │   ├── hello.py
    │   └── world.py
    └── requirements.txt

I have two models, hello and world. Both models are used from app.py where I import them like this:

from models.hello import Hello
from models.world import World

But world also needs to use hello. I tried this in world.py:

from models.hello import Hello

The above technically works when I run the app, but VSCode's Python extension gives me the following error:

E0401:Unable to import 'models.hello'.

What is the proper way of importing a submodule from the same directory? How do I avoid this error in VSCode?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The error you are receiving is one that's reported by a python linter named pylint. So the problem isn't really specific to the vscode extension.

There are two solutions:

  1. Please try adding an .env file in your project directory with the vape PYTHONPATH=./myapp, this will tell pylint where to find your modules

  2. Or just open the folder myapp in vscode directly instead of opening the parent directory in vscode.

over 4 years ago · Santiago Trujillo Relatório

0

The error is coming from pylint. You need to add this line into settings.json file (VS Code):

"python.linting.pylintArgs": ["--init-hook",
        "import sys; sys.path.append('<absolute path to myapp directory>')"],
over 4 years ago · Santiago Trujillo Relatório

0

Since hello.py and world.py are in the same folder (aka package), you should import the Hello class in world.py as follow:

from .hello import Hello

As described in this thread: What does a . in an import statement in Python mean?

The . is here to indicate the import from the current package.

over 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