Python Forum
ValueError: Attempted relative import in non-package - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: Attempted relative import in non-package (/thread-8799.html)



ValueError: Attempted relative import in non-package - JoeB - Mar-08-2018

There are answers here: https://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-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


RE: ValueError: Attempted relative import in non-package - Gribouillis - Mar-08-2018

You can perhaps remove the dot in from .tfidftransformer import TfIdfTransformer