First time here? Check out the FAQ!
1

Failed to run/debug pdfarranger from wing IDE: builtins.ImportError: attempted relative import with no known parent package.

  • retag add tags

I try to run/debug pdfarranger from wing IDE, using a virtualenv created as follows:

$ sudo apt-get -y install python-is-python3 python3-virtualenv python3-pip python3-distutils-extra python3-wheel python3-gi python3-gi-cairo gir1.2-gtk-3.0 gir1.2-poppler-0.18 python3-setuptools
$ pyenv shell system
$ pyenv virtualenv --system-site-packages pdfarranger

When I try to run the main file of pdfarranger, the following error is triggered:

image

Any hints for fixing this problem? See here for relevant discussion.

Regards, HZ

hongyi-zhao's avatar
547
hongyi-zhao
asked 2022-01-05 07:33:53 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

2 Answers

1

This error occurs when you try to import a module using a relative import, but Python cannot find the module's parent package.

To fix this error attempted relative import with no known parent package, you need to make sure that the module being imported is part of a package, and that the package is on the Python path. You can do this by adding an empty __init__.py file to the package's directory, and by making sure that the directory containing the package is on the Python path.

Alternatively, you can run the module using the -m flag with the Python interpreter, like this:

python -m mypackage.mymodule

This tells Python to run mymodule.py as a module, which will allow it to correctly resolve relative imports.

The main advantage of using relative imports is that they make it easier to move packages and modules around within a project, without having to update all the import statements. They also make it easier to reuse code across multiple projects, because the import paths can be modified to match the package hierarchy of each project.

linehammer's avatar
11
linehammer
answered 2023-02-27 00:50:07 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

Many modules in packages such as pdfarranger.py are not meant to be run as scripts. If you ran 'python /path/to/pdfarranger.py', you should get the same exception. You probably want to start the debugger with the module / script used to run the program. According to the README in the pdfarranger project, it's run via python3 -m arranger. To debug in Wing, set the python path so it includes the parent directory of the pdfarranger package directory and create a named entry point using the Named Module option with pdfarranger as the the module name.

More details on how to run / debug:

  1. One way to add the parent directory of the pdfarranger package directory to the python path is to add it in the Project Properties dialog -- image description

  2. To create a named entry point, use Debug > Debug Named Entry Point > Manage Named Entry Points to display the dialog for all named entry points and then click the plus icon to add a new one. Select the Named Module option and enter pdfarranger as the the module name -- image description

Wingware Support's avatar
4.1k
Wingware Support
answered 2022-01-05 09:49:28 -0500, updated 2022-01-06 10:17:50 -0500
edit flag offensive 0 remove flag delete link

Comments

To debug in Wing, set the python path so it includes the parent directory of the pdfarranger package directory and create a named entry point using the Named Module option with pdfarranger as the the module name.

I am still confused by your above description. Can you give me more detailed information?

hongyi-zhao's avatar hongyi-zhao (2022-01-05 18:19:51 -0500) edit

I edited the above answer to add detail because comments can't include images

Wingware Support's avatar Wingware Support (2022-01-06 10:19:20 -0500) edit

Thanks a lot. It works as expected.

hongyi-zhao's avatar hongyi-zhao (2022-01-06 17:53:33 -0500) edit
add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer