First call to pytz.timezone is slow in virtualenv

2019-04-22 17:18发布

问题:

I have installed pytz (v2013.8, but it happens in 2013.b, 2011k) in a virtualenv. The first call to

pytz.timezone("US/Eastern")

takes about 4 seconds. In a regular environment this is essentially instantaneous.

Does anyone have a trick to get this to run faster?

回答1:

I actually came across the answer by playing around and looking at the source code. Since it gets its timezone settings from within the egg and the first call to timezone has to check that all the timezone files exist, the first call could be slow depending on how the os has to find those files. If pytz is installed using apt-get install python-tz then then call hits uncompressed files and is very fast. If it is installed using easy_install pytz then it hits one compressed file over and over again and is slower.

So the solution is to uncompress it. Luckily pip has a handy command.

tl;dr

pip unzip pytz


回答2:

It seems like in Windows just delete pytz-2013.9-py2.7.egg



回答3:

I had a hard time running

pip unzip pytz

as it says it cannot find the package. A workaround that I found which works was to edit the setup.py file and replacing

zip_safe=False

. (Set it to False). Then run the installer again:

python setup.py install

This solved my issue of slow loading time as well.