Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading