Setting Matplotlib MPLCONFIGDIR: consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

后端 未结 3 1328
野的像风
野的像风 2020-12-17 21:10

I am using Linux server to set up a django project. I got this error: \"Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matp

相关标签:
3条回答
  • 2020-12-17 21:54

    Set the MPLCONFIGDIR in code before you import matplotlib. Make sure the directory has permissions such that it can be written to by the app.

    import os
    os.environ['MPLCONFIGDIR'] = "/home/lab/website/graph"
    import matplotlib
    

    Alternatively, you could set it to a tempfile.

    import os    
    import tempfile
    os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
    import matplotlib
    
    0 讨论(0)
  • 2020-12-17 21:59

    If you happen to be working on an AWS Lambda, like I was when I came looking for answers to this, use the /tmp folder:

    import os
    os.environ['MPLCONFIGDIR'] = "/tmp"
    import matplotlib
    
    0 讨论(0)
  • 2020-12-17 22:04

    Per @Esteban I do something like this in modules or scripts:

    import os
    
    try:
        import matplotlib
    except:
        import tempfile
        import atexit
        import shutil
    
        mpldir = tempfile.mkdtemp()
        atexit.register(shutil.rmtree, mpldir)  # rm directory on succ exit
    
        os.environ['MPLCONFIGDIR'] = mpldir
    
        import matplotlib
    

    This way the temporary directory is removed on exit.

    0 讨论(0)
提交回复
热议问题