Search code examples
pythonintellij-ideavirtualenv

IntelliJ with Python plugin and project SDK virtualenv, can't find library code for browsing


Using IntelliJ 2023.1.2 on MacOS 13.3.

Created project with a Python 3.10 virtualenv SDK at project level. Then pip install openai. Copied simple OpenAI tutorial script into my project. It is recognized as a Python file. The script runs successfully from the command line or from an IntelliJ Run Configuration.

I want to learn/explore the OpenAI objects and methods by browsing the OpenAI library source code. But when I click on the script code, such as client = OpenAI(), IntelliJ shows me an unclear error message Cannot find declaration to go to. What is the meaning?

How can I get IntelliJ to let me browse source code of a pip installed Python lib?


Solution

  • Some how IntelliJ botched creation of my Python project. It let me add a project level Python SDK, but wouldn't use it in the project's one and only module.

    In the Project Structure's module tab, it showed an error because of missing module SDK, but wouldn't let me fix it. Clicking the module's SDK "+" button had no effect.

    Invalidating cache and restarting didn't fix the problem.

    Solution

    I fixed the problem by deleting and recreating the project's Python SDK and the module. In particular, this time I made SDK "visible" to other projects. Next, I recreated the module from existing sources. After these steps, IntelliJ finally configured the module's Python SDK correctly.

    After that, IntelliJ let me browse the OpenAI library source code by clicking on my calls to it.

    Pro-Tip: For non-Java/Kotlin development, IntelliJ can be extremely finicky and frustrating. To fix bizarre IntelliJ behavior, it might be better to delete and then recreate the project, module, and SDK definitions rather than waste hours fiddling with broken Project Structure config.