Ken Kinder
Ken Kinder

Reputation: 13140

Python: If there are multiple egg versions of the same package installed, how do I import specifically the version I need?

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?

Upvotes: 8

Views: 1731

Answers (1)

samplebias
samplebias

Reputation: 37919

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'

Upvotes: 16

Related Questions