This project has moved and is read-only. For the latest updates, please go here.

Handle *-nspkg.pth files


Some packages on Python <3.3 create .pth files to emulate namespace packages. These files typically look like:
import sys,types,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('$(NAME)',)); ie = os.path.exists(os.path.join(p,'')); m = not ie and sys.modules.setdefault('$(NAME)',types.ModuleType('$(NAME)')); mp = (m or []) and m.__dict__.setdefault('__path__',[]); (p not in mp) and mp.append(p)
The result is that the folder $(NAME) should be treated as if it includes an empty file.

Because we don't handle arbitrary code in pth files, we will ignore the packages. We should attempt to detect this pattern and include the path(s) it references.


Zooba wrote Nov 27, 2013 at 7:05 PM

The format of this string comes from setuptools, so it is going to be consistent enough that we can parse it.

Zooba wrote Dec 17, 2014 at 3:50 PM

See also:

coopr and pyutilib are good examples of namespaces that use this trick extensively and do not work when an empty file is added instead.

Also, there is the potential for the code above to appear multiple times in the same .pth file.

Zooba wrote Jan 21, 2015 at 9:41 PM

Best detection for these will be the -nspkg in the filename and then a search for all instances of sys\.modules\.setdefault\(['"](\w+)