Why does poetry build raise ModuleOrPackageNotFound exception?
Question:
I want to use poetry to build and distribute Python source packages, but after poetry init
I get an error running poetry build
.
ModuleOrPackageNotFound
No file/folder found for package mdspliter.tree
Answers:
Reason
The reason it can’t be found is most likely because the directory hierarchy is incorrect.
The released package is not directly the source code folder, there are many things in it that are not needed in the final package such as version control, testing and dependency management.
You should put this folder with the same name as the package as a package in that folder.
Solution
- Change the directory hierarchy so that there are packages with the corresponding names in the folder.for example:
D:GitRepositorypython_distributiontemptree
├──_init__.py
├──tree.py
├──pyproject.toml
└──README.rst
↓
D:GitRepositorypython_distributiontemptree
├──tree
│ ├──__init__.py
│ └──tree.py
├──pyproject.toml
└──README.rst
- Specify the folder in
pyproject.toml
packages = [
{ include = "your_folder_as_pack" }
]
Variants
If the name of the project is mdspliter.tree
, then it is not useful at all to include the folder mdspliter.tree
, because this naming scheme does not conform to the specification, if you use poetry new mdspliter.tree
, you will find that the name of the folder actually should be mdspliter_tree
.
(in version 1.2, this behavior has been changed to generate multi-layer folders, mdsplitter/tree
)
Option1:
I had created my project using my own tree structure to make it consistent with other projects. So, I tried creating a new project with
$ poetry new project-new
Created package project-new in project-new
Now put your files into the new tree.
Now run
$ poetry build
The above command will automatically create a dist folder with tar and whl file into it.
Option2:
As mentioned in the above answer please include your package name into the pyproject.toml file.
packages = [
{ include = "package_name" },
]
I hit this error due to a change of the parent directory after I had set up Poetry. I could see the error when I ran:
poetry build
Building old-name (0.1.0)
ModuleOrPackageNotFound
I had created a fresh name, so I wanted Poetry to reflect this:
poetry config --list # showed the old-name
poetry config cache-dir = "/Users/me/org/new-name"
poetry config virtualenvs.path "{cache-dir}/virtualenvs"
poetry update
Then I found my mistake. I needed to update the pyproject.toml
file:
[tool.poetry]
name = "new-name" # it was set to "old-name"
I found the explanation "because the directory hierarchy is incorrect" unsatisfactory and imprecise. What needs to be satisfied is that tool.poetry.name
needs to be resolvable to a module.
Let’s take your example: From your error we see that your package name is mdspliter.tree
. I think it’s to safe that then in your pyproject.toml
you have
[tool.poetry]
name = mdspliter.tree
For that module to be resolvable, any of the following files must then exist (relative to your pyproject.toml
):
./mdspliter/tree.py
./mdspliter/tree/__init__.py
I want to use poetry to build and distribute Python source packages, but after poetry init
I get an error running poetry build
.
ModuleOrPackageNotFound
No file/folder found for package mdspliter.tree
Reason
The reason it can’t be found is most likely because the directory hierarchy is incorrect.
The released package is not directly the source code folder, there are many things in it that are not needed in the final package such as version control, testing and dependency management.
You should put this folder with the same name as the package as a package in that folder.
Solution
- Change the directory hierarchy so that there are packages with the corresponding names in the folder.for example:
D:GitRepositorypython_distributiontemptree
├──_init__.py
├──tree.py
├──pyproject.toml
└──README.rst
↓
D:GitRepositorypython_distributiontemptree
├──tree
│ ├──__init__.py
│ └──tree.py
├──pyproject.toml
└──README.rst
- Specify the folder in
pyproject.toml
packages = [
{ include = "your_folder_as_pack" }
]
Variants
If the name of the project is mdspliter.tree
, then it is not useful at all to include the folder mdspliter.tree
, because this naming scheme does not conform to the specification, if you use poetry new mdspliter.tree
, you will find that the name of the folder actually should be mdspliter_tree
.
(in version 1.2, this behavior has been changed to generate multi-layer folders, mdsplitter/tree
)
Option1:
I had created my project using my own tree structure to make it consistent with other projects. So, I tried creating a new project with
$ poetry new project-new
Created package project-new in project-new
Now put your files into the new tree.
Now run
$ poetry build
The above command will automatically create a dist folder with tar and whl file into it.
Option2:
As mentioned in the above answer please include your package name into the pyproject.toml file.
packages = [
{ include = "package_name" },
]
I hit this error due to a change of the parent directory after I had set up Poetry. I could see the error when I ran:
poetry build
Building old-name (0.1.0)
ModuleOrPackageNotFound
I had created a fresh name, so I wanted Poetry to reflect this:
poetry config --list # showed the old-name
poetry config cache-dir = "/Users/me/org/new-name"
poetry config virtualenvs.path "{cache-dir}/virtualenvs"
poetry update
Then I found my mistake. I needed to update the pyproject.toml
file:
[tool.poetry]
name = "new-name" # it was set to "old-name"
I found the explanation "because the directory hierarchy is incorrect" unsatisfactory and imprecise. What needs to be satisfied is that tool.poetry.name
needs to be resolvable to a module.
Let’s take your example: From your error we see that your package name is mdspliter.tree
. I think it’s to safe that then in your pyproject.toml
you have
[tool.poetry]
name = mdspliter.tree
For that module to be resolvable, any of the following files must then exist (relative to your pyproject.toml
):
./mdspliter/tree.py
./mdspliter/tree/__init__.py