pythonimportpython-importcode-reuse

Is it possible to reuse import code in Python?


There are several imports that are common between some files in my project. I would like to reuse this code, concentrating it in a unique file and have just one import in the other files. Is it possible?

Or is there another way not to replicate the desired import list in multiple files?


Solution

  • Yes its possible. You can create a Python file with imports and then import that Python file in your code.

    For Eg:

    ImportFile.py

    import pandas as pd
    import numpy as np
    import os
    

    MainCode.py:

    from ImportFile import *
    
    #Here you can use pd,np,os and complete your code
    

    OR

    from ImportFile import pd,np
    
    #And then use pd and np