What is the best way to easily add functions I make to new projects? I have made a bunch of python functions that I created myself that I use frequently for my projects, but I lose track of where I put them all and their versions become desynced when I change them
Should I just add all these functions to pypi? That seems time consuming, especially since a lot of these are pretty small.
I could also save the function as .py files and add them to the directories I make new projects on and import with a simple import. But the problem with this is if I change something about the original I would need to track down every instance of my function file and replace them with the new file.
I could also use absolute path imports, which I've looked at briefly. They seem pretty annoying for what they are, but that's maybe less of an annoyance than tracking down every copy of the file every time I change it. The other big problem with that is I do a lot of cloud computing, which would break my code every time I transition from running it locally to on the cloud, and force me to copy the code anyway.
Has anyone here run into a similar problem? Have you developed a nice solution to it? Is there an option I haven't thought of? All suggestions welcome!
Here's basic example to create a local package.
root/
├─ mypackage/
│ ├─ __init__.py
│ ├─ func.py
setup.py
__init__.pyfrom .func import foo
setup.pyfrom distutils.core import setup
setup(name="mypackage",
version='1.0',
description='',
packages=["mypackage"],
)
func.pydef foo():
print("foo")
return
Run following in terminal. Activate your project environment beforehand.
cd root
python setup.py sdist
pip install ./dist/mypackage-1.0.tar.gz
Activate your environment first. Then run following in python console:
>>> import mypackage
>>> mypackage.foo()
# foo
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