pypa/pip — Issue #449: PIP creates invalid .pth when namespace package is unicode
Author: jaraco
Created: 2012-02-06 19:16:25+00:00
State: open

When using PIP to install a namespace package which has unicode literals for the package name, it fails with a TypeError.

```
jaraco@hideaki:~$ virtualenv env
New python executable in env/bin/python2.7
Also creating executable in env/bin/python
Installing setuptools............done.
Installing pip...............done.
jaraco@hideaki:~$ env/bin/pip install jaraco.util==5.0.1
Downloading/unpacking jaraco.util==5.0.1
Downloading jaraco.util-5.0.1.zip (76Kb): 76Kb downloaded
Running setup.py egg_info for package jaraco.util
zip_safe flag not set; analyzing archive contents...

Installed /home/jaraco/env/build/jaraco.util/hgtools-1.0.1-py2.7.egg
/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3'
warnings.warn(msg)
/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3_exclude_fixers'
warnings.warn(msg)

Installing collected packages: jaraco.util
Running setup.py install for jaraco.util
/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3'
warnings.warn(msg)
/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3_exclude_fixers'
warnings.warn(msg)
Skipping installation of /home/jaraco/env/lib/python2.7/site-packages/jaraco/__init__.py (namespace package)

Installing /home/jaraco/env/lib/python2.7/site-packages/jaraco.util-5.0.1-py2.7-nspkg.pth
Installing roll-dice script to /home/jaraco/env/bin
Installing calc-prorate script to /home/jaraco/env/bin
Successfully installed jaraco.util
Cleaning up...
jaraco@hideaki:~$ env/bin/python
Traceback (most recent call last):
File "/home/jaraco/env/lib/python2.7/site.py", line 677, in
main()
File "/home/jaraco/env/lib/python2.7/site.py", line 657, in main
paths_in_sys = addsitepackages(paths_in_sys)
File "/home/jaraco/env/lib/python2.7/site.py", line 273, in addsitepackages
addsitedir(sitedir, known_paths)
File "/home/jaraco/env/lib/python2.7/site.py", line 203, in addsitedir
addpackage(sitedir, name, known_paths)
File "/home/jaraco/env/lib/python2.7/site.py", line 172, in addpackage
exec(line)
File "", line 1, in
TypeError: module.__init__() argument 1 must be string, not unicode
```

By troubleshooting site.py, it seems the error occurs when it tries to process the .pth file for jaraco.util:

```
jaraco@hideaki:~$ cat env/lib/python2.7/site-packages/jaraco.util-5.0.1-py2.7-nspkg.pth
import sys,new,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *(u'jaraco',)); ie = os.path.exists(os.path.join(p,'__init__.py')); m = not ie and sys.modules.setdefault(u'jaraco',new.module(u'jaraco')); mp = (m or []) and m.__dict__.setdefault('__path__',[]); (p not in mp) and mp.append(p)
```

As you can see, the generated .pth file is using unicode literals, which causes the failure when importing site.py. These errors do not occur with setuptools or distribute for the same package.

Replacing namespace_packages=['jaraco'] with namespace_packages=[bytes('jaraco')] in the setup.py works around the issue.

PIP should probably try to generate .pth files that don't corrupt the environment even if the namespace packages are specified using unicode strings.