4

I have a customized built module, lets call it abc, and pip install /local_path/abc-0.1-py3-none-any.whl. Installation is correct,

>>pip install dist/abc-0.1-py3-none-any.whl
Processing ./dist/abc-0.1-py3-none-any.whl
Successfully installed abc-0.1

but I could not import the module. After I ran ppip freeze list and found out the name of module in list is abc @ file:///local_path/abc-0.1-py3-none-any.whl.
my question is how could import the module? Thank you

.
├── requirements.txt
├── setup.py
├── src
│   ├── bin
│   │   ├── __init__.py
│   │   ├── xyz1.py
│   │   ├── xyz2.py
│   │   └── xyz3.py

here is my setup.py

with open("requirements.txt") as f:
    install_requires = f.read()

setup(
    name="abc",
    version="0.1",
    author="galaxyan",
    author_email="galaxyan@123.com",
    description="test whell framework",
    packages=find_packages(include=["src"]),
    zip_safe=False,
    install_requires=install_requires,
)

############ update ############
it does not work even change setup.py

with open("requirements.txt") as f:
    install_requires = f.read()

setup(
    name="abc",
    version="0.1",
    author="galaxyan",
    author_email="galaxyan@123.com",
    description="test whell framework",
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    zip_safe=False,
    install_requires=install_requires,
)
galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • This problem is often caused when your `pip` updates a Python distribution other than the one you're running. Try `python -m pip install /local_path...` and see if that flies. – Tim Roberts Mar 29 '22 at 19:56
  • Are you sure the .whl is built correctly? it's pretty easy to accidentally build a wheel without anything importable actually inside it. And note that the wheel name doesn't necessarily match the import name(s). – wim Mar 29 '22 at 19:57
  • @wim I added my setup.py in my question – galaxyan Mar 29 '22 at 20:01
  • 2
    `include=["src"]` is wrong. See the docs at https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#finding-simple-packages – phd Mar 29 '22 at 20:06
  • `install_requires = f.read()` looks wrong too. that needs to be a list, not a python string. – wim Mar 30 '22 at 00:18

1 Answers1

3

The setup.py is wrong, which means you're building a wheel with no packages actually inside.

Instead of

setup(
    ...
    packages=find_packages(include=["src"]),
    ...
)

Try this:

setup(
    ...
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    ...
)

See Testing & Packaging for more info.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Thank you for the suggestion. it does not work after changing the `setup.py` – galaxyan Mar 29 '22 at 21:45
  • Do you have any packages under "src"? A package must be a subdirectory with an `__init__.py` file in it, in this case (namespace packages will not be found by `find_packages`). Print the output of `find_packages(where="src")` and tell us what you get... – wim Mar 29 '22 at 21:55
  • yes, there are two packages in src, and both have`__init__.py` in it – galaxyan Mar 30 '22 at 00:13
  • @galaxyan there is not enough information in the question to reproduce your problem. You'll have to show the project structure too. Add the result of `tree .` or `find .` into the question please. – wim Mar 30 '22 at 00:19
  • added the folder structure – galaxyan Mar 30 '22 at 00:31
  • @galaxyan This folder structure looks OK. So please add the following info : what name(s) are you trying to import, show the error of the import statement(s), and the output of `find_packages(where="src")`. – wim Mar 30 '22 at 00:42
  • I am trying to `import abc` and the output from `find_packages` is `['bin']` – galaxyan Mar 30 '22 at 00:56
  • You can not import `abc` because the only package included in your wheel is one called `bin`. Check that `import bin` should work. If you want it to be called abc instead, then rename the subdirectory from `bin` to `abc` and rebuild the .whl file. – wim Mar 30 '22 at 01:06