Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Error: No module named pytz after using easy_install

Today is my first day at Python and have been going through problems. One that I was working on was, "Write a short program which extracts the current date and time from the operating system and prints it on screen in the following format: day, month, year, current time in GMT. Demonstrate that it works."

I was going to use pytz, so used easy_install pytz This installed it in my site-packages (pytz-2012d-py2.7.egg)

Is this the correct directory for me to be able to import the module?

In my python shell i use from pytz import timezone I get, "ImportError: No module named pytz"

Any ideas? Thanks in advance

like image 915
scrayon Avatar asked Sep 22 '12 03:09

scrayon


People also ask

Is pytz included in Python?

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference (datetime. tzinfo).

How do you use pytz in lambda function?

You need to install the pytz package so it's available for your lambda. The way you do this is having pip install it into the directory you are going to zip and upload to AWS (i.e. peered with the file containing your lambda function). Then when you zip it up and upload it, it will be available.


3 Answers

time module can also help here.. UTC is Coordinated Universal Time (formerly known as Greenwich Mean Time, or GMT)

In [18]: import time

In [19]: time.gmtime()
Out[19]: time.struct_time(tm_year=2012, tm_mon=9, tm_mday=22, tm_hour=3, tm_min=37, tm_sec=15, tm_wday=5, tm_yday=266, tm_isdst=0)

In [20]: x = time.gmtime()

In [21]: x.tm_year
Out[21]: 2012

In [22]: x.tm_mon
Out[22]: 9

In [23]: x.tm_mday
Out[23]: 22

In [24]: x.tm_hour
Out[24]: 3

Also can you Check the logs while you installed pytz with the below ones...

C:\>easy_install pytz
Searching for pytz
Reading http://pypi.python.org/simple/pytz/
Reading http://pytz.sourceforge.net
Reading http://sourceforge.net/project/showfiles.php?group_id=79122
Reading http://www.stuartbishop.net/Software/pytz
Reading http://sourceforge.net/projects/pytz/
Best match: pytz 2012d
Downloading http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d-py2.7.egg#md5=
e6f9219ae6eff242f13c6700413df69e
Processing pytz-2012d-py2.7.egg
Moving pytz-2012d-py2.7.egg to c:\python27\lib\site-packages
Adding pytz 2012d to easy-install.pth file

Installed c:\python27\lib\site-packages\pytz-2012d-py2.7.egg
Processing dependencies for pytz
Finished processing dependencies for pytz

C:\>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytz
>>> from datetime import datetime, timedelta
>>> utc = pytz.utc
>>> utc.zone
'UTC'
like image 149
avasal Avatar answered Oct 12 '22 02:10

avasal


For what it's worth, the answer to the fundamental problem here is that the pytz installation process didn't actually extract the ".egg" file (at least, this is what I noticed with a very similar issue.)

You may consider going into the site-packages folder and extracting it yourself.

like image 42
Noon Silk Avatar answered Oct 12 '22 01:10

Noon Silk


It is important if you are using python v2 or python v3 - it has separate easy_install package! In debian there are: python-pip python3-pip

and then easy_install easy_install3

If you use wrong version of easy_install you will be updating wrong libraries.

like image 43
Martin Avatar answered Oct 12 '22 01:10

Martin