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'm not sure how to import the local package instead of the already installed one.
create a new virtual environment for testing (or de-install any previously installed, non-modified version of the package) installe the package from the locally created distributable (may be as simple as pip install --no-index --find-links=.. ) run the tests.
Creating Packages Whenever you want to create a package, then you have to include __init__.py file in the directory. You can write code inside or leave it as blank as your wish. It doesn't bothers Python. Create a directory and include a __init__.py file in it to tell Python that the current directory is a package.
The easiest way to do such testing would be to create a virtual environment, and then installing the package in development mode.
Assuming you are on Linux it would look something like this.
$ virtualenv dev_env
$ source dev_env/bin/activate
$ cd ~/project_folder
$ pip install -e .
This workflow would not overwrite the already installed package on your system. Another maybe simpler alternatives would to just use an IDE that handles most of this for you, e.g. PyCharm.
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
You should probably be doing most of your development work in a virtual environment. Your workflow for this could look like:
# activate the virtual environment in ~/vpy
. $HOME/vpy/bin/activate
# install my app and its dependencies
cd $HOME/src/myapp
pip install -e .
# use my forked library instead
cd $HOME/src/forkedlib
pip install -e .
pytest # or whatever tests the forked lib has
# try it out with my application too
cd $HOME/src/myapp
pytest # or whatever tests your app has
myapp
pip install -e
does some magic so that, whenever you import
the module in the library, it gets routed directly to the checked-out source tree, so if you make edits in forkedlib
and then re-run myapp
, you'll see those changes directly.
When you're done, you can pip uninstall forkedlib
and then re-run pip install -e .
to reinstall your application's (declared) dependencies. (Or delete and recreate the virtual environment, if that's easier.)
One way consists in using sys.path().
For example:
import sys
sys.path.insert(0, path/to/module)
In this way, you give priority to a specific path when looking for a module.
This means that the module you want to import will be searched first in path/to/module
and after in the other directories already in sys.path
.
The advantage of this approach is that this new order will hold only inside your script without changing the import order of the other ones.
Note: For development purposes you should use a virtualenv as suggested by @eandersson.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With