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

loud-laptop-89838

12/08/2022, 1:04 PM
Looking for some help re dependency inference. I have a
conftext
file that has
from delta import configure_spark_with_delta_pip
, That package is actually
delta-spark
. When I run
./pants dependencies tests/conftest
that dependency isn't inferred. So in
tests/BUILD
I added
Copy code
python_requirement(
    name="delta",
    requirements=["delta-spark==2.2.0"]
)
But when I run the dependency check again,
delta
still isn't there. Am I putting the requirement in the wrong place?
r

refined-addition-53644

12/08/2022, 1:08 PM
For using conftest.py you need to define
python_test_utils
which just a wrapper over
python_sources
https://www.pantsbuild.org/docs/reference-python_test_utils
Then you should run dependencies on the target associated with this
python_test_utils
b

bitter-ability-32190

12/08/2022, 1:14 PM
Look into the
module_mapper
fields of the
requirement
targets, they map package names to module names (if they aren't the same)
You need to tell pants the
delta
module comes from the
delta-spark
package
r

refined-addition-53644

12/08/2022, 1:16 PM
Ah true didn't check the module name and import are different. So yeah most probably that would be the reason
l

loud-laptop-89838

12/08/2022, 1:19 PM
it works if I add
delta-spark
as a dependency as @refined-addition-53644 mentioned. But would it work better if I used the module mapper?
Yeah that seems to be a better approach. because it would apply to any file, rather than just conftest
Related question though, if I'm using python-dotenv to load a local
.env
file, this would be a dependency that I have to add to the
python_tests
target under
dependencies
? If so, how do you identify those types of files that are stored in the project root
r

refined-addition-53644

12/08/2022, 3:34 PM
You can use a`file` generated target which is just .
env
and then provide explicit dependencies to your
python_tests
https://www.pantsbuild.org/docs/reference-files Further if you need to put them somewhere where dotenv expects them to be you can use
relocated_files
https://www.pantsbuild.org/docs/assets#relocated_files
l

loud-laptop-89838

12/08/2022, 6:44 PM
Thanks @refined-addition-53644 . Testing this now, I added a
files
target at my root as
Copy code
files(
    name="dotenv",
    sources=[".env"],
)
Then in the tests folder that I am running say
tests/module/
I added the following to the
BUILD
file:
Copy code
python_tests(
    name="tests",
    dependencies=[":relocated_dotenv"]
)

relocated_files(
    name="relocated_dotenv",
    files_targets=["//:dotenv"],
    src="",
    dest="tests/module"
)
But still facing the issue that the file is not being used in the test. Is there any way to see how those files are being created in the pex file? to see where the dotenv file appears? Or do you see anything wrong with what I've done?
5 Views