Alexandre Bell
Alexandre Bell

Reputation: 3241

How to debug a Python package in PyCharm

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?

Upvotes: 33

Views: 10769

Answers (5)

Uwe Schweinsberg
Uwe Schweinsberg

Reputation: 48

In a pycharm session with a poetry virtual environment I use absolute import statements like:

from packtest.packtest import count_words

in all modules and submodules.

The packtest project file structure:

packtest
├── docs
│   └── _build
│       ├── doctrees
│       ├── html
│       └── jupyter_execute
├── htmlcov
├── src
│   └── packtest
│       ├── data
│       └── __pycache__
└── tests
    ├── __pycache__
    └── testfiles

In the src, packtest and data directories I put an empty __init__.py file.

While I am in the root packtest directory I run the tests with pytest:

pytest tests/

Debug and run :

e.g. the packtest.plotting module

As in the answer from @Mikhail Lobanov

in PyCharm, "Run" -> "Edit Configurations", I choose

Module name: packtest.plotting

Additionally, I make sure that the working directory is set to the root directory of my project (where the pyproject.toml file is located).

Now I can run, debug and test in the poetry environment without getting

SystemError: Parent module '' not loaded, cannot perform relative import

which I got because I set the working directory to

packtest/src/packtest

instead of:

packtest

which led me to this thread...

The packtest example is derived from the excellent jupyter book "Python Packages" by Tomas Beuzen & Tiffany Timbers: https://py-pkgs.org/

Upvotes: 0

Mikhail Lobanov
Mikhail Lobanov

Reputation: 516

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

Upvotes: 26

SimZor
SimZor

Reputation: 24

I would suggest not using * since that can cause many problems in the future, two classes or methods being named the same etc.

Upvotes: -2

pmneve
pmneve

Reputation: 616

You might also try removing the last node (/cineaste) from the Working Directory. This configuration works (run and debug) for me (in Pycharm: 2017.2.2)

enter image description here

Upvotes: 3

igor-nov
igor-nov

Reputation: 31

One of possible solutions could be to run your module through intermediate script which you'll run in debug mode. E.g. test_runner.py:

import runpy
runpy.run_module('cineaste.metadata')

Upvotes: 3

Related Questions