1

I am using a python package called networkx. I write some customized functions in this package. It works well as long as I do not update this package, or change the conda environment. But I am sure this is not the right way to do it. My question is quite similiar to this one but I am using conda .

I am wondering can I use conda to achieve similiar result? What does the'develop mode' mean? Is it just of folder of this package? Also, if I install it in my project foder, what will happen to the packages that rely on this package? When I install some other packages, they will also install this networkx (such as omnx). So when I import networkx as nx, which networkx I am importing?

A step by step guide is really appreciated!

Xudong
  • 441
  • 5
  • 16
  • What is the reason for stuffing your functions into an existing package instead of writing your own module/package which leverages them? – vaizki Dec 08 '21 at 18:43
  • Hi, because my function relies on some existing function inside this package. It is more like a 'middle function'. So it is more convenient for me to modify inside the original package. – Xudong Dec 08 '21 at 18:48
  • Seems to me like you should just fork the networkx package and build your additions on top (or you could always contribute to networkx and make pull requests). If you want to monkey patch you can look here for some options https://stackoverflow.com/questions/68637641/monkey-patch-add-new-class-and-functions-to-existing-module/68638214#68638214 – Andrew Holmgren Dec 08 '21 at 19:05
  • Yes, I am also thinking about fork the package. But as I mentioned, if I install some packages that rely on networkx, will they install a new 'networkx', or will they use the one I forked? I don't think the conda will be 'aware' that I already put a package in my own directory? – Xudong Dec 08 '21 at 19:10
  • Actually, I don't care which networkx the other packages used. I just hope when I import netwokx as nx, I am importing the one I modified. – Xudong Dec 08 '21 at 19:12
  • You should use Python virtual environments so that you can install packages only for the program you are running. This will free you from package version etc conflicts also. – vaizki Dec 08 '21 at 19:22

1 Answers1

0

If you really really have to do this, one way is to 'monkey-patch' the module from your code right after loading it.

import networkx as nx

def nx_myfunc():
  print("Monkey in the house!")

nx.myfunc = nx_myfunc

Now the module is loaded and your function is available as nx.myfunc() but the module installation has not been modified. So it can be upgraded etc but of course your monkey-patch might not work with a newer version.

At the risk of repeating myself, there are very few scenarios where this is the right way to go.

vaizki
  • 1,678
  • 1
  • 9
  • 12
  • Hi, the problem is I have to put this function inside the networkx. Because there are func_1 and func_2 already inside the networkx. I wrote a func_3. This fun_3 needs to call func_1. And func_2 will call func_3. Then in my project, I am using func_2( ) to get the output. – Xudong Dec 08 '21 at 19:16
  • And that is exactly what the code does. It inserts a new function from the outside into an already loaded module. Yes, Python is dynamic enough for this to be as easy as it looks. But if func_2 already exists and needs to call this new function, then you need to override func_2 with your own version. You can do it exactly like adding a new function, just replaces the original one. – vaizki Dec 08 '21 at 19:19