Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Python package in PyCharm

Tags:

Setup

I have the following tree structure in my project:

Cineaste/ ├── cineaste/ │   ├── __init__.py │   ├── metadata_errors.py │   ├── metadata.py │   └── tests/ │       └── __init__.py ├── docs/ ├── LICENSE ├── README.md └── setup.py 

metadata.py imports metadata_errors.py with the expression:

from .metadata_errors.py import * 

Thus setting a relative path to the module in the same directory (notice the dot prefix).

I can run metadata.py in the PyCharm 2016 editor just fine with the following configuration:

enter image description here

Problem

However, with this configuration I cannot debug metadata.py. PyCharm returns the following error message (partial stack trace):

    from .metadata_errors import * SystemError: Parent module '' not loaded, cannot perform relative import 

PyCharm debugger is being called like so:

/home/myself/.pyenv/versions/cineaste/bin/python /home/myself/bin/pycharm-2016.1.3/helpers/pydev/pydevd.py --multiproc --module --qt-support --client 127.0.0.1 --port 52790 --file cineaste.metadata 

Question

How can I setup this project so that PyCharm is able to run and debug a file that makes relative imports?

like image 233
Alexandre Bell Avatar asked Jun 21 '16 12:06

Alexandre Bell


People also ask

How do I Debug a Python package?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


1 Answers

Today (PyCharm 2018.3) it is really easy but not obvious. You can choose target to run: script name or module name by pressing the label "Script Path" in edit configuration window:

Edit Configuration Window

like image 109
Mikhail Lobanov Avatar answered Sep 18 '22 12:09

Mikhail Lobanov