How to import packages into other sub packages in Python?

2

I have the following structure in a project

 PacoteRaiz/
      __init__.py
      Pacote1/
        __init__.py
        Modulo1.py
      Pacote2/
       __init__.py
       Modulo2.py

If I want to, for example, use some function that is inside Modulo1.py in Modulo2.py , how should I proceed? I tried to do this within Modulo2.py

from PacoteRaiz.Pacote1 import Modulo1

This gives error, "ImportError: No module named Rake / Package1 / Module1". Does anyone know how to do it right?

    
asked by anonymous 20.05.2018 / 21:36

1 answer

1

Since both (Package1 and Package2) are inside the same root directory, just do it in Modulo2.py:

from Pacote1 import Modulo1

to import Modulo1.py, or:

from Pacote1.Modulo1 import nomeDaFuncao

to import a specific function from Modulo1.py.

    
20.05.2018 / 22:18