Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing acting weird
#1
I'm trying to call functions from my "functions.py" module to my "mainTest.py" module.

Here is basically what I write:

from functions import function1

list1 = function1() #function1() is set to return a list#


My problem is, that once I try importing anything from the functions module, it reads the entire module through. Is this how it's supposed to work?

The problem here is, that when it tries to read the entire module, it requires variables that are not yet set.

I would like to import a single function, and work my way from there, and not have the whole module read when I simply want to import a function from there.

Also, I'm guessing that if I run a function from another module, it won't automatically know the variables from the module I am using the function in?

I have started pythoning 3 days ago, so please speak simply if you suggest fixes. But just getting answers to these questions would be nice, and I can figure out the rest I'd imagine.

Thanks guys!
Reply
#2
(Jan-24-2020, 07:43 PM)IILawrenceII Wrote: My problem is, that once I try importing anything from the functions module, it reads the entire module through. Is this how it's supposed to work?
Yes, that's exactly how it works.

(Jan-24-2020, 07:43 PM)IILawrenceII Wrote: The problem here is, that when it tries to read the entire module, it requires variables that are not yet set.
That's a design problem. With more details, we can help you through that.

(Jan-24-2020, 07:43 PM)IILawrenceII Wrote: Also, I'm guessing that if I run a function from another module, it won't automatically know the variables from the module I am using the function in?
No, and that's intentional. If you want a function to use a variable, make it a function parameter, don't try to use global variables or anything like them.
Reply
#3
Now I have another problem. I moved all the functions to the "mainTest.py" module. Here is what happens.

def function1():
     listName = [0,0,0,0]
     return listName


list1 = function1()
Error:
error: list1 not defined
Reply
#4
If you have all the code you posted above in one file it shouldn't error out. It worked for me after fixing the indentation (since you didn't use code tags). If you want to add them, edit your post, select the python code and click the python icon button in the middle of the edit bar ( it's blue, no yellow! ).
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  openpyxl rename sheet acting strange Pedroski55 1 1,906 Sep-26-2022, 08:56 AM
Last Post: buran
  breaking a large program into functions, not acting as expected Zane217 9 3,010 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Nested dictionary acting strange Pedroski55 2 2,085 May-13-2021, 10:37 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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