How to make imported self-written function to see libraries which are in the main file?
I have the main file written in Jupiter Notebook in the root directory with code. This file contains many libraries, including Numpy and the others.
Along with the external libraries, I import in my file self-written library with functions: import functions_lib. Self-written library is also located in the main root.
But the the imported functions (eg functions_lib.xgb_plot_k_fold_roc_curve(X_train, y_train, nfold, seed) don't see the libraries imported from the main file (eg Numpy).
#main file:
import pandas as pd
import numpy as np
import functions_lib
functions_lib.xgb_plot_k_fold_roc_curve(X_train, y_train, nfold, seed)
# improterd library code
def xgb_plot_k_fold_roc_curve(X_train, y_train, nfold, seed):
...
mean_tpr = np.mean(tprs, axis=0)
...
NameError Traceback (most recent call last)
<ipython-input-15-1a9972b490ae> in <module>
2
3
----> 4 functions_lib.xgb_plot_k_fold_roc_curve(X_train, y_train, nfold, seed)
C:\projects\Python-model\functions_lib.py in xgb_plot_k_fold_roc_curve(X_train, y_train, nfold, seed)
39 tprs = []
40 aucs = []
---> 41 mean_fpr = np.linspace(0, 1, 100)
42 plt.figure(figsize=(10,10))
43 i = 0
NameError: name 'np' is not defined
It is useful to understand variables and their scope . Consider np
as a variable. You can use it only in the module where it was defined at the module level. If you load module inside a function as:
def func():
import numpy as np
pass
you can not use np
outside the function since it is a local variable.
It is not like a preprocessor directive #include
in C/C++. Thus insert import numpy as np
in each module where you use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.