Is there some way to configure run to do something like this "python -m pkg.A.foo1" ?
http://stackoverflow.com/questions/23177615/how-to-use-relative-import-within-python-spyder-ide
if foo1
is a file, why not opening it and run it directly? I mean, Run is meant to run files and I think this design is pretty simple to understand.
Closing because this request makes really no sense.
sorry... for my late answer... I was a little bit busy.
Its not possible to run directly foo because is a module inside a package using other modules inside the same package.
For example if we have this structure:
pkg __init__.py a.py A ___init___.py foo.py
And foo.py has this content
# Absolute import
from pkg.a import my_func1
# or relative import
from ..a import my_func2
Then its not possible to run foo.py directly using "python foo.py" because there is relative or absolute imports. It's only possible to execute the module like a package using **python -m pkg.A.foo**
I think that its better to guive more flexibility and allow some template variables in the run command. For example:
{PYTHON_BINARY} -m {FILE}
or like this:
%PYTHON_BINARY% -m %FILE%
Some editors like Geany or Notepad++ use this approach:
http://stackoverflow.com/questions/4614608/execute-commands-with-notepad
https://wiki.geany.org/howtos/configurebuildmenu
Otherwise how can I execute module foo.py using spyder?
Maybe it doesnt cost too much to do this. If you want, I can try to help.
I would like to ask you to reopen again this request for enhancement.
I think this would complicate things a lot for very little gain (your use case only).
So if you want to evaluate modules this way, you'd need to resort to the command line.
@ccordoba12 actually this is a valid issue.
We cannot currently run files that include relative imports and get a message saying this.
This can't be run with runfile
though because it doesn't call python
directly. So I don't see much of a point on it.
The idea is not for Spyder to run modules but scripts that import those modules instead.
This is not an exotic use-case, but widely used even within the standard library (e.g. python -m unittest discover
). As far as I know, there is no proper alternative which allows a script using relative imports to be both importable and callable from the commandline.
IPython supports this via %run -m my.module
, from python you can call import runpy; runpy.run_module("my.module")
, however runpy
has some limitations if you want interact with the python colsole after execution of the script.
@MaxGaukler, you're welcome to send us a pull request to add support for this.
Looks pretty easy! IPython just uses runpy.run_module
from the standard library. It takes a module name and a namespace.
https://docs.python.org/3/library/runpy.html
I would write it but I don't know how to tell if it works :-)
@MaxGaukler %run -m my.module
works fine. Is there a solution to run in debug mode?
@CsatiZoltan you can try the -d
flag. See here:
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-run
@bcolsen Thank you very much, it works!
Most helpful comment
sorry... for my late answer... I was a little bit busy.
Its not possible to run directly foo because is a module inside a package using other modules inside the same package.
For example if we have this structure:
And foo.py has this content
Then its not possible to run foo.py directly using "python foo.py" because there is relative or absolute imports. It's only possible to execute the module like a package using **python -m pkg.A.foo** I think that its better to guive more flexibility and allow some template variables in the run command. For example:or like this:
Some editors like Geany or Notepad++ use this approach:
http://stackoverflow.com/questions/4614608/execute-commands-with-notepad
https://wiki.geany.org/howtos/configurebuildmenu
Otherwise how can I execute module foo.py using spyder?
Maybe it doesnt cost too much to do this. If you want, I can try to help.
I would like to ask you to reopen again this request for enhancement.