Resolving references to current package outside of current source root
I've been searching for a problem like this, but have not been able to find anything like this problem. Apologies if I missed a previous answer.
I'm working in a fairly large code base with Python code spread out across multiple subdirectories in a monorepo. The organization looks something like this:
team1/src/python/company/foo -> import company.foo
team2/src/python/company/bar -> import company.bar
common/src/python/company/{util1,util2,util3} -> import company.{util1,util2,util3}
I have a PyCharm project with "team1/src/python" set as the source root, and I'm working on code in the company.foo package. PyCharm is unable to resolve any references to any subpackage in company except company.foo, but the code in this package has dependencies on those other packages. I'm working in a venv where those dependencies are symlinked in the site-packages directory. PyCharm, though, seems to be latching on to the __init__.py in team1/src/python/company and assuming that all references to items in company are in that source root and is ignoring $VENV/site-packages/company. Is there a way to get PyCharm to look in both places when resolving imports? I have tried manually adding the internal dependencies as separate source roots to my project, but this has the same problem (PyCharm picks one __init__.py and only uses that).
请先登录再写评论。
Have you tried the following?
1) Add your dependencies as Content Roots https://www.jetbrains.com/help/pycharm/configuring-content-roots.html
2) Use namespace packages for packages and modules with the same name https://packaging.python.org/guides/packaging-namespace-packages/
I already tried #1. I'll try #2 and report back.
I cleaned up the extra init files per PEP420 and PyCharm is resolving things properly now. Thanks!