cloudpipe / cloudpipe/cloudpickle

Question: Is there a way to remove the unwanted imports in python when object got pickled

Open
#415 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
Python
Stars
1.9k
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
1

Description

Imagine I have a.py:
```python
import tensorflow as tf
class A:
name = "Class A"
```
dump.py:
```python
import cloudpickle
from a import A

a = A()

with open("a.pickled", "wb") as f:
f.write(cloudpickle.dump(a))
```
load.py:
```python
import pickle
with open("a.pickled", "rb") as f:
a = pickle.load(f.read())
```
When python pickles the object a, it actually also pulls in the `tensorflow`. As a result, when the object got deserialized in load.py, it imports `tensorflow` and brings in a bunch of expensive initialization related to `tensorflow`. I am wondering if we can remove the unwanted `tensorflow` import in `dump.py` without modifying `a.py`

I tried something like this in `dumpy.py`, but this doesn't work for me:
```python
import pickle
from a import A
# Delete tensorflow related import from cache
import sys
names = [name for name in sys.modules.keys() if "tensorflow" in name]
for name in names:
del sys.modules[name]

a = A()

with open("a.pickled", "wb") as f:
f.write(pickle.dump(a))
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.