Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setuptools: package directory does not exist

I have a project with this setup.py file:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="",
    version="0.0.1",
    author="",
    author_email="",
    description="",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(where="./src", exclude=("./tests",)),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.8',
)

This is my project directory structure (first two levels):

$ tree -L 2
.
├── README.md
├── setup.py
├── src
│   └── my_pkg
└── tests
    ├── conftest.py
    ├── data
    ├── __init__.py
    ├── integration
    ├── __pycache__
    └── unit

When I run any setuptools command, I get the following error:

$ python setup.py build
running build
running build_py
error: package directory 'my_pkg' does not exist

The same happens for other commands like python setup.py develop and python setup.py bdist-wheel.

I suspect that it has to do with the src directory, as specified in the find_packages(where="./src") call in the setup.py. However, I've been following the documentation, and it does look the the my_pkg module is discovered at some point.

Why does build_py fail to find it?

like image 701
Carsten Avatar asked Oct 05 '20 10:10

Carsten


People also ask

Does pip install Setuptools?

Type “ pip install setuptools ” (without quotes) in the command line and hit Enter again. This installs setuptools for your default Python installation. The previous command may not work if you have both Python versions 2 and 3 on your computer.

How do I set up setuptools for Python on Windows?

Follow the below steps to install the Setuptools on Windows using the setup.py file: Step 1: Download the latest source package of Setuptools for Python3 from here. Step 2: Extract the downloaded package using the given command. Step 3: Go to the setuptools-60.2.

Does Python include setuptools?

the setuptools is not part of the python vanilla codebase, hence not a vanilla modules. python.org installers or mac homebrew will install it for you, but if someone compile the python by himself or install it on some linux distribution he may not get it and will need to install it by himself.

Why does my package_DIR not work with Setuptools?

package_dir has to be a relative path, not an absolute path. The distutils layer under setuptools tries to reject absolute paths, but the C: confuses it. It ends up converting your path to Note the missing backslash between C: and Users.

What is Setuptools in Python?

Python setuptools is a module that deals with the Python package development process. It is a library that is designed to allow packing Python projects in a simplified manner. What’s the difference between Python setuptools Vs. distutils? What is the difference between easy_install and setup.py (pip package creation) ? This module is external.

What is the difference between Setuptools and package names?

Although setuptools allows developers to create a very complex mapping between directory names and package names, it is better to keep it simple and reflect the desired package hierarchy in the directory structure, preserving the same names. Automatic discovery is a beta feature and might change in the future.

How to create a namespace package in Python?

Historically, there were two methods to create namespace packages. One is the pkg_resources style supported by setuptools and the other one being pkgutils style offered by pkgutils module in Python. Both are now considered deprecated despite the fact they still linger in many existing packages.


Video Answer


1 Answers

find_packages() automatically generates package names. That is, in your case all it does is generate ['my_pkg']. It doesn't actually tell setup() where to find my_pkg, just that it should expect to find a package called my_pkg somewhere. You have to separately tell setup() where it should look for packages. Is this confusing and counter intuitive? Yes. Anyway, you can tell setup() where to find my_pkg by using the package_dir argument. eg.

package_dir={"":"src"}
like image 88
Dunes Avatar answered Nov 06 '22 01:11

Dunes