Importing classes in Python 3

1

I have three .py files in my directory: Banco.py , app.py , Usuarios.py .

My app.py contains the Tkinder import I am using in the code and here I start all my view and import the Usuarios.py class as follows:

#Arquivo app.py
from .Usuarios import *
from tkinter import *

class Application:
def __init__(self, master=None):

In the file Usuario.py it contains all the SQL executions for insert, delete, update and select, and in that file I import the file Banco.py , it follows:

#Arquivo Usuarios.py
from .Banco import Banco

class Usuarios():
    def __init__(self, idusuario=0, nome="", telefone="", email="", usuario="", senha="" ):

Then in the Bank file I only give the import in sqlite for its use, it follows:

#Arquivo Banco.py
import sqlite3

class Banco():
    def __init__(self):

The problem comes up when I run the app.py which returns me the following error:

  

/usr/bin/python3.6 "/ home / owi / Documents / PycharmProjects / Tkinder   examples / app.py "Traceback (most recent call last): File   "/ home / owi / Documents / PycharmProjects / Tkinder examples / app.py", line 1,   in       from .Users import * ModuleNotFoundError: No module named ' main .Users'; ' main ' is not a package

     

Process finished with exit code 1

I would like to understand what the problem is related to main that is not correct since all classes contain def __ini__ .

    
asked by anonymous 02.02.2018 / 02:07

1 answer

1

The ideal is to put everyone in a package .

Structure your project folder like this:

meuprojeto/
    __init__.py            
    app.py                         
    Usuario.py
    Banco.py

Being __init__.py is a file with nothing. When a page has a __init__.py , Python knows that it is a package. Then you can import libraries like this:

from meuprojeto import Usuarios

Usuarios.funcao()

Or:

import meuprojeto as mp

mp.Usuarios.funcao()

I do not like anything being implied in my programs, so I usually give preference to the second method.

Also remember that the idiomatic way of naming modules in python is to use only lowercase letters, eg: usuarios

    
02.02.2018 / 02:38