2

I have two scripts main.py and get_number.py. The script get_number.py returns a random number whenver it's called. I want to call this script from main.py and print all these returned values. In other words, the script get_number.py is the following:

def get_random():
    return np.random.uniform(0,1)

Now I have the following code in main.py

import get_number

n_call = 4

values = np.zeros(n_call)

for i in range(n_call):
      values[i]= get_number.get_random()

print(values)

However I am receiving the error that No module named get_number. How would I go about accomplishing this task?

pikachuchameleon
  • 665
  • 3
  • 10
  • 27

3 Answers3

2

I believe you can import just as importing another libraries

from file1 import *  

Importing variables from another file?

I Found some similar Problems up here

Breno Baiardi
  • 99
  • 1
  • 16
1

You are confusing between get_number and get_random

main.py:

import numpy as np
from get_number import get_random

n_call = 4

values = np.zeros(n_call)

for i in range(n_call):
      values[i]= get_random()

print(values)

Out: [ 0.63433276 0.36541908 0.83485925 0.59532567]

get_number:

import numpy as np

def get_random():
    return np.random.uniform(0,1)
Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
1

You have to import this way: In main.py

from get_number import get_random
n_call = 4

values = np.zeros(n_call)

for i in range(n_call):
      values[i]= get_random()

print(values)
Mohammed Shareef C
  • 3,829
  • 25
  • 35