Before fixing the unresolved reference issues, we must know why these issues occur in the first place.
An unresolved reference issue is raised in PyCharm IDE when:
- A variable is called/used outside the scope of its existence.
- A package is used within the code without prior installation or installed with an improper version.
- A function/class used within the code without importing it in the code or has an improper import path.
PyCharm highlights these areas where the code has the issue. Hover the mouse cursor over the highlighted area and PyCharm will display an Unresolved reference.
Let’s see some of the example scenarios of how the Unresolved reference issue occurs.
Issue 1: Using variables out of scope.
Fix: Declare the variable globally or use it only within the scope.
Issue 2: Importing a package that has not been installed or does not have the required version containing that function/class.
Fix: Install the package manually via the command line or click Install package <package-name> in the suggestion box. PyCharm will fetch and install the globally available packages in the current environment.
pip install flask
Issue 3: Using a function that has not been imported properly in the current Python file.
Fix: Correct the line of import with the actual path to the hello() function.
Disabling the warning altogether
You can also disable the Unresolved references warning completely by following these steps:
- Click on the File option in PyCharm’s menu bar.
- Click on Settings.
- In the right pane of Settings go to Editor > Inspections.