Imports not working?


I am not entirely sure what is happening here but I had it working for a moment before all I did was actually use the imports
The rest of the pictures are in the comments

What might be the issue here?

image

Circular imports is when the addition.py imports main.py, which tries to import addition.py… In an infinite loop.

1 Like

So how might I get some variable from main.py to addition.py?

addition.py is a helper file. main.py treats it as a servant, mostly.
therefore, addition.py should be independent.

the answer is, getting a variable from main.py to addition.py is not recommended, since addition.py is a helper and should not depend on its master.

but, I’ll provide an example, of how main.py could import a function from addition.py and use it.

addition.py

def add(*numbers):
    return sum(numbers)

main.py

from addition import add
sum = add(1,2,3)
assert sum == 6

Okay, well for the most part that is what I had before though I am attempting to add a memory function to it, is there a better way to do that or would i just have to combine the files?

What is a memory function?

so I am coding a calculator and want to add some way of keeping a number from a previous task
so taking the sum of some numbers and in a different function use that sum without having to type it, like you can on some actual calculators

Interesting. Looks like you’ll need a way to store temporary storage.

I recommend environment variables.

addition.py

import os
def add(*numbers,previous=False):
    ans = os.getenv("ans")
    ans = ans if ans and previous else 0
    result = sum(numbers) + ans
    os.putenv("ans",result)
    return result

main.py

import os
from addition import add

result1 = add(1,2,3)
assert result1 == 6

result2 = add(1,2,previous=True)
assert result2 == 6 + 1 + 2

result3 = add(4,5,6)
assert result3 == 15

Did you make the memory function you wanted?
@TheDarkGamer

just has to step away for a moment I will try it and get back to you

I was unable to get this to work, but I have decided to just combine the files and just make a local variable. Thank you for your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.