Python module import Import ('cannot import name rr',))

Asked

Viewed 761 times

0

I have two python codes, one that is called [receiving.py] another that is called [sending.py] I have a problem importing my codes. I’m just gonna send a little piece of my code that I’m in trouble.

EX:

ENVIAR.py

import pika
def envio(ch, method, properties, body):
     print(body)
     try:
         dados_envio = json.loads(body)
         from recebimento import run,rr
         run(data)
         rr(data) ## aqui é a conexão com meu codigo "recebimento.py" para ele receber dados do "enviar.py"

RECEIPT.py

import json
def run(data):
    driver = webdriver.Firefox()
    driver.get('google.com')
    elemento = driver.find_element_by_name('q')
    elemento.send_keys('{}'.format(data['nome']))
    def rr(data):
        elemento2 = driver.find_element_by_name('q')
        elemento2.clear()
        elemento2.send_keys('{}'.format(data['cidade']))

i want to do data exchange with more than one function.

but I get that mistake.

ImportError('cannot import name rr',))

Someone knows how to help me?

1 answer

1


The problem is on the line from recebimento import run,rr of the archive enviar.py. You are trying to import from the module the function rr that is within another function. That is why this error is happening. In Python, you cannot use variables, functions and others, created locally ( created within functions, methods, etc ).

To solve this problem, the function rr should be created within the scope global. Example:

import json
def run(data):
    driver = webdriver.Firefox()
    driver.get('google.com')
    elemento = driver.find_element_by_name('q')
    elemento.send_keys('{}'.format(data['nome']))
def rr(data):
    elemento2 = driver.find_element_by_name('q')
    elemento2.clear()
    elemento2.send_keys('{}'.format(data['cidade']))

Perhaps changing the scope of the function does not affect your program. Otherwise, you can also return this function, passing it to a variable called rr in the global scope. Example:

import json
def run(data):
    driver = webdriver.Firefox()
    driver.get('google.com')
    elemento = driver.find_element_by_name('q')
    elemento.send_keys('{}'.format(data['nome']))
    def rr(data):
        elemento2 = driver.find_element_by_name('q')
        elemento2.clear()
        elemento2.send_keys('{}'.format(data['cidade']))
    return rr
rr = run(data)

Thus the variable rr containing the function will be imported.

Browser other questions tagged

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