Should python imports take this long?

2019-06-21 03:57发布

问题:

For the following command

%time python test.py

on this script, test.py

import numpy as np
from math import * 
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
from scipy import stats

I get the output:

real    0m1.933s
user    0m1.322s
sys     0m0.282s

Is there something wrong? Or is this how long imports should take?

回答1:

Some modules initialize when you use them, while others initialize everything once you start it up. Matplotlib is one of those modules.

Since matplotlib is a huge package that includes a whole lot of functionality, I'm not surprised that it takes this long, although it can get annoying.

So, in answer to your question, yes to some.

If you want a "solution" to your problem, you might want to import matplotlib only when you're going to use it, or have a loading screen / print at the beginning of your program.