Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: attempted relative import with no known parent package #709

Open
u3Izx9ql7vW4 opened this issue Dec 27, 2022 · 1 comment
Open

Comments

@u3Izx9ql7vW4
Copy link

I'm trying to turn into a pip package a notebook which runs using Papermill. There are python file local to the package that I want to import into the notebook but it's giving me an error:

# SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# sys.path.append(os.path.dirname(SCRIPT_DIR))
from . import utils, preprocessing as prep
ImportError: attempted relative import with no known parent package

This way of importing gives this error whether I try to run the notebook manually and through Papermill.

I've also tried importing it directly:

import utils, preprocessing as prep
ModuleNotFoundError: No module named 'utils'

The direct import runs fine if I execute the notebook manually, but when I try to run them through Papermill it doesn't work.

In my package parent I have

import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

from . import preprocessing
from . import utils
@garryod
Copy link

garryod commented Jan 3, 2023

Hi, I'm not part of the team working on this project, but I've come across this issue a couple times before. You should be able to resolve this issue by switching your import statement to:

from your_package_name import utils, preprocessing

Unfortunately, appending to the system path is a bit of a hack and is recommended against - see the python module docs for an in depth explanation of how the system works. You'll want to replace all instances imports with path appends with fully qualified imports too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants