leon-thomm / Ryven

Flow-based visual scripting for Python
https://ryven.org
MIT License
3.76k stars 439 forks source link

Running Ryven from python script fails if node packages are passed as arguments #179

Open pfjarschel opened 8 months ago

pfjarschel commented 8 months ago

Got a simple one here:

If Ryven is run from a python script, AND some node packages are passed as arguments, AND the startup screen is enabled, the startup fails due to some Path/str confusion.

To reproduce, I give you a simple code:

import os
import ryven

# File paths
main_path = os.path.dirname(os.path.realpath(__file__))

ryven.run_ryven(qt_api='pyside6', 
                show_dialog=True, 
                nodes=['std',
                f'{main_path}/ryv_pckg',
                f'{main_path}/ryv_PFJVirtualSG'], 
                project=f'{main_path}/PFJVSG.json',
)

The two node packages are personal packages I am testing. The problem is also present if only 'std' is used, for example.

The error shown is this:

File "C:\Users\pfjar\AppData\Local\Programs\Python\Python311\Lib\site-packages\ryven\gui\startup_dialog\StartupDialog.py", line 647, in update_packages_lists
    if pkg_path.stem == node_item.text():
       ^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'stem'

` It is clear that the problem arises because the script treats the node packages as Paths, but when Ryven is run from a python script (and maybe from the command line?) the passed packages are only strings, not Paths yet. Now, I dug through the code, and couldn't easily find the place where string paths are converted to Paths, when packages are passed as arguments. Not sure if there is a place? So i have come up with a simple solution that works, but a better one can probably be easily implemented.

Proposed solution: Include the following lines before each pkg_path.stem call in StartupDialog.py (there are 4):

if isinstance(pkg_path, str):
    pkg_path = pathlib.Path(pkg_path)

Hope this helps leon-thomm and anyone having this issue!

KerlinerBindl commented 3 months ago

Thank you very much! that helped :-)