How do I 'force' python to use a specific version of a module?

18,849

Solution 1

This is a very messy solution and probably shouldn't be encouraged but I found that if I remove the location of the old version of numpy from the system path I can call the version I want. The specific lines were:-

import sys
sys.path.append('C:/Python27/Lib/site-packages')
sys.path.remove('V:\\\Brian.140\\\Python.2.7.3\\\Lib\\\site-packages')
import numpy

Solution 2

You can also insert the directory to the beginning of the path, so you won't need to remove the old one:

sys.path.insert(1, 'C:/Python27/Lib/site-packages')

That won't work if you've already import your module. You can either import it after the sys.path.insert command, or use importlib.reload(module_name)

Solution 3

Force python to use an older version of module (than what I have installed now) mentions a generic solution:

import pkg_resources
pkg_resources.require("numpy==`1.16.2")  # modified to use specific numpy
import numpy
Share:
18,849
emptyMug
Author by

emptyMug

Updated on June 13, 2022

Comments

  • emptyMug
    emptyMug about 2 years

    I'm new to python so I apologize if this has been answered elsewhere with tags I haven't thought of.

    I'm trying to update numpy from the 1.6 version I have now to 1.8. I've installed numpy in my python site-packages when I call numpy it calls the old 1.6 version. I've tried looking for the root to numpy 1.6 so I can remove it but that leads to :-

    import numpy
    print numpy.__version__
    print numpy.__file__
    >>>
    1.6.2
    V:\Brian.140\Python.2.7.3\lib\site-packages\numpy\__init__.pyc
    

    I've added the folder containing the module to the system path using:-

    sys.path.append('C:/Python27/Lib/site-packages')
    

    and I know this works as I can call other modules in this location with no errors, for example:-

    import wx
    import Bio
    

    and

    import nose
    

    produce no errors. Why is this happening and how can I tell python which version of numpy to use?