The most clean way to standardize imports is to make a new file, maybe standard_imports.py
containing the following:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Then, in your main script, you can easily import everything using:
from standard_imports import *
Some other less neat options:
def all_package():
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
return np, pd, plt
np, pd, plt = all_package()
This still makes a function without side side effects in the global scope, but assuming the goal is to use this function in many modules, means adding a package would involve changing every module.
The simplest way is to use the global
keyword, but it is generally bad practice to change the global scope in a function. The global
keyword is considered a code smell usually:
def all_package():
global np, pd, plt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
all_package()