https://pantsbuild.org/ logo
#general
Title
# general
b

brash-student-40401

09/21/2022, 7:52 PM
I'm getting an import error when I try to run some tests that doesn't make sense to me. My test is failing on
import fastapi
with
ModuleNotFoundError: No module named 'requests'
, which is a dependency of FastAPI. I tried explicitly adding
requests
to my
requirements.txt
and rebuilding the lock file, but it didn't clear the error. This seems funny to me - the code I am trying to test (using FastAPI) builds and runs fine, it's only the test itself that's failing on this import. Any advice? Some more details in ๐Ÿงต
Full error:
Copy code
ImportError while importing test module '/tmp/process-executionq8o3SZ/src/services/dandelion/tests/test_app.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
src/services/dandelion/tests/test_app.py:3: in <module>
    from fastapi.testclient import TestClient
/home/vscode/.cache/pants/named_caches/pex_root/venvs/s/42335c17/venv/lib/python3.8/site-packages/fastapi/testclient.py:1: in <module>
    from starlette.testclient import TestClient as TestClient  # noqa
/home/vscode/.cache/pants/named_caches/pex_root/venvs/s/42335c17/venv/lib/python3.8/site-packages/starlette/testclient.py:15: in <module>
    import requests
E   ModuleNotFoundError: No module named 'requests'
I also tried adding this to the
BUILD
, but it didn't help:
Copy code
python_requirement(
    name="requests",
    requirements=["requests"],
)
I feel like I'm missing something basic here, isn't Pants supposed to handle dependencies of my imports like this?
e

enough-analyst-54434

09/21/2022, 7:57 PM
Which version of starlette? It's tip definitely doesn't depend on requests: https://github.com/encode/starlette/blob/master/pyproject.toml
b

brash-student-40401

09/21/2022, 8:24 PM
@enough-analyst-54434 Starlette is coming from fastapi in Python 3.8, my guess is it's not pulling the latest and greatest. But regardless, shouldn't Pants know about my dependencies' dependencies? @fast-nail-55400 I have read through that, but it didn't help. Neither adding Requests explicitly as a dependency or adding setuptools fixed the modulenotfound error.
e

enough-analyst-54434

09/21/2022, 8:25 PM
What version of fastapi then? Pants only follows imports and transitive 3rdparty deps by default.
@brash-student-40401 if you can provide the fastapi version I can present an analysis of exactly why it doesn't actually require requests which I'm pretty sure must be the case.
b

brash-student-40401

09/21/2022, 8:28 PM
0.85.0 - I see in the
default.lock
that it is asking for requests:
Copy code
"project_name": "fastapi",
          "requires_dists": [
            ...
            "requests<3.0.0,>=2.24.0; extra == \"all\"",
            "requests<3.0.0,>=2.24.0; extra == \"test\"",
e

enough-analyst-54434

09/21/2022, 8:29 PM
Are you asking for the extras?
For example:
fastapi[test]
?
If not - you don't get them
That's not a Pants thing but a Python thing - extras are extra and you must explicitly request them as a comma-sep list in square brackets if you want them.
b

brash-student-40401

09/21/2022, 8:31 PM
I am not, where would I do that? But again I want to say that I did not have any problems with FastAPI in the base code, only the test. And I didn't ask for anything special in there either
e

enough-analyst-54434

09/21/2022, 8:31 PM
Where do you require fastapi right now? In a requirements.txt file?
b

brash-student-40401

09/21/2022, 8:32 PM
Yes
e

enough-analyst-54434

09/21/2022, 8:32 PM
If so, that's where you'd write
fastapi[test]
instead of
fastapi
The reason its a problem in tests is you use different code in tests - that TestClient thing perhaps?
Pants only slices the exact deps you need for each context.
b

brash-student-40401

09/21/2022, 8:33 PM
Ooh good point, I wonder if it's not actually calling requests anywhere else!
e

enough-analyst-54434

09/21/2022, 8:33 PM
It definitely is not.
๐Ÿ™Œ 1
b

brash-student-40401

09/21/2022, 8:33 PM
let me rebuild from my new requirements and see what happens...
And now I'm only getting an error about a package I definitely forgot about! Thank you very much for the help!
e

enough-analyst-54434

09/21/2022, 8:38 PM
You're welcome.
s

shy-advantage-49800

09/22/2022, 6:21 AM
No, you shouldn't use
fastapi[test]
, but you can use
fastapi[all]
. The "test" extra install the packages needed for test itself. It's not related to the
TestClient
. The testclient only needs the
requsts
package, which is included in the "all" extra.
7 Views