8

Say, for example that FooPackage-1.1 and FooPackage-1.2 are both installed in dist-packages as eggs. How do I import the one I need?

Wooble
  • 87,717
  • 12
  • 108
  • 131
Ken Kinder
  • 12,654
  • 6
  • 50
  • 70

1 Answers1

16

You can use pkg_resources to specify your requirements at import time:

import pkg_resources
pkg_resources.require('FooPackage==1.2')
import FooPackage

For example:

% easy_install simplejson==2.1.3
% easy_install simplejson==2.1.2

pkg_resources.require('simplejson==2.1.2')
import simplejson
assert simplejson.__version__ == '2.1.2'
samplebias
  • 37,113
  • 6
  • 107
  • 103
  • 1
    +1, good answer :) In addition to the answer. If you always want to make sure you've got the correct version, try using virtualenv with your own packages. – Wolph Mar 10 '11 at 21:21
  • Doesn't work for me; I get a verison conflict: `pkg_resources.VersionConflict: (SQLAlchemy 1.1.0b1.dev0 (/.virtual/lib/python2.7/site-packages/SQLAlchemy-1.1.0b1.dev0-py2.7-linux-x86_64.egg), Requirement.parse('sqlalchemy==0.8.7')) ` – EoghanM May 06 '16 at 20:46