Python extra path: Custom module path for missing import resolution

Answered

I'm coming from VSCode into PyCharm. In my current python project, I have a python code where we didn't commit all the generated (grpc) python code. To generate this, let's say we run a command like: `bazel build //...`

The directory looks like this:

- workspaceFolder (source root)
|-- models
|-- dao_user.py
|-- dao_accounts.py
|-- database
|-- transaction.py
|-- google_spanner.py
|-- service
|-- grpc_user_svc.py
...
|-- .bazel-bin (auto-generated by bazel)
|-- api
|-- protos
|-- awesome_pb2.py
|-- awesome_service_pb2.py
|-- authentication.py

 

However, this will output the generated code into a different folder inside `${workspaceFolder}/.bazel-bin/`. Of course, this should give an issue in the PyCharm interpreter code where we would be missing some imports because the python code is not in the workspace folder (a.k.a the project source root).

from api.protos.authentication import AuthenticationRequest
from api.protos import awesome_pb2 as api_awersome_pb2
from api.protos import awesome_service_pb2

 

My question is, how can tell PyCharm to get the above imports to be analysed from `.bazel-bin` folders? I can do this in vscode by having the following configuration settings:

// .vscode/settings.json
{
"python.analysis.extraPaths": [
    "${workspaceFolder}/bazel-bin/api",
    "${workspaceFolder}/bazel-bin/cloud",
    "${workspaceFolder}/bazel-bin/storage"
  ],
  "python.linting.pylintPath": "pylint",
  "python.linting.pydocstyleEnabled": false,
  "python.linting.banditEnabled": false,
  "python.linting.flake8Enabled": false,
  "python.linting.prospectorEnabled": false,
  "python.languageServer": "Pylance",
  "python.analysis.autoImportCompletions": false,
}

 

0
2 comments

I think marking it as a Source Root should be enough (right-click the folder and choose Mark Directory as | Sources Root). Does it help?

1

Sergey Karpov yes, that works for me. Thank you, Sergey

0

Please sign in to leave a comment.