Python Forum

Full Version: using variables with functions imported from different files.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
Error:
Traceback (most recent call last): File "O:\Jackpot Studios\PyMemer DEV\pymemer.py", line 294, in <module> inv.invcheck() File "O:\Jackpot Studios\PyMemer DEV\functions\inv.py", line 3, in invcheck print(lifesaver, """Lifesavers NameError: name 'lifesaver' is not defined
how do i get this function to run with the variables in the main file?
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')
Output:
This is a passed argument -> This string is passed as an argument
Where are these variables lifesaver, apple, tidepod etc defined?
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.
Error:
Traceback (most recent call last): File "...\uses_junk.py", line 4, in <module> print_x() File "...\junk.py", line 2, in print_x print(x) NameError: name 'x' is not defined
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)
Error:
Traceback (most recent call last): File ".../junk.py", line 1, in <module> from uses_junk import x File "...\uses_junk.py", line 1, in <module> from junk import print_x File "...\junk.py", line 1, in <module> from uses_junk import x ImportError: cannot import name 'x' from partially initialized module 'uses_junk' (most likely due to a circular import) (...\uses_junk.py)
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.