I need to call a list that I made in a function that's in another file. I was trying attempting this below, but I am getting the error cannot import name 'names' from 'backend'
. Does anyone know how this can be achieved without making a class?
import backend
from backend import names
word = names
print (word)
Error message:
File "C:/Users/user/OneDrive/Desktop/Pokemon/weather.py", line 52, in <module>
from backend import names
builtins.ImportError: cannot import name 'names' from 'backend'
The code form the other file is
import const
SEP = ','
def get_pokemon_stats():
"""Reads the data contained in a pokemon data file and parses it into
several data structures.
Args:
None
Returns: a tuple of:
-a dict where:
-each key is a pokemon name (str)
-each value is a tuple of the following stats:
-pokemon name (str)
-species_id (int)
-height (float)
-weight (float)
-type_1 (str)
-type_2 (str)
-url_image (str)
-generation_id (int)
-evolves_from_species_id (str)
-a dict where:
-each key is a pokemon species_id (int)
-each value is the corresponding pokemon name (str)
-a list of all pokemon names (strs)
-a dict where:
-each key is a pokemon type (str). Note that type_1 and type_2
entries are all considered types. There should be no special
treatment for the type NA; it is considered a type as well.
-each value is a list of all pokemon names (strs) that fall into
the corresponding type
"""
name_to_stats = {}
id_to_name = {}
names = []
pokemon_by_type = {}
DATA_FILENAME = 'pokemon.csv'
with open(const.DATA_FILENAME) as f:
header_to_col_num = parse_header(f)
for line in f:
info = line.split(const.SEP)
name = (info[(header_to_col_num['pokemon'])])
col_names = ('pokemon', 'species_id', 'height', 'weight', 'type_1',
'type_2',
'url_image', 'generation_id', 'evolves_from_species_id',)
value = [info[header_to_col_num[col]] for col in col_names]
value[1] = int(value[1])
value[2] = float(value[2])
value[3] = float(value[3])
value[7] = int(value[7])
value = tuple(value)
name_to_stats[name] = value
species_id = int(info[(header_to_col_num['species_id'])])
id_to_name[species_id] = name
names.append(name)
for name, info in name_to_stats.items():
type1 = info[4]
type2 = info[5]
if type1 in pokemon_by_type:
pokemon_by_type[type1].append(name)
else:
pokemon_by_type[type1] = [name]
if type2 in pokemon_by_type:
pokemon_by_type[type2].append(name)
else:
pokemon_by_type[type2] = [name]
return name_to_stats, id_to_name, names, pokemon_by_type