Issue
I created a Jupter Notebook file to work on a project and carry out data analysis on a given dataset. In the notebook, I downloaded/cleaned the data and implemented different regression algorithms. The Jupyter notebook file has become very long (rows) and is now slow to execute / scroll through the document (lag).
I have decided to move the project over to the Spyder IDE. I want to split the python code into different classes to make it easier to run and interpret/understand my code (different functionality in each class).
For example:
- I have a create_dataframe.py file which contains a class to download the dataset and create the dataframe.
- I have a clean_dataframe.py file which contains a class to clean and carry out initial data visualisation.
- Both these classes are imported to the main.py file and called from there.
In all 3 files, I have had to import the pandas/numpy packages.
My question is what is the accepted convention when importing packages in different classes? Is it normal practise to import the same packages in different classes or is there a way to import the packages on the main.py file and then have the package accessible in all other files?
Solution
It is normal and accepted to re-import the same module in each file where it's used. This helps your code be more explicit about where the things in its namespace originate - better to import numpy
than to from myOtherModule import numpy
. In general, the only things you want to import from a file or module are things that originate in that file or module. Any other shared resources should probably be imported from the shared source directly.
This doesn't cause any memory problems, because the python interpreter only ever loads any given module once (unless you use importlib
or similar to mess with that behavior or reload a module explicitly). The first time a module is imported, it's cached in sys.modules
, and subsequent imports check sys.modules
to see if the module has been loaded, before trying to load it again.
For further reading on this topic, see python's documentation of the import system.
Answered By - Green Cloak Guy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.