Python Forum
Help importing a function from another python file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help importing a function from another python file
#1
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?
Reply
#2
Please double check your spelling of model_validataion

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
(Apr-11-2018, 12:05 AM)ljmetzger Wrote: Please double check your spelling of model_validataion

Thanks, Still throws up the same error though.
Reply
#4
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
Reply
#5
(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.
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 698 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  python standard way of importing library mg24 1 872 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Problem with importing Python file in Visual Studio Code DXav 7 4,911 Jun-15-2022, 12:54 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,330 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Importing a function from another file runs the old lines also dedesssse 6 2,479 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,550 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  How to use the count function from an Excel file using Python? jpy 2 4,370 Dec-21-2020, 12:30 AM
Last Post: jpy
  importing a list of numbers into python script barrypyth 8 4,433 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  Importing python data to Textfile or CSV yanDvator 0 1,720 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  importing a CSV file into Python russoj5 1 2,917 Aug-02-2020, 12:03 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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