[Tutor] Modules and variable usage in functions from other files
Richard Bekenstein
rbekenstein at mail.com
Wed Dec 23 20:45:50 EST 2015
Dear All,
I have a main .py script which contains code that occasionally calls some
functions. This script calls in these defined functions from other .py
files in the same directory as the main .py. However, even after importing
the functions from the different .py files, I get error messages, such as
"global name 'np' is not defined" even though I define "import numpy as
np" initially in my main .py script.
To show some detail, I import some modules at the beggining of my main .py
script as such:
from scipy.constants import m_p,G,k
import numpy as np
from math import cos, pi, floor
import matplotlib.pyplot as plt
from findall import findall
from pylab import *
import sys
import timeit
from copy import copy
from array import array
from bol_heatFTCS_nonadaptive import heatFTCS #this is my first
user-defined function that calls the defined function 'heatFTCS' from file
'bol_heatFTCS_nonadaptive.py'
from bol_runge_kutta_functions import dy1_dt,dy2_dt,dy3_dt,dy5_dt
#this is my second user-defined function that calls the defined functions
'dyi_dt's from file 'bol_runge_kutta_functions.py'
from bol_runge_kutta_evl_nonadaptive import runge_kutta_evl #this is
my first user-defined function that calls the defined function
'runge_kutta_evl' from file 'bol_runge_kutta_evl_nonadaptive.py'
m_range,p_range,T_range,r_range,rho_range = np.loadtxt('(there are
some other directories here that don't matter for this
discussion)/planetary_structure_params.txt', unpack=True,
usecols=[0,1,2,3,4]) #imports some data to 5 arrays
When I run this main.py, I get an error message from the imported
'runge_kutta_evl' function that numpy is not defined. Why is it that even
though I have it defined in my script it does not recognize it in my
function called to the script? It turns out that even if I then state
'import numpy as np' in my 'runge_kutta_evl' function, I then another
error that 'r_range' is not defined. But aren't 'r_range' and 'np'
supposed to be accesible to all called functions in the main .py?
Thank you,
R.B.
More information about the Tutor
mailing list