73

I would like to use urlparse, but Python 3 is not finding the module.

I do import urlparse, but it gives me this error

ImportError: no 'module' named urlparse
metatoaster
  • 17,419
  • 5
  • 55
  • 66
Mohamed Mahdi
  • 1,286
  • 3
  • 11
  • 13

2 Answers2

87

The urlparse in Python 2.7.11 was renamed to urllib.parse in Python 3. So, if you have a code such from urlparse import urljoin, I suggest you change it to from urllib.parse import urljoin

Abhijit
  • 1,728
  • 1
  • 14
  • 25
54

As noted in urlparse's documentation:

Note The urlparse module is renamed to urllib.parse in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

I.e., just use urllib.parse instead:

import urllib.parse
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • how do i can install 2to3 tool and use it? – Mohamed Mahdi Jan 03 '18 at 07:23
  • 1
    @MohamedMahdi it's provided by the `python3-tools` rpm. Once installed, just run `2to3 myscript.py`. – Mureinik Jan 03 '18 at 07:28
  • i fount the 2to3 file in python34/tools/scripts and i run it. it gave me " at least on file required. then i wrote this command " 2to3 myscript.py, but it told me that it can't open myscript.py as there is no such a file or directory! – Mohamed Mahdi Jan 03 '18 at 07:48