debug a run configuration with a package/module instead of script
Hi,
With latest pycharm (4.0.4) :
accoding to this stackoverflow response I can run a module (with a somehow trick) instead of a script: http://stackoverflow.com/questions/22081065/create-a-pycharm-configuration-that-runs-a-module-a-la-python-m-foo
it's ok for the normal run.
but I want also to debug but this fail with :
/home/gstarck/.virtualenvs/surveil/bin/python -m /home/gstarck/work/devtools/pycharm/pycharm-4.0.3/helpers/pydev/pydevd.py –multiproc –client 127.0.0.1 –port 48055 –file surveil.cmd.api
/home/gstarck/.virtualenvs/surveil/bin/python: Import by filename is not supported.
Process finished with exit code 1
Effectively pydevd expects a real filename for its –file argument.. that's the problem.
If someone has an idea on how to do this ??
With latest pycharm (4.0.4) :
accoding to this stackoverflow response I can run a module (with a somehow trick) instead of a script: http://stackoverflow.com/questions/22081065/create-a-pycharm-configuration-that-runs-a-module-a-la-python-m-foo
it's ok for the normal run.
but I want also to debug but this fail with :
/home/gstarck/.virtualenvs/surveil/bin/python -m /home/gstarck/work/devtools/pycharm/pycharm-4.0.3/helpers/pydev/pydevd.py –multiproc –client 127.0.0.1 –port 48055 –file surveil.cmd.api
/home/gstarck/.virtualenvs/surveil/bin/python: Import by filename is not supported.
Process finished with exit code 1
Effectively pydevd expects a real filename for its –file argument.. that's the problem.
If someone has an idea on how to do this ??
请先登录再写评论。
Hi I know it's an old post but did you find a solution? I am having the same problem now.
You can find a working example of a non-obvious work-around in https://intellij-support.jetbrains.com/hc/en-us/community/posts/206600855-How-to-run-library-module-as-a-script . You have to put the command in the "interpreter options" field.
Go to the tracker and vote for better support for this at https://youtrack.jetbrains.com/issue/PY-2784
Vernon Cole's link to Amnong's python_runner worked.
The following solutions found on stackoverlfow and other places and at PyCharm forums all failed to resolve relative imports in run/debug of PyCharm or maybe in PyCharm with multiple windows.
Adding package to PYTHONPATH
import sys
sys.path.insert(0, '../..')
from .relative
from relative # remove .
if __name__ == "__main__":
import os, sys
# get an absolute path to the directory that contains mypackage
foo_dir = os.path.dirname(os.path.join(os.getcwd(), __file__))
sys.path.append(os.path.normpath(os.path.join(foo_dir, '..', '..')))
from mypackage import bar
import sys
if 'pydevd' in sys.modules:
print("Debugger")
I had resorted to adding boiler bricks to my code to check if __package__ was None.
python_runner fixed relative imports without the need for any boiler bricks.
The 2017.3 EPA version of MyCharm added support for this feature. See an official JB response here:
https://youtrack.jetbrains.com/issue/PY-2784