Profile python module with relative import
Answered
Hi,
I'm trying to profile a module that I would usually run like
python -m project.myscript <my args>
However I cannot profile it using Pycharm Profiler. I tried to change the configuration file for the test but still no success I keep getting:
SystemError: Parent module '' not loaded, cannot perform relative import
Is there a workaround for this ?
Thanks !
Please sign in to leave a comment.
Hi Julien! Unfortunately there's no option to specify modules in Run Configuration (corresponding feature request: https://youtrack.jetbrains.com/issue/PY-2784, please vote!). For luck there is an easy workaround - you can create a temporary "entry point" at the project root, for example:
And use it in Run configuration to run profiler (and debugger). Does it help?
Hi Pavel, thanks for your answer :) Will do !
Yes, it works with the workaround, thanks again !
If you could leave an example of Python package layout of files and folders where this would work, it would be great, please.
I understand this is?
project/
__init__.py
entry.py (containing the previously written lines)
sample1/
__init__.py
sample2.py (this is the executable)
Is this correct? Are there any additional requirements? Any missing source code lines to write in any file?
I'm just trying to understand how to get to work the relative imports in PyCharm, and I can't find a way to easily initialize or change the search path "sys.path" through the IDE options.
Hi Martin! You can add different directories to sys.path by marking folders as Sources Root (right click -> Mark Directory as ... -> Sources Root) or edit interpreter paths:
Could you please provide more details about your project and what do you want to run?