quiet-painter-18838
01/19/2022, 12:05 AMDuplicate module named "src"
Let me share my situation in the thread.quiet-painter-18838
01/19/2022, 12:08 AM- projects/
|- prj1/
|- src/__init__.py
|- BUILD
|- prj2/
|- src/__init__.py
|- BUILD
- pants.toml
pants.toml (part)
[GLOBAL]
pants_version = "2.9.0"
[source]
root_patterns = [
'/projects/*',
]
[mypy]
version = "mypy==0.910"
pyproject.toml (part)
[tool.mypy]
python_version = "3.9"
ignore_missing_imports = true
disallow_untyped_defs = true
pants roots
projects/prj1
projects/prj2
quiet-painter-18838
01/19/2022, 12:09 AMpython_sources(
name="pkg",
sources=["src/**/*.py"],
)
hundreds-father-404
01/19/2022, 12:09 AMprojects/prj1/src/app.py
, what do you use with import
statement?quiet-painter-18838
01/19/2022, 12:12 AMquiet-painter-18838
01/19/2022, 12:14 AMimport src.hoge.hige
in the child dir (e.g. src/hoge),
from .fuga import Fuga
Did I answer your question?hundreds-father-404
01/19/2022, 12:24 AMhundreds-father-404
01/19/2022, 12:24 AM__init__.py
files empty?quiet-painter-18838
01/19/2022, 1:32 AM./pants check projects/prj1:
it works without error, but if I run;
./pants check ::
the error I mentioned above was raised.
Also are yourYes. Empty. no import is there.files empty?__init__.py
enough-analyst-54434
01/19/2022, 2:40 AMsrc
package at different sys.path
entries (source roots). In order to do that in Python in general you need a namespace package. There are 3 ways to create one:
1. If you're python3 only, just remove the empty __init__.py
files.
2. Declare a namespace package in the __init__.py
files. using pkgutil
from the Python standard library.
3. Declare a namespace package in the __init__.py
files. using pkg_resources
from the setuptools
project.
For details on 1 see https://www.python.org/dev/peps/pep-0420
For examples of 2 & 3 see: https://www.python.org/dev/peps/pep-0420/#namespace-packages-today
With one of those fixes in place, you then need to tell MyPy that you're using namespace packages. Pants itself does and the critical configuration is highlighted here: https://github.com/pantsbuild/pants/blob/2d9e7b5e0cfd5b288d5ed8fd8388574171f23d9c/pyproject.toml#L29-L32quiet-painter-18838
01/19/2022, 4:24 AM./pants check ::
. But thanks for your answer, now I understood that mypy will be executed from parent repository by pants.enough-analyst-54434
01/19/2022, 4:41 AMenough-analyst-54434
01/19/2022, 4:43 AMsrc
the actual shared package name or is that just an example placeholder you're using to describe your problem case (much like foo
, bar
& `baz`are often used as placeholders)?
I ask because, generally seeing src
as a package when someone is configuring Pants for the 1st time points to a configuration issue setting up proper source roots. Usually it would be the 1st directory under a src
directory that is the root Python package.quiet-painter-18838
01/19/2022, 4:55 AM- projects/
|- prj1/
|- src/__init__.py
|- BUILD
|- pyproject.toml
|- prj2/
|- src/__init__.py
|- BUILD
|- pyproject.toml
- pkg/
|- pkg1/
|- pkg1/__init__.py
|- BUILD
|- pyproject.toml
|- pkg2/
|- pkg2/__init__.py
|- BUILD
|- pyproject.toml
- pants.toml
pkg
holds shared packages (e.g. logger), and they can be installed by pip or poetry.
projects
holds webapi or workers, and they will install pkg
, but not install other projects because we would like to keep projects as independent.quiet-painter-18838
01/19/2022, 4:58 AMpkg
needs to be unique, so we intentionally use a unique namespace. But projects
don’t need to use namespace because they are independent.enough-analyst-54434
01/19/2022, 5:27 AMquiet-painter-18838
01/19/2022, 5:39 AMquiet-painter-18838
01/19/2022, 5:55 AMenough-analyst-54434
01/19/2022, 5:55 AMNow I’m leaning towards the idea that it might be better to use a unique namespace for each project.Probably. For one, in an IDE when you go to import a type in prj1, say
src.util
, you are very likely to select the wrong src.util
since it's probably the case that several other projects, say prj3 and prj16, also have a src.util
.enough-analyst-54434
01/19/2022, 5:56 AM