Error when loading model trained with python3.7 pytorch 0.4.1 using python2.7 pytorch 0.4.1

username: M_Tu
cooked: <p>Model is saved with torch.save(model.state_dict()).<br>
When I switch to python 2.7 and use</p>
<pre><code class="lang-auto">checkpoint = torch.load(Path(model_dir))
model.load_state_dict(checkpoint)
</code></pre>
<p>It gives the following error</p>
<pre><code class="lang-auto">AttributeError: 'PosixPath' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.
</code></pre>
<p>Is there a way to avoid this?</p>
<p>Thanks</p>

username: albanD
cooked: <p>What is the <code>Path</code> object that you create? You can give <code>torch.load()</code> the string containing the path. Otherwise, as said in the error message, you should read the file in a buffer and give that buffer to <code>torch.load()</code>.</p>

username: M_Tu
cooked: <p>Thanks for the reply. I change the Path object to string object and it works.</p>

username: apsvieira
cooked: <p>If you prefer working with <code>pathlib</code>, you can also use the <code>Path.as_posix</code> method to get a string referencing the same file. In that way you can handle paths with Path objects and get the string only when loading/saving the model.</p>

username: Brando_Miranda
cooked: <p>I’m having this issue too but I have not change my code nor did I change the version of pytorch (from today to yesterday). May I request a specification of when this error occurs exactly please?</p>
<p>It’s work for months without having to convert to strings.</p>
<p>Thanks in advance.</p>

username: albanD
cooked: <p>It may be some other library that changed (like the Path one that used to support this in different versions?).<br>
But converting to string should just work.</p>

username: Brando_Miranda
cooked: <p>Just to be precise, this is the error I am getting:</p>
<pre><code class="lang-auto">AttributeError: 'PosixPath' object has no attribute 'tell'
</code></pre>
<p>I don’t understand your guess. What is causing this issue, how do I avoid it? I use Path objs everywhere, how do I know no other weird unexpected bugs will occur?</p>
<p>do you know what is causing this?</p>

username: albanD
cooked: <p>Hi,</p>
<p>The is caused by the fact that the pytorch API expects either a string or a file-like object. And your Path object is neither of them.<br>
Hence my recommendation to change it to a string to call into pytorch’s save/load APIs.</p>

username: Brando_Miranda
cooked: <p>my confusion is that I’ve always used Path objects and never had this issue (until I pull a checkpoint from my sever). Why did it decide to throw an error this time but not before?</p>
<p>So to clarify, when can I use Path objects with pytorch? is it just not with torch.save and torch.load?</p>

username: albanD
cooked: <aside class="quote no-group" data-username="Brando_Miranda" data-post="9" data-topic="30931">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/48/14355_2.png" class="avatar"> Brando_Miranda:</div>
<blockquote>
<p>Why did it decide to throw an error this time but not before?</p>
</blockquote>
</aside>
<p>I don’t know.<br>
Since you said that you did not change your pytorch version, my best guess is that you changed the version of the libary providing the Path object?</p>
<blockquote>
<p>So to clarify, when can I use Path objects with pytorch? is it just not with torch.save and torch.load?</p>
</blockquote>
<p>To be safe i would say you should never give a Path object directly to the pytorch API as it is not supported (even though it might work by change for some versions of pytorch/Path object).</p>

username: Brando_Miranda
cooked: <p>sorry for being a complainer but I want to help. Why not instead of passing the responsibility to the user to do this which is sort of unusual you guys have a check or something where it converts to string (or what guarantees it will work most likely). It feels Path objects are standard to work so why not just convert to string or handle a few comment errors?</p>
<p>That would be so wonderful to a such already fantastic library that pytorch is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: albanD
cooked: <p>Hi,</p>
<p>The most likely reason is that we don’t want to add whatever lib provides Path as a dependency. More dependency means slowing build and install time so we try to keep them as low as possible.<br>
That being said, if there is a standard API to extract the string from these objects that won’t require adding new dependencies, I am sure you can add a feature request on github and it will most likely be accepted <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>