7

I have two packages that install different packages with the same name. They are both "packages" in that they have top-level setup.py files which specify package=['foo'] in the setup command.

If I install using distutils.core, the last to be installed overwrites the previous one (but I think wouldn't overwrite unless the .py files all had the same names?). If I install using setuptools, the two packages get installed into different eggs.

One option would be to explicitly set sys.path before importing the package name; this seems "un-pythonic" and rather dirty.

Assuming I have these two identically named packages installed in different eggs from setuptools, how do I specify which is imported?

keflavich
  • 18,278
  • 20
  • 86
  • 118
  • out of curiosity, which packages are they? – Steven Rumbalski Feb 08 '12 at 16:49
  • 2
    it's fairly unpythonic to have 2 packages with the same name installed in the first place, IMO. – Wooble Feb 08 '12 at 16:49
  • http://pypi.python.org/pypi/pydns and http://pypi.python.org/pypi/dnspython come to mind. One uses DNS while other uses dns as name. – Zart Feb 08 '12 at 16:58
  • StevenRumbalski - they were two packages called 'mpfit' included as packages within two of my packages, [agpy](agpy.googlecode.com) and [pyspeckit](pyspeckit.bitbucket.org). Wooble - yeah, fair enough. I still think my question should be answerable (currently, two packages of the same name will give preference to the earlier egg in alphabetical order), but the workaround is to include the "packages" as sub-packages instead. – keflavich Feb 09 '12 at 17:25

2 Answers2

2

Setuptools guide mentions --multi-version (-m) switch that removes package from sys.path completely. You have to use pkg_resources.require('package==version') in your code as early as possible to let it fix sys.path. This advice is what easy_install always prints when one uses -m.

But you can't have both imported at once (unless they're designed to do so using namespace packages).

Zart
  • 1,421
  • 10
  • 18
  • This is a nice solution, but it looks like it only works with the 'develop' command; is there any alternative for the 'install' command? – keflavich Feb 08 '12 at 23:09
  • develop is just a specific kind of in-place install. -m works for install too. I often use "easy_install -zmaxd /output/directory packagename" to fetch package and it's dependencies from pypi and build them all into eggs. Then I install them into virtual env or whatever with "venv/bin/easy_install -f /output/dir packagename", saving tons of time on download/build. pip doesn't support binary eggs though. – Zart Feb 09 '12 at 07:10
  • It does? If I try `python setup.py install -m` or `pip install [name] -m` (or reverse the order of install / -m), I get `error: option -m not recognized`. It only works with `easy_install`, which I guess is a workaround, but what if someone uses pip? – keflavich Feb 09 '12 at 17:23
  • 2
    pip doesn't support neither eggs no multiple-version packages. easy_install comes with setuptools and they were designed before virtualenv was created. So setuptools solves the problem of multiple packages managing .pth file in site-dir while pip simply doesn't bother and relies on virtualenv isolation. With pip you are forced to use two distinct virtualenvs. Also note that "setup.py develop" uses easy_install's machinery and installs each package in it's own versioned directory. "pip install -e ." does the same with pip's machinery and dumps everything into site-packages. – Zart Feb 10 '12 at 10:03
0

I think the best way to workaround, would be to change the name of the toplevel directory, unless other packages depend on that package.

You can do this by altering the setup.py or just change the name folder in site-packages directly. The egg is just meta data.

As far as setting sys.path, it's better to use the site module, by creating a .pth file. When instantiated, any paths located in that file will be added to the "head" of the python path.

Are these two packages different and the naming is a coincidence, or are they simply branches of the same?

James R
  • 4,571
  • 3
  • 30
  • 45