Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify a library in python and use it? [closed]

To be more specific, I want to modify some functions in scikit-learn and import it to python. But I do not know how to work.

I tried to modify .py files directly from where the sklearn stores in my local directory, but there are some files I could not open to modify, such as those with .cp36-win_amd64.

Any advice would be helpful!

like image 350
TripleZ Avatar asked Mar 14 '18 14:03

TripleZ


People also ask

Can we modify Python library?

You should never edit an installed package. Instead, install a forked version of package. If you need to edit the code frequently, DO NOT install the package via pip install something and edit the code in '.../site_packages/...' Put your changes in a version control system, and tell pip to install it explicitly.

How do I change libraries in Python?

Just rename the scikit-learn root folder (the folder that contains the setup.py file) to something like myScikitLearn). Install it using "pip install -e myScikitLearn/".

How do I reimport a package in Python?

We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it. Now we can directly call this function.


2 Answers

Delete the scikit-learn package, clone the version you are interested in from github. Go to the directory where you've cloned it and run:

pip3 install -e ./

This will install the package in development mode. Any changes you make will take effect the next time you run your application.

like image 109
Alexander Ejbekov Avatar answered Sep 17 '22 21:09

Alexander Ejbekov


Modifying source files is not a good idea...especially if you want to use the "unmodified" version later on. My advice would to:

  • Checkout the Scikit-learn repository on github
  • Give it a custom name (e.g. myScikitLearn)
  • Install it using pip install -e
  • All modifications made to myScikitLearn source files can then be used immediately in your code
like image 23
ma3oun Avatar answered Sep 20 '22 21:09

ma3oun