Q:

How not to reload Spacy?

When using Spacy and cross importing functions that use it, I would like to avoid having to reload the language models.
Should I pass the loaded model (often named nlp()) as a parameter ? Or is there something more elegant / efficient to do ? 
E.g. of what I have today:  
module.py
nlp = spacy.load('fr_core_news_md') # IMPORTS ONCE (long time...)

def my_function():
    ... does something with spacy...
    return

main.py
from module import my_function
nlp = spacy.load('fr_core_news_md') # IMPORTS AGAIN (long time...)

my_function()

Thanks for your help !

A:

If you're using a model in your application, you definitely only want to load it once and then pass around the nlp object. This isn't only faster, it also ensures consistency: if you modify the pipeline to add components or if the vocabulary is updated at runtime, you want those changes reflected across your entire application.
That said, the French loading times are unfortunately a bit slow at the moment, because they require more static data. This should hopefully improve in the upcoming versions. You can find more details and solutions in this thread. Summary:

Cut down the list of tokenizer exceptions yourself and limit them to what you need.
Serve your model via a microservice and expose an API endpoint that accepts text you want to process and returns the JSON-formatted predictions. This works especially well if you only need the model to extract something from your text.

