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

278
Vistas
How can I fix the relative import error: "with no known parent package"?

I have been trying for a couple of days to solve the relative import error in my project with no success. I have the following project tree:

proj
|
|---__init__.py
|
|---lib
|    |---__init__.py
|    |---func.py
|
|---src
|    |---main.py
  • the __init__.py is an empty file.

  • the func.py contains a print function:

    def hello_func():
        print("Hello !!!")
    
  • the main.py:

    from ..lib import *
    hello_func()
    

and when I run it I receive this error:

ImportError: attempted relative import with no known parent package

did anybody face the same problem?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I want to give the solution first. You can try to use from lib import * and run /proj/__init__.py, it works fine for me.
The reason for the no known parent package error is because you run /proj/src/main.py, which manifests /proj/src is the root package. Basically, a package is for importing, not running. That is, you shouldn't run the python code in a package, instead, you should import it.

over 4 years ago · Santiago Trujillo Denunciar

0

from ..lib import * would instruct python to climb to the parent directory of main.py, there find lib and import everything from it - that is from the empty init file so nothing will happen either. You have to change this to from ..lib import func; func.hello_func(). This will still blow with the same error. The error is due to python not thinking in terms of directories exactly as I said above. Python will only look for packages in the working directory you started it from (and in the sys.path list but don't mess with that). You get this error cause you run your script directly from src - so for python the root of the "filesystem" is src - there is no known (to python) parent package (folder). This is an anti-pattern in python (running scripts from their dir). What you should be doing instead is

$ cd proj/..
$ python -m proj.src.main # note no .py

This makes the proj folder the "root" of the filesystem (for python) and python will detect all packages in there - the src.main tells python that the main.py is part of the src package which is detected as well as lib (as they are direct children of proj) and when you do a relative import as in ..lib python already knows about the lib package.

over 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