Import of python modules

Asked

Viewed 181 times

2

I’m having a problem with python.

I have a python code file working with Selenium, and I have another python file working on JSON. These two codes are inside the same folder, for me to import a JSON.py file into the Selenium.py file

This is the JSON.py file

import json
def json():
    dados = '{'dados_enviar': 'google.com'}'
    data = json.loads(dados['dados_enviar'])
    print(data)
json()

This is the Selenium.py file

from JSON import * # meu arquivo JSON.py que está na mesma pasta que o Selenium.py
import json           # biblioteca nativa do python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('https://www.google.com')
elem = driver.find_element_by_name('q')
elem.send_keys('{}'.data['dados_enviar'])

when I run the code it returns me the following error.

NameError: global name 'data' is not defined

Can someone please help me???

2 answers

1


Well, man I ran your code in my IDE, and initially the first error was regarding the name of the function you assigned in "JSON.py", the name json() cannot be used when you care about the json library, when trying to create a function with that name it will give error, since this function is already broken in the json library. Strange that your editor didn’t point out such a problem.

Second thing, you wouldn’t necessarily need to import json into both programs, because if one is importing the other, there’s no need.

And note that the declaration of the variable "data" has a syntax error, if you want it to be a dictionary, it should be as follows:

dados = {'dados_enviar': 'google.com'}

EDIT: As for the error that gives, the same indicates that the variable date is not defined, and this happens because the names defined in a function refer to the module in which they have been defined, and not in the modules in which they are being imported, I have a suggestion to correct this, that would be:

JSON.py:

import json
def worker():
    dados = {'dados_enviar': 'google.com'}
    data = json.loads(dados['dados_enviar'])
    return data

Selenium.py:

from JSON import * 
import json           
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

data = worker()   #Fazendo isso, você estancia a variavel no módulo local, recebendo o valor retornado pelo import

driver = webdriver.Firefox()
driver.get('https://www.google.com')
elem = driver.find_element_by_name('q')
elem.send_keys('{}'.data['dados_enviar'])
  • I had put (data = '{"process": "google.com"}' ), edited the code as you said but it returns an error for me ( Typeerror: expected string or buffer )

  • i edited the function name, put ( def worker(): ), keeps giving the same error.

  • edited the question in order to try to solve what you want, see if it was coherent, and if it worked!

  • OOOWW thanks guy helped a lot!!!!!!

0

When you set a variable in a function, it is only accessible in that capacity. Therefore, the data defined in def json() is not available in the context of your Selenium.py.

To provide information for other functions, you have to pass it through a return instruction within the function, possibly assigning it to a variable. So you can call this returned variable that is "loaded" with the data that came from your json.

import json
def json():
    dados = '{'dados_enviar': 'google.com'}'
    data = json.loads(dados['dados_enviar'])
    print(data)
    return data
json()
  • so I did what you suggested to me, it returned me error ( Nameerror: name 'data' is not defined )

  • Yes, you have to declare it as a global variable. Read this. It’s very easy to understand and apply. There I put only one direction

Browser other questions tagged

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