How to exclude *.pyc and __pycache__ from python wheels?
Asked Answered
P

5

15

What's the correct way to exclude files from a python wheel distribution package?

Editing the MANIFEST.in does not have any effect and I can't find information about this detail.

Porpoise answered 19/4, 2016 at 14:14 Comment(1)
Do you have an example source code available? Would be nice to see a repo of this so it can be reproduced.Loudspeaker
R
2

I assume you are asking how to exclude __pycache__ directory and *.pyc while packaging into a wheel file using bdist/sdist. If so, the following manifest commands worked for me.

global-exclude */__pycache__/*
global-exclude *.pyc
Rettarettig answered 7/3, 2023 at 22:53 Comment(0)
D
1

If you are noticing this as part of a pip install one thing to do is figure out if the .pyc files are part of the build or part of the install.

To distinguish between the two options have a look at the files in the build/ directory. If you see pyc files in there then you know it's part of the wheel and the MANIFEST.in solution from @Sreekhar will work to exclude them.

However if there are no .pyc files in the build/ directory then the pyc files are created during the install. To fix this you simply need to add the --no-compile option to pip install.

Doubleganger answered 26/1 at 3:7 Comment(0)
S
0

I have never had that problem.

Here's an excerpt from my setup.py:

   name='aenum',
   version='2.0.2',
   url='https://bitbucket.org/stoneleaf/aenum',
   packages=['aenum'],
   package_data={
       'aenum' : [
           'LICENSE',
           'README',
           'doc/aenum.rst',
           'doc/aenum.pdf',
           ]
       },
   include_package_data=True,
   license='BSD License',
   description="Advanced Enumerations (compatible with Python's stdlib Enum), NamedTuples, and NamedConstants",
   long_description=long_desc,
   provides=['aenum'],
   author='Ethan Furman',
   author_email='...',
   classifiers=[
        ...
        ],

and my MANIFEST.in:

exclude aenum/*
include setup.py
include README
include aenum/__init__.py
include aenum/test.py
include aenum/test_v3.py
include aenum/LICENSE
include aenum/CHANGES
include aenum/README
include aenum/doc/aenum.pdf
include aenum/doc/aenum.rst

I would say the exclude aenum/* is what does the trick for me, so probably an exclude <package_name>/__pycache__ would work for you.

Susurrant answered 3/3, 2017 at 21:15 Comment(0)
W
0

find_packages from setuptools does that, additionally you may specify direrctories to exclude, like:

packages=find_packages(exclude=['docs', 'tests']),

But compiled artifacts (pyc files and __pycache__ dir) should be excluded automatically.

Woodenware answered 27/6, 2018 at 15:49 Comment(1)
find_packages(exclude=[]) only filters at the package levelFarreaching
M
-3

Why would you be doing that? The __pycache__ directory will be generated anyway when a project is run for the first time on the target machine. It's simply an optimised bytecode representation of Python.

But anyway, you can write an script that unpacks the .whl file and does the modifications and then repacks the wheel.

Mervinmerwin answered 11/1, 2020 at 20:55 Comment(1)
i think the "why" is beside the point. However, if the optimized bytecode can be generated on the client side then it is reasonable to exclude it from the distribution wheel. The size may not make much of a difference for one or two uploads/downloads but if you have thousands of downloads, it adds up.Glomeration

© 2022 - 2024 — McMap. All rights reserved.