Python Forum

Full Version: Help importing a function from another python file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My directory structure is like so:

cnc-binary
---> ff_repos
---> ---> ff_nlp_trained_models
---> ---> ---> ff_nlp_trained_models
---> ---> ---> ---> __init__.py
---> ---> ---> ---> model_validation.py
---> ---> ---> ---> quantity_pred_pickle.py
---> ---> ---> ---> quantity_pred.py
---> try.py

In try.py I have this:

import ff_repos.ff_nlp_trained_models.ff_nlp_trained_models.quantity_pred_pickle
In quantity_pred_pickle.py I have this:

from model_validation import get_metrics_str
from quantity_pred import QuantityClassifier, ToTextacyDocTransformer, evaluate_prediction
When I run try.py I get the following ModuleNotFoundError:

Error:
ModuleNotFoundError: No module named 'model_validataion'
I thought the __init__.py file would solve this, but it doesn't.

I also tried changing quantity_pred_pickle to:

from ff_nlp_trained_models.model_validation import get_metrics_str
from ff_nlp_trained_models.quantity_pred import QuantityClassifier, ToTextacyDocTransformer, evaluate_prediction
This didn't work.

How can I solve this?
Please double check your spelling of model_validataion

Lewis
(Apr-11-2018, 12:05 AM)ljmetzger Wrote: [ -> ]Please double check your spelling of model_validataion

Thanks, Still throws up the same error though.
you need to add an __init__.py in the parent directory (cnc-binary) that looks like:
cnc-binary/
    try.py
    ff_repos/
        ff_nlp_trained_models/
            ff_nlp_trained_models/
                __init__.py
                model_validation.py
                quantity_pred_pickle.py
                quantity_pred.py
Since I can't test, check that format is correct.
Add file, reload code, and try again
(Apr-11-2018, 12:51 AM)Larz60+ Wrote: [ -> ]you need to add an __init__.py in the parent directory (cnc-binary) that looks like:

Thanks. So my new file structure is:

cnc-binary/
    __init__.py
    try.py
    ff_repos/
        ff_nlp_trained_models/
            ff_nlp_trained_models/
                __init__.py
                model_validation.py
                quantity_pred_pickle.py
                quantity_pred.py
This still fails.
It shouldn't is try.py in the stated directory.
if you still have issues, suggest reading: http://mikegrouchy.com/blog/2012/05/be-p...t__py.html