Solutions To Fix ModuleNotFoundError: No module Named ‘pytz’ In Python

ModuleNotFoundError: No module named 'pytz' in Python

This article contains 3 simple ways to fix the the ModuleNotFoundError: No module named ‘pytz’ in Python. If you’re struggling with it, scroll down and find out what works best for you.

What causes the ModuleNotFoundError: No module named ‘pytz’ in Python and ways to fix it

Let’s say you are doing a project that needs to display the date and time based on the time zone. Instead of thinking about how to do it, the Pytz module will be the solution. It is a library for working with time zones in Python. Below are some causes of this error and some ways to fix it.

Pytz is not installed

Most of the reason for the above error is that we don’t have pytz installed, but we import that library at the beginning of our program and use it. For a better overview of this error, observe the example below.

# Pytz package is not installed. But trying to use it
import pytz
from datetime import datetime

# Create a US timezone object
usTimezone = pytz.timezone('US/Central')

# Get current time in the US
usDateTime = datetime.now(usTimezone)
print("Time in US:", usDateTime.strftime("%m/%d/%Y, %H:%M:%S %Z%z"))

Output:

ModuleNotFoundError: No module named 'pytz'

To fix the error in this case, we need to install the Pytz library with the command line:

pip install pytz

If you are using macOS or Linux operating system:

pip3 install pytz

Then you will receive two lines of messages as below:

Installing collected packages: pytz
Successfully installed pytz-2022.5

That means you have successfully installed it. And the error will not appear anymore. Now, use it:

import pytz
from datetime import datetime

# Create a US timezone object
usTimezone = pytz.timezone('US/Central')

# Get current time in the US
usDateTime = datetime.now(usTimezone)
print("Time in the US:", usDateTime.strftime("%m/%d/%Y, %H:%M:%S %Z%z"))

Output:

Time in the US: 10/28/2022, 12:40:09 CDT-0500

The name imported is incorrect

The ModuleNotFoundError error will appear when we import the library’s appellation with the first character capitalized. Like this:

# Import the appellation
import Pytz
from datetime import datetime

Output:

ModuleNotFoundError: No module named 'Pytz'

So the solution here is quite simple, we need to change the character ‘P’ to ‘p’, and we will solve the error in no time. Like this:

# Changed 'P' to 'p'
import pytz
from datetime import datetime

# Create a London timezone object
londonTimezone = pytz.timezone('Europe/London')

# Get current time in London
londonDateTime = datetime.now(londonTimezone)
print("Time in London:", londonDateTime.strftime("%m/%d/%Y, %H:%M:%S %Z%z"))

Output:

Time in London: 10/28/2022, 18:54:35 BST+0100

Using tarball file

If you have installed Pytz using pip but still get the ModuleNotFoundError, try this way.

First, we must delete the Pytz folder in Python’s site-packages directory to avoid conflicts.

To go to the site-packages directory, open the command line interface and type: 

where python

The returned result is the directory containing Python. Like the image below:

ModuleNotFoundError: No module named 'pytz' in Python

Go to the Python310 directory and continue to the Lib\site-packages directory. If a directory named pytz exists here, delete it.

ModuleNotFoundError: No module named 'pytz' in Python

Next, go to this link to download Pytz’s tarball file.

Uncompress it and access the extracted folder.

ModuleNotFoundError: No module named 'pytz' in Python

Here, open a terminal and type:

python setup.py install

If the following message appears:

Finished processing dependencies for pytz==2022.5

You can use Pytz without errors.

import pytz
from datetime import datetime

# Create a Tokyo timezone object
tokyoTimezone = pytz.timezone('Asia/Tokyo')

# Get current time in Tokyo
tokyoDateTime = datetime.now(tokyoTimezone)
print("Time in Tokyo:", tokyoDateTime.strftime("%Y/%m/%d, %H:%M:%S %Z%z"))

Output:

Time in Tokyo: 2022/10/29, 03:33:08 JST+0900

Summary

In summary, we have clarified the cause and how to fix the ModuleNotFoundError: No module named ‘pytz’ in Python. Hopefully, through this article, you will not have to struggle with errors similar to this error anymore.

Happy coding!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *