Python Forum
using variables with functions imported from different files.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using variables with functions imported from different files.
#1
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?
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Where are these variables lifesaver, apple, tidepod etc defined?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can I get some clarification on importing functions from external files. wh33t 3 914 Feb-25-2023, 08:07 PM
Last Post: deanhystad
  Can a module tell where it is being imported from? stevendaprano 3 1,191 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  module detecting if imported vs not Skaperen 1 1,683 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Storing whole functions in variables dedesssse 3 2,102 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  [newbie] Why is a module imported twice? Winfried 3 4,094 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  Getting parent variables in nested functions wallgraffiti 1 2,150 Jan-30-2021, 03:53 PM
Last Post: buran
  How do use data from csv files as variables? JUSS1K 1 2,160 Oct-25-2020, 08:31 PM
Last Post: GOTO10
  Passing Variables between files. victorTJ 3 2,261 Oct-17-2020, 01:45 AM
Last Post: snippsat
  Calling Variables from Other Files in Different Folders illmattic 14 5,575 Aug-01-2020, 07:02 PM
Last Post: deanhystad
  module to store functions/variables and how to call them? mstichler 3 2,418 Jun-03-2020, 06:49 PM
Last Post: mstichler

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020