spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.33k stars 1.61k forks source link

Spyder run configuration for relative imports #4258

Open aplicacionamedida opened 7 years ago

aplicacionamedida commented 7 years ago

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

ccordoba12 commented 7 years ago

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.

ccordoba12 commented 7 years ago

Closing because this request makes really no sense.

aplicacionamedida commented 7 years ago

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.

ccordoba12 commented 7 years ago

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.

goanpeca commented 7 years ago

@ccordoba12 actually this is a valid issue.

We cannot currently run files that include relative imports and get a message saying this.

ccordoba12 commented 7 years ago

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.

MaxGaukler commented 5 years ago

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.

ccordoba12 commented 5 years ago

@MaxGaukler, you're welcome to send us a pull request to add support for this.

bcolsen commented 5 years ago

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 :-)

CsatiZoltan commented 4 years ago

@MaxGaukler %run -m my.module works fine. Is there a solution to run in debug mode?

bcolsen commented 4 years ago

@CsatiZoltan you can try the -d flag. See here:

https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-run

CsatiZoltan commented 4 years ago

@bcolsen Thank you very much, it works!

Greedquest commented 2 years ago

Is there any wiggle room on this? Now that Spyder supports code cells with #%% I really want to use spyder to run my code rather than from the command line (code cells don't really make sense out of a GUI). Is there a way to run a code cell within a module context, right now if I want to use relative or absolute imports, I have to run the whole file at once rather than a single cell.