Search code examples
pythonsetuptoolsdistutilsegg

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?


Solution

  • 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'