Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First call to pytz.timezone is slow in virtualenv

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?

like image 268
Mark Avatar asked Dec 10 '13 17:12

Mark


People also ask

Is PYTZ deprecated?

python3-pytz ) package will be deprecated in Fedora 35. Packages in Fedora should use Python standard library modules, such as dateutil.tz and zoneinfo (available since Python 3.9) instead.

What does PYTZ UTC localize do?

localize() is the correct function to use for creating datetime aware objects with an initial fixed datetime value. The resulting datetime aware object will have the original datetime value.

What does virtualenv do?

virtualenv is a tool for creating isolated Python environments containing their own copy of python , pip , and their own place to keep libraries installed from PyPI. It's designed to allow you to work on multiple projects with different dependencies at the same time on the same machine.


2 Answers

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
like image 166
Mark Avatar answered Nov 04 '22 20:11

Mark


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

like image 28
TTT Avatar answered Nov 04 '22 22:11

TTT