" No module named build.__main__; 'build' is a package and cannot be directly executed " when doing 'python -m build'
Asked Answered
L

2

13

I was following a tutorial on how to build a Python package, and after installing the package locally using pip install . In the project directory.

The next was to python -m build in the project directory, but get

No module named build.__main__; 'build' is a package and cannot be directly executed

I do have a build folder , but not __main__.py file though.

The repo of the project is here

Longo answered 29/4, 2023 at 10:36 Comment(1)
build is a tool that needs to be installed separately.Woundwort
L
10

Apparently, a new version of build 0.10.0 was released that no longer works like this or has a bug.

For a quick fix simply:

pip install 'build<0.10.0'

Then everything should work like in your tutorial.

By the way, these links below are my favorite tutorial about how to make a pip installable python package.

Good luck, best, Elmar

Lantha answered 30/4, 2023 at 3:30 Comment(5)
@Lantha did pip install build and then tried, seems to work now . 0.10.0 was the version installed . ThanksLongo
@blue Do you have a link to a bug report for the bug you are mentioning? I never heard of this. I am not sure recommending to install an older version makes sense. Even OP says they did no need the old version, that it works fine with the latest version.Woundwort
@Woundwort oh gosh i didnt know they're different! i've installed it from the link i commented and everything just work fine, please can you link me a reference?Blackstone
@DemistheAnacronist here is the link: pypi.org/project/buildWoundwort
Came here totally by accident, but thank youConfraternity
L
2

One thing you should check is PYTHONPATH. If it matches some other "build" then you can hit this issue.

You can see where it thinks the module should be imported from using the code below. In my case I work for a large company with lots of different repo's etc... It was picking up an internal "build" directory for something else...

import build
print(build.__path__)
Larrigan answered 25/5, 2024 at 7:54 Comment(1)
I had not checked if the local venv was activated, this nailed it for me.San

© 2022 - 2025 — McMap. All rights reserved.