acoustic-library-86413
07/06/2023, 10:49 AMpyproject.toml
file at the root of my repository and ensure that these dependencies are installed when running e.g. pants test /path/to/project
?
The folder structure is similar to this:
my-repository/
pants.toml
pyproject.toml
poetry.lock
projects/
project-a/
README.md
project_a/
routers/
...
project-b/
README.md
project-b/
routers/
...
gorgeous-winter-99296
07/06/2023, 10:53 AMpyproject.toml
as the source for a python_requirements
build target: https://www.pantsbuild.org/docs/python-third-party-dependencies#pep-621-compliant-pyprojecttoml
The rest of the setup should work as regular with setting up a default resolve, generating lockfiles, and running your tests.refined-addition-53644
07/06/2023, 10:55 AMpoetry_requirements()
https://www.pantsbuild.org/docs/python-third-party-dependencies#poetryacoustic-library-86413
07/06/2023, 10:56 AMpants test /path/to/subproject/tests/unit
pants will give me a ModuleNotFound error. It appears as if having poetry_requirements
at the top-level is not enough. What would I put in my BUILD
files at the subproject?acoustic-library-86413
07/06/2023, 10:57 AMsource
parameter for poetry_requirements
but receive the following error:
InvalidFieldException: The 'source' field in target projects/my_project:my_project should not include `../` patterns because targets can only have sources in the current directory or subdirectories. It was set to ../../../pyproject.toml. Instead, use a normalized literal file path (relative to the BUILD file).
Which makes perfect sense, since this is inside a sub-folder of the root directory where the pyproject.toml is defined.gorgeous-winter-99296
07/06/2023, 11:02 AMpants dependencies /path/to/subproject/tests/unit/some_file.py
, does it reference any of your dependencies at all? Do you have a default-resolve setup, and/or does your python_tests
and the poetry_requirements
use the same resolve? Does generating lockfiles work (pants generate-lockfiles
)?acoustic-library-86413
07/06/2023, 11:33 AMpants tailor ::
.
It then generated BUILD
files in all subdirectories containing python-sources. And the BUILD
file at the top-level has the following:
poetry_requirements(
name="root",
)
I had assumed that this would be sufficient for me to be able to run pants test
afterwards, and have the necessary dependencies installed in the PEX.
If I run pants dependencies
on one of the files as you suggest it does contain references to the relevant projects in the form of /path/to/project:poetry#requests
as an example.gorgeous-winter-99296
07/06/2023, 11:43 AM/path/to/project:poetry#requests
sounds like you have multiple sets of requirements here - not sure if that's your intention. If you have a root pyproject.toml with your dependencies I'd expect that to say //:root#requests
.gorgeous-winter-99296
07/06/2023, 11:45 AMacoustic-library-86413
07/06/2023, 12:41 PMrepository/
libs/
lib-1/
lib_1/
projects/
project-1/
project_1/
In this case, we have lib-1
installed as an editable development dependency in the top-level pyproject.toml, howver, lib-1 also has its own pyproject.toml
which contains its own dependencies. I suspect this isn't the way to handle first-party dependencies in Pants.
In this case the BUILD
file in lib-1
at the top level simply contains poetry_requirements()
.gorgeous-winter-99296
07/06/2023, 12:45 PMacoustic-library-86413
07/06/2023, 12:46 PMlib-1 = {path = "libs/lib-1", develop = true}
.gorgeous-winter-99296
07/06/2023, 12:49 PMpants roots
should list them allacoustic-library-86413
07/06/2023, 12:53 PMlibs
projects
acoustic-library-86413
07/06/2023, 12:56 PMlibs/lib-1
to my root_patterns, things start to look better.gorgeous-winter-99296
07/06/2023, 12:57 PMlib_foo.a
it'll check try to find libs/lib_foo/a.py
and projects/lib_foo/a.py
, etc. This means that if your layout is libs/lib-1/lib_1/a.py
or some such it won't find it. The solution is to add each project as a source root. The easiest way to do this is to use a marker file, which allows you to say pyproject.toml
denotes a root you can import from.
[source]
marker_filenames = ["pyproject.toml"]
gorgeous-winter-99296
07/06/2023, 12:58 PMgorgeous-winter-99296
07/06/2023, 1:00 PMacoustic-library-86413
07/06/2023, 1:07 PMpytest-env
to define environment variables directly in the pytest.ini
file. However, it is saying that all of my environment variables required for the tests are not defined.acoustic-library-86413
07/06/2023, 1:09 PMextra_env_vars
exists in the python_tests
directive. I'll try using this first.gorgeous-winter-99296
07/06/2023, 1:10 PMgorgeous-winter-99296
07/06/2023, 1:12 PMacoustic-library-86413
07/06/2023, 1:18 PMextra_requirements
, that seems to work. I was also able to get extra_env_vars
to work. However, now I'm getting another ModuleNotFoundError, this time for a third-party package that is present in the top-level pyproject.toml
.
Since last time I have removed pyproject.toml
from lib-1
, since it is now a first-party dependency and have updated my roots to include it correctly. If I run pants dependencies
on the tests folder of the project the module it complains about is not listed here.gorgeous-winter-99296
07/06/2023, 1:18 PMPIL
is provided by pillow
, which Pants cannot know about easily.acoustic-library-86413
07/06/2023, 1:19 PMacoustic-library-86413
07/06/2023, 1:19 PMpython-package
, but the import is simply from package import ...
gorgeous-winter-99296
07/06/2023, 1:19 PMmodule_mapping
is your next step: https://www.pantsbuild.org/docs/reference-poetry_requirements#codemodule_mappingcodeacoustic-library-86413
07/06/2023, 1:22 PMacoustic-library-86413
07/06/2023, 1:26 PMpatch
calls from unittest.mock
is now failing with an AttributeError.
The error message is: lib_1.utils has no attribute 'something'
. However, libs/lib-1/lib_1/utils/something.py
does exist. Have you experienced similar issues?acoustic-library-86413
07/06/2023, 1:28 PMpants dependencies projects/project-1/tests/test_something.py
does not list the module that we needed to configure in module_mapping
as a dependency.gorgeous-winter-99296
07/06/2023, 1:29 PMgorgeous-winter-99296
07/06/2023, 1:30 PMacoustic-library-86413
07/06/2023, 1:31 PMpatch
call inside a unit-test. The actual code is never directly imported since the test uses FastAPIs TestClient to carry out an HTTP Request that calls another function. I guess I would have to inform Pants that all of the functions in the routers
are dependencies of the tests (?)gorgeous-winter-99296
07/06/2023, 1:31 PMacoustic-library-86413
07/06/2023, 1:40 PMgorgeous-winter-99296
07/06/2023, 1:51 PMacoustic-library-86413
07/06/2023, 1:53 PMfastapi.TestClient
is used to run an API query against /my_endpoint
. This in turn calls some_function()
in my code. However (AFAIC) this dependency cannot possibly be inferred by Pants, unless there is a plugin that tightly integrates with FastAPI. As a result, I have a bunch of tests that make API calls that in turn call functions in my project which aren't picked up automatically.acoustic-library-86413
07/06/2023, 1:59 PMpants dependencies --transitive projects/project-1/tests/unit/
One of the dependencies listed is projects/project-1/project_1/routers/users.py
which has the following on line 8: from thefuzz import fuzz
.
The package thefuzz
is present in the top-level pyproject.toml
file, but is not included in the list of transitive dependencies. As far as I can tell, this should definitely have been picked up as a dependency by Pants.gorgeous-winter-99296
07/06/2023, 2:06 PMpants dependencies --transitive
acoustic-library-86413
07/06/2023, 2:06 PM--transitive
flag.gorgeous-winter-99296
07/06/2023, 2:07 PMgorgeous-winter-99296
07/06/2023, 2:07 PMpants dependencies projects/project-1/project_1/routers/users.py
find it?acoustic-library-86413
07/06/2023, 2:08 PM--transitive
set (although this should be unnecessary).acoustic-library-86413
07/06/2023, 2:08 PMgorgeous-winter-99296
07/06/2023, 2:09 PM-ldebug
and see what shows up. I'm not too familiar with the dependency inference, especially if it doesn't print any warnings.acoustic-library-86413
07/06/2023, 2:10 PMPants cannot infer owners for the following imports in the target /path/to/users.py
* thefuzz.fuzz (line: 8)
I suspect this is related to the module_mapping
somehow?gorgeous-winter-99296
07/06/2023, 2:11 PMthefuzz>=1.2.3
f.ex. that should be fine.acoustic-library-86413
07/06/2023, 2:12 PM"thefuzz": ["fuzz"]
To:
"thefuzz": ["fuzz", "thefuzz.fuzz"]
It is suddenly picked up correctly.gorgeous-winter-99296
07/06/2023, 2:13 PMgorgeous-winter-99296
07/06/2023, 2:15 PMMODULE_MAPPINGS = {
"grpcio-reflection": ["grpc_reflection"],
"grpcio-health-checking": ["grpc_health"],
"google-cloud-storage": ["google.cloud"],
"google-auth": ["google.auth", "google.oauth2"],
"pyyaml": ["yaml"],
"emote-rl": ["emote"],
"pillow": ["PIL"],
}
The right-hand side is what things can be imported by having the left hand side as a dependency. In the case of "thefuzz" it always gets imported as "thefuzz" so no work needed. 🙂acoustic-library-86413
07/06/2023, 2:19 PMacoustic-library-86413
07/06/2023, 2:47 PMpants export
command that can create a virtual environment. However, it doesn't seem to me that this environment contains the site-package dependencies (from poetry) as well. It this possible to achieve, I don't see any options I could pass in to export that would allow for this.
My use case is that I want to be able to introspect the code and jump to the source directly in my IDE.gorgeous-winter-99296
07/06/2023, 2:48 PMgorgeous-winter-99296
07/06/2023, 2:48 PMacoustic-library-86413
07/06/2023, 2:51 PMhttpx
as a dependency, I would expect this to be present in the dist/
folder somewhere.gorgeous-winter-99296
07/06/2023, 2:52 PMacoustic-library-86413
07/06/2023, 2:54 PMpants export --py-resolve-format=symlinked_immutable_virtualenv --resolve=python-default
16:53:01.75 [ERROR] 1 Exception encountered:
Engine traceback:
in `export` goal
IntrinsicError: Unmatched glob from the resolve `python-default`: "3rdparty/python/default.lock"
gorgeous-winter-99296
07/06/2023, 2:56 PM[python] enable-resolves = true
in pants.toml. Then pants generate-lockfiles ::
. Then you should get further. I don't think you can get a working export without resolves enabled.acoustic-library-86413
07/06/2023, 8:04 PMpyproject.toml
file. However, it errors out on all internal packages (published to our own internal package manager). The error is:
ERROR: Could not find a version that satisfies the requirement your-package<0.3.0,>=0.2.2
ERROR: No matching distribution found for your-package<0.3.0,>=0.2.2
This was installing fine when it created the PEX file to run tests before we enabled resolves. It also installs just fine using plain poetry and the same index-url. Using -ldebug
I see that the lock action runs with --extra-index-url
set correctly.acoustic-library-86413
07/06/2023, 8:04 PM.tar.gz
and .whl
.gorgeous-winter-99296
07/07/2023, 8:05 AM[python]
interpreter_constraints = [">=3.9,<3.10"]
and
[python.resolves_to_interpreter_constraints]
the-resolve = [">=3.9,<3.10"]
might both be necessaryacoustic-library-86413
07/07/2023, 8:07 AM==3.11.*
.acoustic-library-86413
07/07/2023, 8:08 AM"REQUESTS_CA_BUNDLE={chroot}/ca-certificates.crt"
in env_vars.add
in addition to this. I do feel like the documentation could use some love, how long have you been using Pants?gorgeous-winter-99296
07/07/2023, 8:15 AMacoustic-library-86413
07/07/2023, 8:18 AMgorgeous-winter-99296
07/07/2023, 8:28 AMacoustic-library-86413
07/07/2023, 8:29 AMrequests
internally.gorgeous-winter-99296
07/07/2023, 8:35 AM--cert
and --client-cert
so we should probably forward the setting in that case, if we don't already. In which case that'd probably be a Pex bug...acoustic-library-86413
07/07/2023, 8:40 AMca_certs_path
and not want pip to use this, but I honestly don't know.gorgeous-winter-99296
07/07/2023, 8:42 AMacoustic-library-86413
07/07/2023, 8:45 AMBy default, Pants will respect and pass through theThe underlying issue here is thatandSSL_CERT_DIR
environment variables.SSL_CERT_FILE
requests
doesn't respect the SSL_CERT_FILE
variable, and relies on the (much older) REQUESTS_CA_BUNDLE
. Since we pass along SSL_CERT_FILE
it should also be safe to pass along REQUESTS_CA_BUNDLE
. AFAIK they share the same purpose, but some applications support both or only one or the other.gorgeous-winter-99296
07/07/2023, 8:48 AMca_certs_path
? I've never worked in the kind of environment where this ends up being necessary, so maybe I'm seeing the wrong problems.acoustic-library-86413
07/07/2023, 8:53 AMca_certs_path
contains all certificates installed on the system. However, it's entirely possible to point it to a file containing a single certificate as well if you don't have access to modify the internal bundle. A change that automatically propagates ca_certs_path
and sets the necessary environment variables might cause unintended issues in such a configuration.gorgeous-winter-99296
07/07/2023, 8:58 AMacoustic-library-86413
07/07/2023, 9:01 AMpants test -ldebug --output=all -- -s
gorgeous-winter-99296
07/07/2023, 10:50 AM