Python Forum
ValueError: Attempted relative import in non-package
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: Attempted relative import in non-package
#1
There are answers here: https://stackoverflow.com/questions/1153...th-init-py but I don't really understand them.

My directory structure is as follows:

Output:
dir1/ __init__.py model_helpers.py quantity_pred_pickle.py quantity_pred.py tfidftransformer.py
Inside quantity_pred_pickle.py I have the following:

import quantity_pred
import tfidf_transformer
import model_helpers
quantity_pred and tfidf_transformer are imported without any problems, but when importing model_helpers I get the following error:

Output:
MacBook-Air:ff_nlp_trained_models Olly$ python quantity_pred_pickle.py Traceback (most recent call last): File "quantity_pred_pickle.py", line 14, in <module> from model_helpers import train_clf File "/Users/Olly/Documents/MSc/Natural Language Processing/Dissertation/dir1/model_helpers.py", line 14, in <module> from .tfidftransformer import TfIdfTransformer ValueError: Attempted relative import in non-package
How can I solve this? Why is it occurring? Thanks.

SOLVED
Reply
#2
You can perhaps remove the dot in from .tfidftransformer import TfIdfTransformer
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to import sklearn after installing any package ilango 0 1,161 Oct-25-2021, 07:03 AM
Last Post: ilango
  How to reference the relative directory when creating a photoimage kenwatts275 3 6,206 May-18-2021, 07:22 PM
Last Post: menator01
  Can't import package hubenhau 1 5,869 Jan-15-2021, 06:23 PM
Last Post: buran
  import scalalib package doesn't work manu_brighter 2 2,841 Apr-17-2020, 06:36 PM
Last Post: snippsat
  Importing module from a package results in import error goghvv 2 2,344 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Import all items from all modules from package DD169 3 2,053 Jan-25-2020, 12:33 PM
Last Post: buran
  pyautogui screenshotting region relative to image found on screen Bmart6969 3 6,339 Oct-05-2019, 06:20 PM
Last Post: Bmart6969
  how do i write a cross-platform relative file path? pseudo 2 2,883 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  package import issues groovydingo 4 3,495 Oct-03-2018, 11:26 AM
Last Post: groovydingo
  function wanted: resolve relative path Skaperen 4 3,299 Sep-06-2018, 01:52 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020