Pickle load: ImportError: No module named doc2vec_ext
<p>This is the structure I'm dealing with:</p>

<pre><code>src/
    processing/
        station_level/
            train_paragraph_vectors.py
    doc2vec_ext.py
    word_embeddings_station_level.py
</code></pre>

<p>I have trained and stored a model in <code>word_embeddings_station_level.py</code> like this:</p>

<pre><code>from src.doc2vec_ext import WeightedDoc2Vec

# ...

model = WeightedDoc2Vec(
    # ...
)

train(model, vocab, station_sentences, num_epochs)

# Saving the model -&gt; pickles it
model.save(open(model_file, "w"))
</code></pre>

<p>This is working fine so far. However, I want to load that model in <code>train_paragraph_vectors.py</code> like this:</p>

<pre><code>import sys
from src import doc2vec_ext
sys.modules["doc2vec_ext"] = doc2vec_ext

if __name__ == "__main__":
# ...
    model = doc2vec_ext.WeightedDoc2Vec.load(station_level_sentence_vectors)
</code></pre>

<p>but I'm getting:</p>

<pre><code>Traceback (most recent call last):
  File "E:/python/kaggle/seizure_prediction/src/processing/station_level/train_paragraph_vectors.py", line 57, in &lt;module&gt;
    model = doc2vec_ext.WeightedDoc2Vec.load(station_level_sentence_vectors)
  File "C:\Python27\lib\site-packages\gensim\models\word2vec.py", line 1684, in load
    model = super(Word2Vec, cls).load(*args, **kwargs)
  File "C:\Python27\lib\site-packages\gensim\utils.py", line 248, in load
    obj = unpickle(fname)
  File "C:\Python27\lib\site-packages\gensim\utils.py", line 911, in unpickle
    return _pickle.loads(f.read())
ImportError: No module named doc2vec_ext
</code></pre>

<p><strong>doc2vec_ext.py</strong></p>

<p>Here you can see, that I just inherit from the <code>gensim.models.Doc2Vec</code> class and do some stuff:</p>

<pre><code>class WeightedDoc2Vec(Doc2Vec):

    def __init__(self, dm=1,window=5, f_size=0, size=100, min_count=1, negative=0, dbow_words=1, alpha=0.015, workers=8, seed=42, dm_weighted=False, dm_stacked=False):
        Doc2Vec.__init__(self,
            # Constructor arguments ..
            )

        # ...
</code></pre>

<p>I don't know what's the problem here. I've tried to do the <code>sys.modules[]</code> but it's still not working properly.</p>

<p>How can I load my stored model?</p>

<hr>

<p><strong>Important:</strong></p>

<p>I noticed that I can't even load from the same module. If I try to load the model in the file where it was created (here <code>word_embeddings_station_level.py</code>) it's still not working giving me the same error.</p>

<p>When saving and loading binary data like a pickle, you need to use binary mode, not text mode.</p>

<pre><code>model.save(open(model_file, "wb"))  # 'b' for binary
</code></pre>

python pickle gensim