Profile python module with relative import



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 !


Comment actions Permalink

Hi Julien! Unfortunately there's no option to specify modules in Run Configuration (corresponding feature request:, please vote!). For luck there is an easy workaround - you can create a temporary "entry point" at the project root, for example:

from sample1.sample2 import __main__

And use it in Run configuration to run profiler (and debugger). Does it help?

Comment actions Permalink

Hi Pavel, thanks for your answer :) Will do !
Yes, it works with the workaround, thanks again !

Comment actions Permalink

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/  (containing the previously written lines)

    sample1/ (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.


Comment actions Permalink

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?


Please sign in to leave a comment.