I saw this code on stack overflow

def writeProfile(profileName, profileData):
    """ pickle user profile data """

    os.chdir(PROFILE_DIR)

    fileName = profileName + ".dat"
    f = open(fileName, "wb")
    pickle.dump(profileData, f)
    f.close()

    os.chdir("..")

It is for saving progress under the pickle module. I don't see a way to save variables. How would I do that?

Dani AI

Generated

Short answer for : yes — pickle can save variables, but the practical, reliable approach is to choose exactly what you want to persist and serialize that. Blindly dumping a module’s globals or changing the process working directory (as in the example you found) tends to bring along non-picklable objects and fragile state.

A pragmatic workflow:

  • Build an explicit state mapping (a plain dict of the names/values you care about) rather than trying to pickle everything.
  • Test objects for picklability (attempt a small pickle.dumps() inside a try/except) and convert complex objects to simple representations (e.g., obj.__dict__ or a small dict of attributes).
  • Write the state with a context manager and the highest protocol; store a tiny header (schema version, Python version) so you can detect incompatible files later. Use absolute paths or pathlib instead of changing the working directory.

Important cautions and alternatives:

  • Never unpickle data from an untrusted source — unpickling can execute arbitrary code.
  • Pickles depend on importable class/module names and on Python versions; they are not a good long-term, cross-language format. For simple data prefer JSON (human-readable) or a small database (sqlite) for structured persistence. For more advanced cases (serializing lambdas/closures) consider third-party tools such as dill; shelve is handy but still uses pickle under the hood and has platform/backend quirks (as noted).

Troubleshooting tips tied to earlier replies:

  • If unpickling raises AttributeError/ModuleNotFoundError, ensure the class is defined at top-level and importable by the same name, or save a simpler representation.
  • Keep the saved state small and explicit — that’s the most maintainable solution and avoids many of the pitfalls discussed by , and others.

Recommended Answers

All 10 Replies

Well, pickle.dump() should have no problem saving a variable, as in pickle.dump(variable,f). Now if pickle doesn't like that it will raise an exception, but I suspect if you just try it will work fine...based on pickle.dumps() working on a test case I just tried.

You will probably find your real problem comes later, when tryng to unpickle. But that's later...

You can pickle your code's local or global dictionary, this will save the name and value of each variable.

If you follow Lardmeister's advice, make sure your variable names are rather unique.

I just stumbled into this thread and, not being a python programmer, found it quite aumusing ... pickle, unpickle, huh? Can you cucumber.dump() too?

No, Dani, cucumber is for Ruby. :)

Python has Lettuce. But cucumber claims to work with python too.


pickle is for turning objects*
into text,to write to a file so you
can save variables

*Most objects, including user-defined, If not, you can customize to do it.
It can even do recusive lists!

So it's like php's var_export() or serialize() then I guess.

My preference would be Python module shelve, which creates a 'persistent to file' dictionary. Dictionary objects are highly optimized in Python.

My preference would be Python module shelve, which creates a 'persistent to file' dictionary.

I don't like shelve. I think it is very slow. Did you try ? (in linux mint, install it with the package manager!)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.