Python 3 issue with urllib3 (local) import

The problem with broken import occurs when Python 3.3.0 (virtualenv, Ubuntu 12.04 LTS).

In Python 2.7.4 (virtualenv, same machine) everything runs smoothly.

The traceback:

```
File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests-1.2.3-py3.3.egg/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
ImportError: cannot import name urllib3
```


Weird, this looks like your install is broken. Try reinstalling Requests in the same virtualenv?


It seems that the following causes the problem.

# Directory structure

├── src
│   └── myapp
│       │   ├── test_requests.py
│       │   └── utils.py
└── test_request.py

# `test_requests.py` contents

Both `src.myapp.test_requests` and `test_requests` modules have the following content:

import requests

# OK tests

python test_requests.py

# Fail tests

python src/myapp/test_requests.py

# Solution

Use absolute imports?

Otherwise, I have to rename my `utils.py` package which now resides in `src/myapp/` to something else. Then it would work.


Uh, isn't your problem about importing `urllib3`, not `utils`?


No, actually, I'm trying to import the requests, but it seems to be impossible if the package, in which the import actually takes place, already has a module named `utils`.

I though it's quite clear from the directory structure I have given as an example.

I think it's barely requests problem.


Oh, I agree that it certainly could be a problem. =) It's just not the problem identified by your traceback, which complains about failing to import urllib3. =)


Ah, that. Yep, could be more detailed indeed. The more detailed traceback follows:

```
Traceback (most recent call last):
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/starbase-0.2-py3.3.egg/starbase/client/http/__init__.py", line 9, in <module>
    import requests
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/__init__.py", line 58, in <module>
    from . import utils
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/utils.py", line 23, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/compat.py", line 7, in <module>
    from .packages import charade as chardet
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
ImportError: cannot import name urllib3
```

Thus, the urllib3 comes as last, but the problem is causes by relative import of `utils` module.


See, I think `utils` is definitely not your problem. The import of `utils` correctly finds the Requests version. Can you print the files in `home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/packages`?


```
├── charade
│   ├── big5freq.py
│   ├── big5prober.py
│   ├── chardistribution.py
│   ├── charsetgroupprober.py
│   ├── charsetprober.py
│   ├── codingstatemachine.py
│   ├── compat.py
│   ├── constants.py
│   ├── cp949prober.py
│   ├── escprober.py
│   ├── escsm.py
│   ├── eucjpprober.py
│   ├── euckrfreq.py
│   ├── euckrprober.py
│   ├── euctwfreq.py
│   ├── euctwprober.py
│   ├── gb2312freq.py
│   ├── gb2312prober.py
│   ├── hebrewprober.py
│   ├── __init__.py
│   ├── jisfreq.py
│   ├── jpcntx.py
│   ├── langbulgarianmodel.py
│   ├── langcyrillicmodel.py
│   ├── langgreekmodel.py
│   ├── langhebrewmodel.py
│   ├── langhungarianmodel.py
│   ├── langthaimodel.py
│   ├── latin1prober.py
│   ├── mbcharsetprober.py
│   ├── mbcsgroupprober.py
│   ├── mbcssm.py
│   ├── __pycache__
│   │   ├── big5freq.cpython-33.pyc
│   │   ├── big5prober.cpython-33.pyc
│   │   ├── chardistribution.cpython-33.pyc
│   │   ├── charsetgroupprober.cpython-33.pyc
│   │   ├── charsetprober.cpython-33.pyc
│   │   ├── codingstatemachine.cpython-33.pyc
│   │   ├── compat.cpython-33.pyc
│   │   ├── constants.cpython-33.pyc
│   │   ├── cp949prober.cpython-33.pyc
│   │   ├── escprober.cpython-33.pyc
│   │   ├── escsm.cpython-33.pyc
│   │   ├── eucjpprober.cpython-33.pyc
│   │   ├── euckrfreq.cpython-33.pyc
│   │   ├── euckrprober.cpython-33.pyc
│   │   ├── euctwfreq.cpython-33.pyc
│   │   ├── euctwprober.cpython-33.pyc
│   │   ├── gb2312freq.cpython-33.pyc
│   │   ├── gb2312prober.cpython-33.pyc
│   │   ├── hebrewprober.cpython-33.pyc
│   │   ├── __init__.cpython-33.pyc
│   │   ├── jisfreq.cpython-33.pyc
│   │   ├── jpcntx.cpython-33.pyc
│   │   ├── langbulgarianmodel.cpython-33.pyc
│   │   ├── langcyrillicmodel.cpython-33.pyc
│   │   ├── langgreekmodel.cpython-33.pyc
│   │   ├── langhebrewmodel.cpython-33.pyc
│   │   ├── langhungarianmodel.cpython-33.pyc
│   │   ├── langthaimodel.cpython-33.pyc
│   │   ├── latin1prober.cpython-33.pyc
│   │   ├── mbcharsetprober.cpython-33.pyc
│   │   ├── mbcsgroupprober.cpython-33.pyc
│   │   ├── mbcssm.cpython-33.pyc
│   │   ├── sbcharsetprober.cpython-33.pyc
│   │   ├── sbcsgroupprober.cpython-33.pyc
│   │   ├── sjisprober.cpython-33.pyc
│   │   ├── universaldetector.cpython-33.pyc
│   │   └── utf8prober.cpython-33.pyc
│   ├── sbcharsetprober.py
│   ├── sbcsgroupprober.py
│   ├── sjisprober.py
│   ├── universaldetector.py
│   └── utf8prober.py
├── __init__.py
├── __pycache__
│   └── __init__.cpython-33.pyc
└── urllib3
    ├── _collections.py
    ├── connectionpool.py
    ├── contrib
    │   ├── __init__.py
    │   ├── ntlmpool.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-33.pyc
    │   │   └── pyopenssl.cpython-33.pyc
    │   └── pyopenssl.py
    ├── exceptions.py
    ├── filepost.py
    ├── __init__.py
    ├── packages
    │   ├── __init__.py
    │   ├── ordered_dict.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-33.pyc
    │   │   ├── ordered_dict.cpython-33.pyc
    │   │   └── six.cpython-33.pyc
    │   ├── six.py
    │   └── ssl_match_hostname
    │       ├── __init__.py
    │       └── __pycache__
    │           └── __init__.cpython-33.pyc
    ├── poolmanager.py
    ├── __pycache__
    │   ├── _collections.cpython-33.pyc
    │   ├── connectionpool.cpython-33.pyc
    │   ├── exceptions.cpython-33.pyc
    │   ├── filepost.cpython-33.pyc
    │   ├── __init__.cpython-33.pyc
    │   ├── poolmanager.cpython-33.pyc
    │   ├── request.cpython-33.pyc
    │   ├── response.cpython-33.pyc
    │   └── util.cpython-33.pyc
    ├── request.py
    ├── response.py
    └── util.py

11 directories, 107 files
```


