I\'m trying to make changes to an existing python module, and then test it locally. What\'s the best way to do this?
I cloned the github module and made changes, but I\'
You can:
I recommend reading this article that explains pretty well modules and packages.
You need to create a module or a package (it doesn't make difference) using the same name as the module/package you want and put it in the same folder as the script that it's going to use it.
This because modules are searched starting from the sys.path variable (where the first element is the script's directory)
import platform
print(platform.system())
Launching it (python your_test_script.py) should return:
Now in the same directory of the previous test script create a file named exactly platform.py with the following contents:
def system():
"""Just a docstring passing by"""
return "We have just overwritten default 'platform' module...\nFeel the force!"
If you launch the script now, you'll notice the output is different:
Better option if your project is more complicated.
From the root of your package (where you'd launch the build):
pip install -e ./
Now you're able to edit code and see the changes in real time..
From The Joy of Packaging:
It puts a link (actually *.pth files) into the python installation to your code, so that your package is installed, but any changes will immediately take effect.
This way all your test code, and client code, etc, can all import your package the usual way.
No sys.path hacking