Skip to content
Advertisement

Pass multiple variables from one module to another

I have several modules in the folder “Programm/modules” which are called by another script in “Programm”.

One for example looks like this:

“calculate.py”

def calculate():
  x = 1 + 2
  y = 3 + 4
  return x
  return y

this I would like to load into another module “print.py”

import os
import sys
sys.path.append(".")
import modules.calculate

def print():
  print(x)
  print(y)

But I get the error "ModuleNotFoundError: No module named 'calculate'"

What’s wrong?

EDIT

Thx guys, I can now load the module with:

from modules.calculate import calculate

And I changed the return to:

return x, y

But now I get:

"NameError: name "x" is not defined"

How can I import “x” and “y” into “print.py”?

Advertisement

Answer

if your programm folder looks like this:

├── programm.py
├── modules
│   ├── calculate.py

from modules.calculate import calculate

EDIT:

use the value you return to assign it to a variable. like x, y = calculate(). Now you can use (these) x and y in your print.py like this:

import os
import sys
sys.path.append(".")
import modules.calculate

def print():
    x, y = calculate()
    print(x)
    print(y)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement