Problem Statement: How to fix the Python error – “Import error: No module named urllib2” ?
A Gentle Introduction
URL stands for Uniform Resource Locator. Basically, URL is used to identify a resource on the internet. In order to fetch an URL from the internet and use the data within the URL, the
urllib2 standard python module was being used in Python2. The
urllib2 module supported a lot of functions and classes that helped the users to open an URL and extract the contents. However, In Python3, the
urllib2 module is not available. Instead, the module is split into several sub-modules like
If an import statement experiences difficulty in successfully importing a module, it raises an ImportError. Commonly, such an issue occurs because of a faulty installation or an invalid path, which will usually raise a ModuleNotFoundError in Python 3.6 and newer versions.
Example: In the following example we see the occurence of the “ModuleNotFoundError: No module named ‘urllib2′”
Now, let’s go ahead and resolve this error!
Solution 1: Use import urllib.<module>
The error occurred in this case because the
urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. Therefore, the import was unable to find any module named
urllib2. Had it been Python 2, then our example would have yielded the expected output. Unfortunately, we are on Python 3 in this case and need to use a different module here.
Let’s see what the official documentation says:
Go through the different modules and determine the function that is to be used. Here we will import the module that contains the required function.
1. If you want to use the
urlopen() function, then you have to import urllib.request module.
from urllib.request import urlopen #fetch the contents of a URL to handler res = urlopen("https://blog.finxter.com/") #read the contents from the handler content = res.read()
2. If you want to use the
urlparse() function, then you have to import urllib.parse module.
from urllib.parse import urlparse # Parse the URL to extract necessary components res = urlparse("https://blog.finxter.com/how-to-fix-unicodedecodeerror-when-reading-csv-file-in-pandas-with-python/") print(res)
ParseResult(scheme='https', netloc='blog.finxter.com', path='/how-to-fix-unicodedecodeerror-when-reading-csv-file-in-pandas-with-python/', params='', query='', fragment='') Process finished with exit code 0
Solution 2: Use Try and Except Block
When you are not sure which version of python is actually in use and want your script to work with Python 2 as well as Python 3, you can include a try and except block.
try: #For Python 2 import urllib2 as <alias> except: # If an error is seen, use the format supported in Python 3 import urllib.<module> as <alias>
Example: If you want to use the urlopen() function, we can have the try and except block, as follows:
try: #For Python 2 import urllib2 as req except: # If an error is seen, use the format supported in Python 3 import urllib.request as req #fetch the contents of a URL to handler res = req.urlopen("https://blog.finxter.com/") #read the contents from the handler content = res.read()
Explanation: Within a
try block, import the module corresponding to Python 2 and give it an alias name. In the above example, we have given an alias name as
req. In the
except block, import the module corresponding to Python3 and give the same alias name as given in the
try block. No matter which import statement is used, the alias name req will be assigned to it. We can use this alias to access the functions in the program.
Solution 3: Using Python 2to3 Tool
If you do not want to make any changes manually to your script and run the script without seeing any error, you can use the in-built 2to3 tool that will automatically change the imports in the Python File.
- 2to3 is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code. The 2to3 will automatically adapt imports when converting your sources to Python 3. You can simply modify your existing file by using the same.
- In order to use 2to3 tools, open your terminal and install the tool with the following command: pip install 2to3
- Once you have successfully installed the tool, open your terminal and follow this syntax to use it:
2to3 -w file_name.py
For more details, refer to the official documentation here.
Let’s say we have a file named sample_py2_file.py with the following contents:
from urlib2 import urlopen # fetch the contents of a URL to handler res = urlib2.urlopen("https://blog.finxter.com/") # read the contents from the handler content = res.read() print(content)
In order to convert this file to Python 3, open the terminal window and execute the below command :
2to3 -w "C:\Users\admin\Desktop\Finxter\venv\Scripts\sample_py2_file.py"
You will see that the tool would convert the file to a Python 3 file as shown below:
(venv) C:\Users\admin\Desktop\Finxter>2to3 -w "C:\Users\admin\Desktop\Finxter\venv\Scripts\sample_py2_file.py" RefactoringTool: Skipping optional fixer: buffer RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: set_literal RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: Refactored C:\Users\admin\Desktop\Finxter\venv\Scripts\sample_py2_file.py --- C:\Users\admin\Desktop\Finxter\venv\Scripts\sample_py2_file.py (original) +++ C:\Users\admin\Desktop\Finxter\venv\Scripts\sample_py2_file.py (refactored) @@ -1,4 +1,4 @@ -import urllib2 +import urllib.request, urllib.error, urllib.parse #fetch the contents of a URL to handler res = urlib2.urlopen("https://blog.finxter.com/") RefactoringTool: Files that were modified: RefactoringTool: C:\Users\admin\Desktop\Finxter\venv\Scripts\sample_py2_file.py
The changes would be made to the file sample_py2_file.py. Also, a backup sample_py2_file.py.bak will be maintained in the location where the file is.
In this tutorial, we have seen different ways to fix the No module named ‘urllib2’ error in cases where you are writing a new script and also in cases where you are making changes to the already existing script. We hope you found this tutorial helpful. If you want to get updates on more such tutorials, stay tuned and subscribe.
- Recommended Reads:
Post Credits: Anusha Pai and Shubham Sayon
To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members: