Python module import problem

Asked

Viewed 1,162 times

0

I am with a proble, use Python 3.6.7 I am making 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

inside the /file folder has some py scripts that I use to read a TXT and register in the BD generate some media make an account and save in the BD have BD query scripts, have the main.py file that I use in the same shell, it has a little menuzinho that I inform the name of the TXT file and it automates everything, IE inside the folder /file ta everything working fine.

The problem is with app.py that makes the Flask routes, it matters:

import arquivospy.interfaceBD and 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'
  • In the archive interfaceBD place import . definirMercados with the point to leave the relative import path.

  • if you do this from the invalid syntax error

  • Yes, for a moment I read that you were using the from .definirMercados import ..., only with the import doesn’t work at all. So put the full import path: import arquivospy.definirMercados as mercados

  • If I do this the app.py works the plication wheel do everything right in the Browser, but if you need to use main.py to register new things, then error have to remove all the import files. in all py files that I have changed and put import definitionMercados the markets, ie have to go back to work looking for the same folder

  • You created the file __init__.py in the folders where each module is?

  • I’ve created a void with ALL = [file name.py] and I saw nothing spoken that from python 3.4 was not but accurate. problem ta in app.py call a module(interfaceBD.py) and this module do its job by interacting with its sister modules (within the same folder)

  • Ih, I haven’t seen the comments, :D You’re running your application from where, from which file in which directory?

Show 2 more comments

1 answer

0


Hello, good morning! All right?

# 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 module definirMercados inside the archive interfaceBD.py the same way you did inside the app py., that is, try to change 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 directory arquivospy with the shell and tries to run the main py. from it, then all imports present in the main py. try to search the modules from this directory, so the
import arquivospy.definirMercados
does not work inside the directory where the main py..

Two possible solutions are:

  1. Move the file main py. to the same directory where the app py. in order to execute you from this location;
  2. Execute the main py. in the terminal from the directory where the app py.;
  • within the /app folder I run app.py which is what gives me the Web interface(Flask) and the files inside of /app/file are what make all the work of the application the way it is inside the folder /app/file they are working perfect without error the problem ta in the app.py call these modules inside the folder /app/file and they continue working,

  • for them to work I have to go in all.py files inside the /app/file folder and change the import to : arquivospy.filefilefilefilefilefilefilefilefile.py then the app.py runs and does everything right, but then the module that runs straight into the main.py shell stops working; ai I have to get back all the.py files that have been changed to import direct(in the same folder) import filename.py; that main.py I run it manually from time to time only when I want to feed the BD

  • How are you doing to run main.py? Are you entering the 'arquivospy' directory with the shell and running it from there? Python Imports are relative to the directory the module is running from, if so, try moving main.py to the same directory as the app.py to see if it works. Or try running the main from the app/ directory (the same directory where the app is.py).

  • that even moved the main.py to the /app directory and it worked, thanks for the help

  • I’m glad it worked out! I edited the answer ;)

Browser other questions tagged

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