That's perplexing. Can you try uninstalling and reinstalling Requests?


Reinstalling requests doesn't solve the problem.


From where and how are you installing it?


Btw, when installing the requests the following problem occurs.

Running setup.py install for requests

```
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/packages/urllib3/contrib/ntlmpool.py", line 38
    """
     ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 130-132: truncated \uXXXX escape
```

Like any other package, I install the requests using pip install in virtualenv.


Hmm. I'm a little confused, because I can cleanly import Requests 1.2.3 in 3.3.2. I wonder if the interpreter changed its behaviour.


It only affects Python 3.3.0. Earlier versions, including 2.6.8, 2.7.3 and 3.2.3 are not affected.


Does it affect later versions of 3.3?


Just installed the most recent version 3.3.2.

The full traceback:

```
Traceback (most recent call last):
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/starbase-0.2-py3.3.egg/starbase/client/http/__init__.py", line 10, in <module>
    import requests
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/__init__.py", line 58, in <module>
    from . import utils
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/utils.py", line 23, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/compat.py", line 7, in <module>
    from .packages import charade as chardet
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/packages/urllib3/__init__.py", line 16, in <module>
    from .connectionpool import (
  File "/home/me/.virtualenvs/starbase3/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 18, in <module>
    from httplib import HTTPConnection, HTTPException
ImportError: No module named 'httplib'
```


Btw, 3.2.3 - same problem.


I think this error is caused by the directory [`http`](https://github.com/barseghyanartur/starbase/tree/master/src/starbase/client/http) you have in your project.
There is builtin package `http` which urllib3  needs.
But instead it gets yours which doesn't work.


t-8ch: Damn, you're right! `httplib` has been renamed to `http` in Python 3 and thus - the problem. Thanks, man.


@t-8ch is so :metal:


Damn straight he is.


Thanks guys. This issue already has come up on a few occasions.
I'm in favour of a section `Troubleshooting` in the manual.
It should at least contain:
- Import errors due to own `http` module
- Weird server behaviour due to SSL version
- SSL proxy errors due to `https://` in proxy URL.

I'm sure you know some more :smile:


I just downgraded to an old version of urllib3 (from 1.22) and it fixed the problem. The issue came up for me on a fresh install of elasticsearch python client. Fix:

    pip install urllib3==1.15

@radzhome You should really try to find the root cause of the issue.
Downgrading and being stuck on this old version may expose you to security issues and will make it harder to fix errors you encounter.

@radzhome usefull tips, with this I can install awx