I'm getting an import error when I try to run some...
# general
b
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
Which version of starlette? It's tip definitely doesn't depend on requests: https://github.com/encode/starlette/blob/master/pyproject.toml
b
@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
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
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
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
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
Where do you require fastapi right now? In a requirements.txt file?
b
Yes
e
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
Ooh good point, I wonder if it's not actually calling requests anywhere else!
e
It definitely is not.
๐Ÿ™Œ 1
b
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
You're welcome.
s
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.