Python Module Import Problem

0

I have a problem, I use Python 3.6.7 and I'm doing a Flask application with the following structure:

   /app
    /arquivospy
        consultaMercados.py
        definirMercados.py
        interfaceBD.py
        main.py

        outros arquivos py

    /static
    /templates    
    app.py
    form.py

In the folder / filespy have a py scripts that I use to read a TXT and register in the BD generate a means to do accounts and save in the BD has scripts of query to the BD, has the file main.py that I use in the same shell , it has a little box that I inform the name of the TXT file and it automates everything, ie inside the folder / filespy everything is working right.

The problem is with app.py that does the Flask routes, it does matter:

import filespy.interfaceBD and from this error:

File "app.py", line 7, in 
    import arquivospy.interfaceBD
  File "/home/karont/SiteHtml/app/arquivospy/interfaceBD.py", line 2, in 
    import definirMercados as mercados
ModuleNotFoundError: No module named 'definirMercados'
    
asked by anonymous 23.11.2018 / 04:04

1 answer

0

Hello, good morning! Is that okay?

# O seu módulo interfaceBD está importando definirMercados
File "/home/karont/SiteHtml/app/arquivospy/interfaceBD.py", line 2, in
import definirMercados as mercados
# Aqui ele diz que não conseguiu encontrar o módulo definirMercados que foi importado
ModuleNotFoundError: No module named 'definirMercados'

Try to import the setMarkets module into the interfaceBD.py file in the same way you did within app.py , that is, try changing your import line to:

import arquivospy.definirMercados as mercados

[EDITED]
When you insert an import into the module, this import is relative to the root directory from which you are running your application. If you enter the filespy directory with the shell and try to run the main.py from it, then all imports in main.py try to fetch the modules from this directory, so the import filespy.definirMercados
does not work inside the main.py directory.

Two possible solutions are:

  • Move the main.py file to the same directory as app.py to be able to run it from this location;
  • Run main.py on the terminal from the directory where app.py is
  • 23.11.2018 / 12:26