chilly-tailor-75063
11/08/2022, 8:33 PM[source]
section like this in my pants.toml
file:
[source]
root_patterns = [
"/project/app",
"tests"
]
But my unit test code can’t find my project modules to import, things like this in the test code:
from . import app
How do I configure pants so test code can import project modules?witty-crayon-22786
11/08/2022, 8:33 PMwitty-crayon-22786
11/08/2022, 8:34 PMwitty-crayon-22786
11/08/2022, 8:34 PMchilly-tailor-75063
11/08/2022, 8:37 PMproject
+-- app
+-- auth
+-- routers.py
tests
+-- auth
+-- routers.py
chilly-tailor-75063
11/08/2022, 8:38 PMwitty-crayon-22786
11/08/2022, 8:38 PMwitty-crayon-22786
11/08/2022, 8:39 PMauth.routers
… there are two places to do that fromwitty-crayon-22786
11/08/2022, 8:39 PMauth/routers.py
twicewitty-crayon-22786
11/08/2022, 8:40 PMprojects/app/auth/routers.py
path, so that it was importable as app.auth.routers
?chilly-tailor-75063
11/08/2022, 8:41 PM./pants test ::
gives me these errors:
==================================== ERRORS ====================================
_________________ ERROR collecting tests/auth/test_routers.py __________________
ImportError while importing test module '/private/var/folders/5q/xbnx98wd4g12xd_60swx7yp00000gn/T/pants-sandbox-PkCA2T/tests/auth/test_routers.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/auth/test_routers.py:9: in <module>
from <http://project.app|project.app> import app
E ModuleNotFoundError: No module named 'project'
- generated xml file: /private/var/folders/5q/xbnx98wd4g12xd_60swx7yp00000gn/T/pants-sandbox-PkCA2T/tests.auth.test_routers.py.tests.xml -
=========================== short test summary info ============================
ERROR tests/auth/test_routers.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.18s ===============================
with this line in my test file:
from <http://project.app|project.app> import app
chilly-tailor-75063
11/08/2022, 8:42 PMwitty-crayon-22786
11/08/2022, 8:43 PMwith this line in my test file:
if you would like to be able to import asCopy codefrom <http://project.app|project.app> import app
<http://project.app|project.app>
, then you should adjust your source roots to add one at the root of the repository: i.e, chop nothing off of those pathswitty-crayon-22786
11/08/2022, 8:45 PMwitty-crayon-22786
11/08/2022, 8:45 PM[source]
root_patterns = [
"/",
"/tests"
]
chilly-tailor-75063
11/08/2022, 8:49 PM==================================== ERRORS ====================================
_________________ ERROR collecting tests/auth/test_routers.py __________________
ImportError while importing test module '/private/var/folders/5q/xbnx98wd4g12xd_60swx7yp00000gn/T/pants-sandbox-rQDDI8/tests/auth/test_routers.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/auth/test_routers.py:9: in <module>
from project.app.main import app
project/app/__init__.py:9: in <module>
from .config import get_settings
project/app/config.py:17: in <module>
from dynaconf import Dynaconf, Validator
/Users/dfarrell/.cache/pants/named_caches/pex_root/venvs/7e318eaca32c509748765c7d46333ae480dc3894/f3dca776f079a35aa240d41b31cbb24e27b21c02/lib/python3.9/site-packages/dynaconf/__init__.py:5: in <module>
from dynaconf.contrib import DjangoDynaconf # noqa
/Users/dfarrell/.cache/pants/named_caches/pex_root/venvs/7e318eaca32c509748765c7d46333ae480dc3894/f3dca776f079a35aa240d41b31cbb24e27b21c02/lib/python3.9/site-packages/dynaconf/contrib/__init__.py:4: in <module>
from dynaconf.contrib.flask_dynaconf import DynaconfConfig # noqa
/Users/dfarrell/.cache/pants/named_caches/pex_root/venvs/7e318eaca32c509748765c7d46333ae480dc3894/f3dca776f079a35aa240d41b31cbb24e27b21c02/lib/python3.9/site-packages/dynaconf/contrib/flask_dynaconf.py:17: in <module>
import pkg_resources
E ModuleNotFoundError: No module named 'pkg_resources'
- generated xml file: /private/var/folders/5q/xbnx98wd4g12xd_60swx7yp00000gn/T/pants-sandbox-rQDDI8/tests.auth.test_routers.py.tests.xml -
=========================== short test summary info ============================
ERROR tests/auth/test_routers.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.48s ===============================
witty-crayon-22786
11/08/2022, 8:50 PMwitty-crayon-22786
11/08/2022, 8:51 PMpypi
have hidden dependencies on pkg_resources
which they don’t declare as requirements, because their build environment leaked itwitty-crayon-22786
11/08/2022, 8:52 PMdynaconf
has an undeclared dependency on setuptools
witty-crayon-22786
11/08/2022, 8:53 PMchilly-tailor-75063
11/08/2022, 8:54 PMwitty-crayon-22786
11/08/2022, 8:54 PMchilly-tailor-75063
11/08/2022, 8:57 PMpython_requirement(…)
go, that doesn’t look like valid pants.toml syntax.witty-crayon-22786
11/08/2022, 9:00 PMBUILD
file (likely already generated for you by tailor
)witty-crayon-22786
11/08/2022, 9:01 PMtailor
will have generated one next to any files named requirements.txt
chilly-tailor-75063
11/08/2022, 9:01 PMchilly-tailor-75063
11/08/2022, 9:07 PMpython_requirement(
name="setuptools",
requirements=["setuptools==41.0.1"],
)
to the BUILD file in the directory where my test file lives. Running ./pants test ::
still can’t find pkg_resources.witty-crayon-22786
11/08/2022, 9:13 PMdynaconf
in particular has a dep on setuptools
chilly-tailor-75063
11/08/2022, 9:19 PMchilly-tailor-75063
11/08/2022, 9:28 PMpython_requirement(
name="setuptools",
requirements=["setuptools"],
)
python_requirement(
name="dynaconf",
requirements=["Dynaconf"],
dependencies=[":setuptools"],
)
Getting a ModuleNotFoundError: No module named ‘dynaconf’ when I run the tests.
I’m using Poetry, do I need to use the overrides
option in the python_requirements
section?happy-kitchen-89482
11/08/2022, 11:27 PMhappy-kitchen-89482
11/08/2022, 11:27 PMhappy-kitchen-89482
11/08/2022, 11:27 PMpoetry_requirements(
...
overrides = {
"dynaconf": {"dependencies": [":setuptools"]} # Or wherever your setuptools requirement is...
}
)
to add the dep to the existing dynaconf target, instead of having two of themchilly-tailor-75063
11/09/2022, 1:12 PMchilly-tailor-75063
11/09/2022, 1:54 PMpoetry_requirements(
name="dynaconf",
overrides={
"dynaconf": {"dependencies": [":setuptools"]}
}
)
to my BUILD file in the directory of the module I’m trying to test. Unfortunately, I got a new error:
./pants test ::
08:54:07.10 [ERROR] 1 Exception encountered:
Exception: Unmatched glob from poetry_requirements(address="tests/auth:dynaconf", description=None, module_mapping=FrozenDict({}), overrides={('dynaconf',): {'dependencies': [':setuptools']}}, source=pyproject.toml, tags=None, type_stubs_module_mapping=FrozenDict({}))'s field `source`: "tests/auth/pyproject.toml"
Do the file(s) exist? If so, check if the file(s) are in your `.gitignore` or the global `pants_ignore` option, which may result in Pants not being able to see the file(s) even though they exist on disk. Refer to <https://www.pantsbuild.org/v2.14/docs/troubleshooting#pants-cannot-find-a-file-in-your-project>.
chilly-tailor-75063
11/09/2022, 6:46 PMhappy-kitchen-89482
11/09/2022, 7:52 PMpoetry_requirements()
target in the same dir as the pyproject.toml containing your requirements? That error is saying that the target expects to find tests/auth/pyproject.toml
, so I assume you put the target in tests/auth/BUILD
, and that is I guess not where pyproject.toml lives?chilly-tailor-75063
11/09/2022, 8:16 PMpoetry_requirements()
target to the tests/auth/BUILD
file. I though that change was relative to the directory where the code lives that used it.
From what you’re saying I should move the poetry_requirements
to BUILD
file where my pyproject.toml file lives….chilly-tailor-75063
11/09/2022, 8:20 PM./pants test ::
back to complaining about No module named 'requests'
. I do have an import requests line in the test code.happy-kitchen-89482
11/09/2022, 8:27 PMpoetry_requirements
“wraps” pyproject.toml, for the purpose of generating requirements from it.happy-kitchen-89482
11/09/2022, 8:27 PMrequests
in the pyproject.toml?happy-kitchen-89482
11/09/2022, 8:29 PMrequests
is obviously in the latter category.happy-kitchen-89482
11/09/2022, 8:31 PMrequirements.txt
or a pyproject.toml
or something, and then a python_requirements
(resp. poetry_requirements
) target in a sibling BUILD file tells Pants to parse the requirements.txt
(resp. pyproject.toml
) and find providers of symbols there.happy-kitchen-89482
11/09/2022, 8:31 PMchilly-tailor-75063
11/10/2022, 1:50 PMfrom fastapi.testclient import TestClient
I’m guessing I need to add poetry_requirements()
something like what we did for dynaconf, but for requests?chilly-tailor-75063
11/10/2022, 2:05 PMpoetry_requirements(
name="fastapi", overrides={"fastapi": {"dependencies": [":requests"]}}
)
Still getting the NoModuleFound error for requests….happy-kitchen-89482
11/10/2022, 5:09 PMfastapi
should be declaring a requirement of requests
, and Pants will use that. So that is weird.happy-kitchen-89482
11/10/2022, 5:19 PMhappy-kitchen-89482
11/10/2022, 5:21 PMrequests
in your pyproject.toml
?happy-kitchen-89482
11/10/2022, 5:21 PM"dependencies": ["#requests"]
, I am forever confused by thishappy-kitchen-89482
11/10/2022, 5:25 PMall
extra, so just installing fastapi
will not bring requests
inhappy-kitchen-89482
11/10/2022, 5:25 PMhappy-kitchen-89482
11/10/2022, 5:25 PM(venv) $ pip install fastapi
Collecting fastapi
Downloading fastapi-0.86.0-py3-none-any.whl (55 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 55.5/55.5 KB 431.3 kB/s eta 0:00:00
Collecting pydantic!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0,>=1.6.2
Using cached pydantic-1.10.2-cp39-cp39-macosx_11_0_arm64.whl (2.6 MB)
Collecting starlette==0.20.4
Downloading starlette-0.20.4-py3-none-any.whl (63 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 63.6/63.6 KB 1.2 MB/s eta 0:00:00
Collecting anyio<5,>=3.4.0
Using cached anyio-3.6.2-py3-none-any.whl (80 kB)
Collecting typing-extensions>=3.10.0
Using cached typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting sniffio>=1.1
Using cached sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting idna>=2.8
Using cached idna-3.4-py3-none-any.whl (61 kB)
Installing collected packages: typing-extensions, sniffio, idna, pydantic, anyio, starlette, fastapi
Successfully installed anyio-3.6.2 fastapi-0.86.0 idna-3.4 pydantic-1.10.2 sniffio-1.3.0 starlette-0.20.4 typing-extensions-4.4.0
WARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.
You should consider upgrading via the '/private/tmp/venv/bin/python -m pip install --upgrade pip' command.
(venv) $ python
Python 3.9.13 (main, Aug 5 2022, 16:05:40)
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastapi
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
>>>
happy-kitchen-89482
11/10/2022, 5:26 PMfastapi[all]
rather than just fastapi
, to get those 3rdparty requirements in.chilly-tailor-75063
11/10/2022, 8:37 PMfastapi[all]
actually add it. 🙂