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”
JavaScript
x
6
1
def calculate():
2
x = 1 + 2
3
y = 3 + 4
4
return x
5
return y
6
this I would like to load into another module “print.py”
JavaScript
1
9
1
import os
2
import sys
3
sys.path.append(".")
4
import modules.calculate
5
6
def print():
7
print(x)
8
print(y)
9
But I get the error "ModuleNotFoundError: No module named 'calculate'"
What’s wrong?
EDIT
Thx guys, I can now load the module with:
JavaScript
1
2
1
from modules.calculate import calculate
2
And I changed the return to:
JavaScript
1
2
1
return x, y
2
But now I get:
JavaScript
1
2
1
"NameError: name "x" is not defined"
2
How can I import “x” and “y” into “print.py”?
Advertisement
Answer
if your programm folder looks like this:
JavaScript
1
4
1
├── programm.py
2
├── modules
3
│ ├── calculate.py
4
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:
JavaScript
1
10
10
1
import os
2
import sys
3
sys.path.append(".")
4
import modules.calculate
5
6
def print():
7
x, y = calculate()
8
print(x)
9
print(y)
10