I wrote several graphs with python-nodes in it.
In every Python-node i use my custom functions.
To load my custom package I wrote:
import sys
sys.path.append('ADD MY PATH PACKAGE FOLDER HERE')
from <filename> import <function>
I do not want to set my path package folder in every Python-node before I use my functions.
Is there a way to set my path package folder once before using my functions?
Edit: If I want to change the path I don’t want to modify every Python-node in every graph.
I don’t get it. I still have to add this (template) in every Python-node?
But if I want to change the path I have to modify every Python-node in every graph.
You can set up a template python file so that every (new) instance of the python node contains whatever lines of code you want to include by default, like loading your custom functions.
If you end up changing the location, then yes, you would have to update the location in every node. But that’s standard behavior for a pathed package. You have to tell python where to load those functions from.
Assuming this is an issue with the path changing between users, you can dynamically get the path via programmatic means (look for a Python method to find the users’s app data path). You could also fetch the user’s package path directories and then fetch your package’s path and use that.
If it varies but you want to make things a bit harder to move, you can provide the path as an input to the node.
For individual use you have a couple of options…
Crate or add your path to the PYTHONPATH environment variable
:: See if PYTHONPATH exists
:: cmd.exe
set | findstr PATH
# powershell
gci env: | findstr PATH
Use a semi-colon to delimit multiple paths
Alternately (And this would be my preference as it keeps it to a single instance of python) you can add a .pth file to the site-packages folder
Find the folder with
# site-package library paths
import sysconfig
OUT = sysconfig.get_path("platlib")
If youre up to managing a local package you can store py files in the bin or extras folder and draw from that as a common resource potentially. I believe bumblebee package does that as an example.
It still means you need to start with sys append, but might be a better deployment method if others need to use your code.
Should work for any type of Python install* as it is part of the initialisation when python starts
You can also call it from python by using the site.main() method.
*Embedded python may require you to modify the python[version]._pth file i.e. python39._pth to run import site
You can also place the .pth file in the user space packages folder which is typically %APPDATA%\Python\Python[version]\site-packages on Windows