using variables with functions imported from different files. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: using variables with functions imported from different files. (/thread-37290.html) |
using variables with functions imported from different files. - Scordomaniac - May-24-2022 hey, I'm trying to run a function from a different file in my main file. this isn't working as the function won't load variables across files, how can this be achieved? main file: inv.invcheck()function file: def invcheck(): print("function has loaded...") print(lifesaver, """Lifesavers """, apple, """Apples """, tidepod, """Tidepods """, alcohol, """Bottles Of Alcohol """, rifle, """Hunting Rifles """, laptop, """Laptops """, cheese, """Cheese Slices """, pole, """Fishing Poles """, fish, """Common Fish """, rarefish, """Rare Fish """, exoticfish, """Exotic Fish """, legendaryfish, """Legendary Fish """, rarepepe, """Rare Pepe """, pepec, """Pepe Coins """, pepem, """Pepe Medal """, pepet, """Pepe Trophys""")when I run the main file: how do i get this function to run with the variables in the main file?
RE: using variables with functions imported from different files. - menator01 - May-24-2022 The error is saying lifesaver is not defined. You are not passing it any args. Indentation is wrong with your example as well. Here is an example myinc.py def myfunc(arg): print(f'This is a passed argument -> {arg}')test.py import myinc myinc.myfunc('This string is passed as an argument')
RE: using variables with functions imported from different files. - Gribouillis - May-24-2022 Where are these variables lifesaver, apple, tidepod etc defined? RE: using variables with functions imported from different files. - deanhystad - May-24-2022 I'm pretty sure this is the situation. module junk.py def print_x(): print(x)module uses_junk,py from junk import print_x x = 1 print_x()When run you get a NameError. The variable "x" does not exist in the module that is trying to use it (junk.py). It is defined in a different module (uses_junk.py). Not only this, but the problem cannot be fixed by importing the variable so it can be used because this creates a circular reference.junk.py from uses_junk import x def print_x(): print(x) A way around this is to define the variables in the module that uses them.junk.py x = 1 def print_x(): print(x)uses_junk.py import junk junk.x = 42 junk.print_x()Since global variables are a bad idea in general, this kind of thing is usually done by placing the variable and the function inside a class. junk.py class X: def __init__(self, value): self.value = value def print(self): print(self.value)uses_junk.py from junk import X my_x = X(42) my_x.print()You need to decide where the variables belong. If the variables are associated with the function, they should be defined where the function is defined. If the variables are used by the function, but they have more meaning elsewhere, they should be passed as arguments to the function as mentioned in menator's post. |