0

I want to submit a pull request to a library (imblearn, v.0.3.0) that is included in my python distribution (anaconda 4.3.14) by default. Before submitting, I want to to test my cloned repo. Therefore, I need to reload the module from a different location (cloned repo), instead of same the default location like described here.

Adding the location to the path as first element does not work:

>>> import sys
>>> sys.path.insert(0, 'C:\\my repositories\\imbalanced-learn\\imblearn')

printing the version still gives the result from the version.py in the anaconda folder

>>> from imblearn import version
>>> version.__version__
'0.3.0.dev0'

Is there a non hacky way ?

sophros
  • 14,672
  • 11
  • 46
  • 75
Boern
  • 7,233
  • 5
  • 55
  • 86
  • 1
    Create a new `conda` environment, then `python setup.py install` the package (in the new environment)? – MSeifert May 18 '17 at 13:39
  • thanks ! after creating (`conda create --name imblearnpr`) and activating (`activate imblearnpr`) an environment an error occurs during installing the unmodified repo using `python setup.py install`: `ValueError: '.\\under_sampling\\tests' is not a directory` – Boern May 18 '17 at 14:43
  • Possible duplicate of [How do I unload (reload) a Python module?](http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – stovfl May 18 '17 at 15:25
  • 1
    I'd argue against the duplicate: I dont want to reload the same library, I want import a library with the same name from a different location – Boern May 18 '17 at 18:03

1 Answers1

1

I figured it out:

>>> import os, sys
>>> dir = os.path.dirname(os.path.abspath(os.path.realpath('.')))
>>> libRoot = os.path.join(dir, 'imbalanced-learn') # include parent folder of library
>>> sys.path.insert(0,libRoot) # NOTE: insert at beginning of path array

resulting into

>>> from imblearn import version
>>> version.__version__
'0.3.1.pr'

assuming that the file structure is as follows and e.g. the jupyter notebook is started from the root folder:

root
|- main.py
|- imbalanced-learn/
   |- imblearn/
      |- __init__.py
      |- setup.py
      |- ...
Boern
  • 7,233
  • 5
  • 55
  • 86