How to fix Unresolved reference issue in PyCharm
Last Updated :
30 Dec, 2022
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.
Unresolved reference: Variable out of scope
Fix: Declare the variable globally or use it only within the scope.
Fixed: Variable out-of-scope reference issue
Issue 2: Importing a package that has not been installed or does not have the required version containing that function/class.
Unresolved reference: package not installed
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
Fixed: Package installed to resolve the reference
Issue 3: Using a function that has not been imported properly in the current Python file.
File: other.py with hello() function
Unresolved reference: path to function is incorrect
Fix: Correct the line of import with the actual path to the hello() function.
Fixed: Unresolved function import
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.
File option
Settings
- In the right pane of Settings go to Editor > Inspections.
This is a part of PyCharm’s code Inspection feature, using which programmers can foresee the areas of code which may cause an issue during execution. PyCharm also suggests a quick resolution to most of these issues. This improves the agility of coding along with fewer errors.