Forum Archive

Module runs in stash but not on console

djl

I am trying to understand the use of modules in stash and on the python console. I have a module that runs perfectly in stash with a command like:

Module_name -s d blabla

"-s d blabla" are necessary arguments in this example . I cannot figure out how to do the same thing from the Pythonista console. I can import the module, but any way I try to add the arguments, I get the error:

SyntaxError: invalid syntax
bennr01

@djl said:

"-s d blabla" are necessary arguments in this example . I cannot figure out how to do the same thing from the Pythonista console.

You can long-press the run-Button to add arguments

Regarding the SyntaxError: Maybe you run StaSh in a different python version than the console?

djl

@bennr01

I tried long press to run arguments in a script, and it didn't work. I don't see such an option when using the console command line. I checked python versions and also paths in the console and in stash -- they are the same.

cvp

@djl

djl

@cvp thank you for your help, but my original question dealt with the python console command line, not from within a script. If one runs the module itself using the long_press method, it starts the init script and added arguments do nothing. I am just trying to replicate what happens so easily in stash, in the normal Pythonista framework, and I'm not getting anywhere.

cvp

@djl and this in the console?

import webbrowser;webbrowser.open('pythonista://script_name?action=run&argv=p1&argv=p2')
djl

@cvp there is no script with the functionality of the module invoked in the stash command line. The is a module with sub modules, including init

Without understanding the answer to my original question, I did discover a workaround. If I import a submodule from the main module, I can reproduce expected output by directly calling some of the included functions. I am just surprised that this is so different than simply invoking the main module in stash.

cvp

@djl Ok, sorry, I didn't understand correctly your post, my fault 😢

JonB

The equivalent in the python console is
import run_py
sys.argv=[arg0, arg1, ...]
runpy.run_module(module and)

Where arg0 is the script name.

Alternatively, import module would also work the first time, if you have set sys.argv. but subsequent imports don't run __main__.

Take a look at the module's if name=='main' section-- sometimes they call out to a main() method that you could replicate.

djl

@JonB
Thank you Jon. That worked when I entered the argv statement followed by:

Package_name.module_name.main()

I'm just learning how all of this fits together.

JonB

no problem. for modules that define a main like that, that will work. in some cases, a module might have

if __name__=='__main__':
    do_stuff()

in which case, run_py is the way to go (python -m actually uses run_py under the hood)