How to import Python libraries that are in another hierarchy?

3

I have the following directory structure

src\
    conectores\
              mysql.py
    bibliotecas
    auxiliares
teste\
    chamarMysql.py

In the above structure how can I inside the test file \ callMysql.py, make a call to a class that is in src \ connectors \ mysql.py?

    
asked by anonymous 29.08.2016 / 21:16

2 answers

2

In order for it to understand that the other files are modules, create an empty __init__.py file in each folder.

If what you're running is in the root folder (before src and teste ), import gets something like:

from src.conectores import mysql
mysql.seu_metodo()

If it is not in the root folder, you will need a similar solution to the one described here: link

    
31.08.2016 / 13:44
0

Just put a blank file with the name in the same folder:

__init__.py

For example:

src\
    conectores\
              __init__.py
              mysql.py
    bibliotecas
    auxiliares
teste\
    chamarMysql.py

All folders belonging to your project should have the same file.

    
30.08.2016 / 11:40