How to import packages within other subpackages in Python?

Asked

Viewed 107 times

2

I have the following structure on a project

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

If I want to 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, "Import: No module named Pacoteroot/Pacote1/Modulo1". Does anyone know how to do it correctly?

1 answer

1


Since both(Pacote1 and Pacote2) are within the same root directory, just do, in Modulo2.py:

from Pacote1 import Modulo1

to import Modulo1.py, or:

from Pacote1.Modulo1 import nomeDaFuncao

to import a specific Modulo1.py function.

  • Hello! Very grateful to answer! Well, I did what you said and gave the same mistake! Giving a read on the Internet, I need to add in sys.path the directory where the application is hosted?

  • Hello, really what was missing was to add to sys,path the package paths and your reply helped to arrive at the expected result, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.