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

1

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?

Texas answered 10/3, 2011 at 20:47 Comment(0)
C
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'
Crysta answered 10/3, 2011 at 21:18 Comment(2)
+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.Poppas
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')) Syreetasyria

© 2022 - 2024 — McMap. All rights reserved.