Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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.

You can actually specify the location of zoneinfo by setting the environment variable PYTZ_TZDATADIR:

export PYTZ_TZDATADIR=/usr/share/zoneinfo

The compressed zoneinfo files are read only if PYTZ_TZDATADIR is not set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top