Call .py file with arguments

When using <py-script src="/"></py-script> I would like to call it with arguments since can be called from the command line like arg1 arg2.
How can I envoke this in pyscript? <py-script src="/ arg1 arg2"></py-script> doesn’t work.

I don’t believe there is a direct way to do this in PyScript currently - the src attribute of a PyScript tag fetches the content from the specified URL as the Python code to be run. It doesn’t know how to accept additional arguments.

You could potentially open an issue/proposal about it over on GitHub, I don’t think I’ve seen this suggested before. Might stir some interesting discussion.

If refactoring your code just a bit is acceptable, a workaround is to wrap your code in a function:

def main(arg1, arg2):
    #Original here

if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])

And then in PyScript:

#py-env fetches and loads it into the virtual filesystem
    - paths:
        - ./
    from myscript import main
    main(arg1, arg2)

This should preserve the ability to use the script from the command line, while also allowing you to feed arguments to the main function directly.


Great, I can refactor the to fit both. Thanks!

1 Like