-1

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

Nobody
  • 77
  • 7
  • Did you create the ``__init__.py`` file in ``modules``? – Torben Klein Apr 25 '22 at 07:39
  • 1
    not really relevant, but your mockup calculate function only returns x, as it never reaches the `return y`statement. – Damiaan Apr 25 '22 at 07:46
  • 1
    Considering the import error, use: ```from modules import calculate``` and use the function with ```calculate.calculate()``` or import the function with ```from modules.calculate import calculate```. Away from import error, you will not get y-value, since your function "is done" after the first return statement. consider returning a tuple instead: ```return x, y``` and unpack it: ```x, y = calculate()```. – prog2de Apr 25 '22 at 07:46
  • @TorbenKlein No, for what do I need the __init__.py? – Nobody Apr 25 '22 at 07:50
  • The presence of ``__init__.py`` indicates to Python that the directory should be considered as Python Package. See https://stackoverflow.com/questions/448271/what-is-init-py-for – Torben Klein Apr 25 '22 at 07:55
  • Thx! I inserted the file, but there is no difference between `from modules import calculate`. @Ifreist: Where do I need to unpack it? – Nobody Apr 25 '22 at 07:59
  • @TorbenKlein: From python 3.3 packages don't need the `__init__.py` file. – quamrana Apr 25 '22 at 08:17
  • Seems you're right. I'm a dinosaur. – Torben Klein Apr 25 '22 at 08:32

1 Answers1

3

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)
Damiaan
  • 777
  • 4
  • 11
  • That worked, thanks! And how can I import "x" and "y" into the module "plot"? Because I get now the new error "NameError: "x" is not defined" – Nobody Apr 25 '22 at 07:49
  • 1
    Since you `return` `x` and `y` in your function, you dont have to import them! just assign it to a variable like `x, y = calculate()`. Do note that (as I commented on your original answer) you should adjust your `calculate()` function to `return x, y` instead of `return x` and `return y`. The `y` is now never returned as the program never reaches it after the first `return` statement. Does this answer your question? – Damiaan Apr 25 '22 at 07:53
  • 1
    @Nobody please also see my edit to the original answer. – Damiaan Apr 25 '22 at 07:54
  • Thx! But where do I have to add "x, y = calculate()"? into the "parent script" within "Programm", or within "calculate.py" or "plot.py"? – Nobody Apr 25 '22 at 07:56
  • 1
    @Nobody i've made another edit to clarify. – Damiaan Apr 25 '22 at 07:59
  • 1
    @Nobody it is not smart to use `print()` as a function name, since `print()` is also a core python function itself. You even use it inside your `print` function! to avoid weird bugs, you should rename your `print` function to something like `my_print()`. – Damiaan Apr 25 '22 at 08:02