0

I have a main python code with variables that I wish to control from a separate python script. I get a name error message and I am unsure how to resolve this. Has anyone encountered a similar challenge? How can I control the variable n from a separate python script?

from main import *
npart = 5

NameError                                 Traceback (most recent call last)
Input In [12], in <module>
----> 1 from main import *
      2 npart = 5
 
File ~/main.py:91, in <module>
     88 latmin = 45.977778
     89 latmax = 45.931389
---> 91 npart = n  # number of particles to be released
     92 for i in range(n):
     93     lon = lonmin + random.uniform(0, 1)*(lonmax-lonmin)

NameError: name 'n' is not defined

The main code that I am trying to control looks like this. main.py. Here I type in 5 so the code will run without error. I would like to call it a variable n here. In the separate code define n = 5 and reproduce the results.

import random
x = []
y = []

lonmin = -63.226111
lonmax = -63.229722
latmin = 45.977778
latmax = 45.931389


npart = 5  # number of particles to be released
for i in range(5):
    lon = lonmin + random.uniform(0, 1)*(lonmax-lonmin)
    x.append(lon)
    lat = latmin + random.uniform(0, 1)*(latmax-latmin)
    y.append(lat)

print(x, y)
4
  • 1
    Would it be possible to have a reproducible example to help you? Seeing the code that is failing would be very helpful. Thanks! Commented Feb 10, 2022 at 17:17
  • Thank you, I had edited my question to include the code I would like to control. I had set npart = 5 here so the code will run and produce an array. But I would like to be able to control npart from a separate script so I can change npart from there and never have to touch the main code. My goal would be for npart and the for loop to share the same variable. Commented Feb 10, 2022 at 17:28
  • So, did you initialized variable n? Commented Feb 10, 2022 at 17:28
  • I have tried setting n = n in the main. In the separate script i would import main and on the next line type n=5, but would get the same error. I am just a little confused on the correct way to initialize n in main.py. Commented Feb 10, 2022 at 17:35

2 Answers 2

1

If I understand what you're trying to do correctly, the error appears to be in your main.py file, you don't define the variable n. You need to add n = 10, or whatever, to that file.

Update
I think what you want to do is import a function from main.py, rather than a variable. So it would look something like this:

in main.py:

def makeCalculations(n):
    x = []
    y = []
    for i in range(n):
        lon = lonmin + random.uniform(0, 1)*(lonmax-lonmin)
        lat = latmin + random.uniform(0, 1)*(latmax-latmin)
        x.append(lon)
        y.append(lat)
    return x,y

in other python file:

from main import *
n = 5
x,y = makeCalculations(n)

In general, it's a bad idea to use import * in your scripts, because it can lead to different things having the same name, causing confusing errors. To be safer I think you would be better off saying what you want to import from main.py explicitly:

from main import latmin, latmax #and any other variable you want
from main import makeCalculations #and any other function you want
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. What I want to do is type n = 10 in the separate python script and have n just be a variable in the main script that can be manipulated. How would I define n in the main script such that I can change n in the separate code?
if i define n in the main code def n(): return n, it removes the error I was getting. What remains unresolved now is n in the for loop. for i in range(n):. I am wondering what to write so that it can be recognized in the for loop as well.
Hmm ok, when you import main, python needs to be able to run all of main.py without tripping on any errors. So having an undefined variable is not going to work. I think what you want to do is import a function from main, instead of a variable. I will add to my answer what I think that would look like.
Thank you this worked for me. I wish I could give a thumbs up but I dont have enough points to do that yet.
Glad it helped. If your question is answered, you should be able to confirm it as a solution, even if you don't have enough points to vote yet.
0

It's because you never declared the variable n in your file main.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.