4
votes

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

1 Answers

4
votes

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.