I have a script called "custom_functions.py" with functions I've written to simplify my work. Each custom function calls different libraries. "custom_functions.py" looks something like this:
# This is custom_functions.py
import pandas as pd
import numpy as np
def create_df():
return pd.DataFrame({'a': [1, 2, 3]})
def create_array():
return np.array([1, 2, 3])
Over the last few months, as I've added functions to custom_functions.py, there are more and more libraries I need to import for me to be able to call the script into another file (let's call it "main.py"). It seems inefficient/unnecessary to load in the libraries for all the functions when I end up only needing one.
Is there a way to call only e.g. create_array without needing to also load in the libraries required for create_df? It'd be ideal, for example, if I could remove all library calls in custom_functions.py and only import the necessary libraries in main.py before I call in a specific function from custom_functions.py., e.g.:
# This is the proposed custom_functions.py
def create_df():
return pd.DataFrame({'a': [1, 2, 3]})
def create_array():
return np.array([1, 2, 3])
and
# This is the proposed main.py
import numpy as np
from custom_functions import create_array
The above code throws an error (NameError: name "np" is not defined). Is the only solution to break custom_functions.py into separate scripts and just load all associated libraries every time I load custom_functions into main.py?
In case it's helpful, I'm using Python 3.6.5 with Anaconda on a Windows 10 machine.
Thanks for your help!