Module and hierarchies import - Python

Asked

Viewed 86 times

2

Guys I’m having the following question.

I am studying importing modules and grabbed the following problem, my directory is with the following configuration:

Estrutura Diretório

Doubt 1 -> I want to import the operation modules to print and vice versa

Doubt 2 -> Import outro_módulo.py for imprimir.py or for one of the operating modules.

I’ve used that procedure:

import imp  
arquivo, caminho, descrição = imp.find_module('módulo', ['/caminho/do/módulo/'])
nome_pro_módulo = imp.load_module('módulo', arquivo, caminho, descrição)  

(worked but if changing machine can give conflict with respect to the way)

I used this one too:

import ..operacao import soma

But the following error occurs: Parent module '' not loaded, cannot perform relative import

Would anyone know where I’m going wrong?

  • In which file you are importing, in main.py?

  • So in main.py I can import normally. The problem is qd I want to import the main.py module to print or if I want to import the soma.py module to print... these are the cases where the listed errors occur.

  • 1

    Is it possible to post code snippets? It is very confusing and difficult to understand what you are trying to do. Post an excerpt that worked in the desirable way and one that did not work, if possible.

  • If you are inside main.py this import works normally: "from operation.subt import Subtracts"

  • Now if you are inside print.py and want to import this same module I used this path: "from .. import operation Subtrai", this error appears: Parent module 'not Loaded, cannot perform relative import

  • Take a look here: http://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py

  • Thanks, but I had already looked this Question and it didn’t work for what was said.

Show 2 more comments

1 answer

0

Inside the module imprimir just import as follows:

from operacao import soma
  • I have tried this too and not its certain, says that there is no such module. I tried even this way: from . operation import sum and tb did not work.

Browser other questions tagged

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