hoangKnLai / vscode-ipython

VSCode Extension integrating Editor with IPython console.
MIT License
18 stars 5 forks source link

Allow to run file as module #22

Closed guy-attali closed 1 year ago

guy-attali commented 1 year ago

Hey! Great plugin. was thinking maybe an option to run the file as a module, so it won't run the main() function. To do so, run ipython with -m current_file and drop the %run part.

hoangKnLai commented 1 year ago

Hi! Thanks for the feedback and I'm glad that you find the extension useful.

As for running the script, did you mean to run the .py as a script and not as a module? I.e., skipping __name__ == '__main__'?

If so, from IPython %run documentation, the -n option might be the solution.

This is supported via the Ipython: Run Arguments setting of the extension. The default keyboard mapping for run command currently are:

So if your script.py got

# ... other code above 

if __name__ == '__main__': 
    print('Run __main__') 
else:
    print('Skip __main__')

Change or add -n to setting Ipython: Run Arguments list. Using shift + f5 for running the file should print to the terminal something like this:

In [1]: %run -n "script.py" 
....

Out[1]: ...
Skip __main__

Now try just f5, you should get

In [1]: %run "script.py" 
....

Out[1]: ...
Run __main__
hoangKnLai commented 1 year ago

Hi! Thanks for the feedback and I'm glad that you find the extension useful.

As for running the script, did you mean to run the .py as a script and not as a module? I.e., skipping __name__ == '__main__'?

If so, from IPython %run documentation, the -n option might be the solution.

This is supported via the Ipython: Run Arguments setting of the extension. The default keyboard mapping for run command currently are:

  • f5 : do %run file.py -cli with -cli the file command line arguments
  • shift + f5: do %run -args file.py -cli with -args the %run command line arguments

So if your script.py got

# ... other code above 

if __name__ == '__main__': 
    print('Run __main__') 
else:
    print('Skip __main__')

Change or add -n to setting Ipython: Run Arguments list. Using shift + f5 for running the file should print to the terminal something like this:

In [1]: %run -n "script.py" 
....

Out[1]: ...
Skip __main__

Now try just f5, you should get

In [1]: %run "script.py" 
....

Out[1]: ...
Run __main__

Hi, does this solves your issue? Thanks.

guy-attali commented 1 year ago

Yes! I'm sorry, I've missed that option. Thank you very much!