4

How can I get Intellij to recognizes a builtin that is statically linked to the interpreter such as sys? When I do import sys therefore I do not get auto-complete features sys there is no corresponding .py file in site-packages. And why does this work with PyCharm but it does not work with IntelliJ?

import sys

def dump(module):
    if module in sys.builtin_module_names:
        print("<BUILTIN>")
    else:
        module = __import__(module)
        print(module.__file__)

dump("sys")

output:

<BUILTIN>

1 Answer 1

5

I had a similar issue (IntelliJ did not recognise built-in functions like all), and found your question.

I was able to solve it myself though: you have to configure the Python SDK for your IntelliJ project. This is under File > Project Structure > Facets > Python. If the "Python interpreter" drop-down is empty, you can add one via the "..." button, which opens a "Configure SDKs" dialog.

Usually, this should list Java SDKs present on your machine, but you can also add a Python interpreter (or several), using the "+" button. You then navigate to your system's Python interpreter via a file menu (or pick a custom-installed interpreter).

After you have clicked "OK" in the dialog and in the preferences, built-in functions are recognised, and I checked that sys functions are auto-completed, too.

I would say this works out of the box for Pycharm because there having at least one Python interpreter declared in the project structure is a requirement, while in Idea with Python as a plugin it is not: having a Java SDK declared is the only requirement for a working project.

Sign up to request clarification or add additional context in comments.

2 Comments

how does adding/configuring a facet differ from just setting Python as the SDK in File > Project Structure > Project? I opened a file individually (intending to save it as a project later) and noticed "tuple" wasn't recognized, and setting the SDK where I just described seemed to work just fine. What does the facet tab even do??
Thanks for this, took me a number of random pokes and googling to stumble on this correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.