Search code examples
pythonpython-3.xsetuptoolsmodulenotfounderrormypyc

Getting ModuleNotFoundError when trying to import third party modules in setup.py


I am currently writing a module and I want to use mypyc for it. I was following the documentation for using it inside setup.py but I am getting a ModuleNotFoundError everytime I try to import it there and run pip install ..
When running python setup.py install everything works without issue.
The error shows up when I try to import other modules too. They are all installed of course.
I have no idea what the problem could be.

This is my setup.py file:

from mypyc.build import mypycify
from setuptools import find_packages, setup

setup(
    name="my_lib_name",
    author="my_name",
    url="my_url",
    license="MIT",
    description="Some description",
    packages=find_packages(),
    version="0.0.1",
    install_requires=["levenshtein", "unidecode", "mypy"],
    ext_modules=mypycify(["mylib/"]),  # type: ignore
    python_requires=">=3.9",
    setup_requires=["pytest-runner"],
    tests_require=["pytest"],
    test_suite="tests",
)

And the error I am getting when running pip install .:

    File "setup.py", line 1, in <module>
      from mypyc.build import mypycify
  ModuleNotFoundError: No module named 'mypyc'
  ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\my_path\venv\scripts\python.exe' 'c:\users\my_path\venv\lib\site-packages\pip\_vendor\pep517\_in_process.py' get_requires_for_build_wheel 'C:\Users\my_name\AppData\Local\Temp\tmpa7qmg4q1' Check the logs for full command output.

Again, when running python setup.py install I dont get any error.
And of course, when I remove the mypyc import everything also works as intended.
Not sure what I am doing wrong here, hope someone can help me out.

For completion, I am using Python Version 3.9.2 and pip Version 20.2.3, but this error is also there when using the latest pip Version 22.0.4.


Solution

  • After a lot of trial and error, I have found the culprit. I had a pyproject.toml file in my project which seemed to cause the issue for whatever reason?? I am not sure why, it interferes with it for some reason.

    This was the content of that pyproject.toml file:

    [tool.black]
    line_length=88
    
    [tool.isort]
    profile="black"
    

    I added these lines on the top:

    [build-system]
    requires = ["setuptools", "setuptools-scm", "mypy"]
    build-backend = "setuptools.build_meta"
    

    "mypy" being the package that I want to import in the setup.py file.

    Deleting the file completely works, too.

    I hope this will help someone, maybe.