0

I have a repo on GitHub that contains a bunch of Matplotlib style files (*.mplstyle files), and I would like to put it up on PyPI.org.

My directory structure looks like this:

|-- README.md
|-- setup.py
|-- styles/
    |-- style1.mplstyle
    |-- style2.mplstyle
    |-- style3.mplstyle
    |-- subdir/
        |-- substyle1.mplstyle

My setup.py file looks like this:

""" Install Matplotlib style files.

This file is based on a StackOverflow answer:
https://stackoverflow.com/questions/31559225/how-to-ship-or-distribute-a-matplotlib-stylesheet

"""

import atexit
import glob
import os
import shutil
import matplotlib
from setuptools import setup
from setuptools.command.install import install

def install_styles():

    # Find all style files
    stylefiles = glob.glob('styles/**/*.mplstyle', recursive=True)

    # Find stylelib directory (where the *.mplstyle files go)
    mpl_stylelib_dir = os.path.join(matplotlib.get_configdir() ,"stylelib")
    if not os.path.exists(mpl_stylelib_dir):
        os.makedirs(mpl_stylelib_dir)

    # Copy files over
    print("Installing styles into", mpl_stylelib_dir)
    for stylefile in stylefiles:
        print(os.path.basename(stylefile))
        shutil.copy(
            stylefile, 
            os.path.join(mpl_stylelib_dir, os.path.basename(stylefile)))

class PostInstallMoveFile(install):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        atexit.register(install_styles)

setup(
    name='myrepo',
    package_data={
        'styles': [
            "style1.mplstyle",
            "style2.mplstyle",
            "style3.mplstyle",
            "subdir/substyle1.mplstyle",
        ]
    },
    include_package_data=True,
    install_requires=['matplotlib',],
    cmdclass={'install': PostInstallMoveFile,},
)

This allows the package to be installed via:

pip install git+https://github.com/<myrepo>.git

But it doesn't work when I put it up on PyPI. (I can upload it to PyPI and pip install it from PyPI, but the *.mplstyle files aren't copied over into my computer and they aren't available when I run Python.) Any ideas on what I could add/change to get it working?

qtc0
  • 1
  • 1
  • `name='myrepo',` Was it really necessary to hide the package name? You've already published it for the entire world at PyPI. – phd Apr 15 '20 at 18:20
  • "*But it doesn't work when I put it up on PyPI.*" What exactly have you put at PyPI? A wheel? You cannot have any post-installation script with a wheel, but you can have it with a source distribution. – phd Apr 15 '20 at 18:22
  • I used `python setup.py sdist`, which creates a source distribution in `dist/.tar.gz`. But the `*.mplstyle` files are not contained within this file. Is there a way to force the mplstyle files into the source distribution? – qtc0 Apr 15 '20 at 19:17
  • 1
    Thanks for your help... I got it working by adding `global-include *.mplstyle` to my `MANIFEST.in` file. – qtc0 Apr 15 '20 at 19:33

0 Answers0