Open In App

How to fix Unresolved reference issue in PyCharm

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. A variable is called/used outside the scope of its existence.
  2. A package is used within the code without prior installation or installed with an improper version.
  3. 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

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

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

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

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

File: other.py with hello() function

Unresolved reference: path to function is incorrect

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

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

File option

  • Click on Settings.
Settings

Settings

  • In the right pane of Settings go to Editor > Inspections.
Editor > Inspections”><figcaption>Editor > Inspections</figcaption></figure>
<ul>
<li>Expand the <strong>Python</strong> dropdown.</li>
</ul>
<div style=Python drop-down

Python drop-down

  • Look for the Unresolved references option in the dropdown list and deselect the checkbox.
Deselect Unresolved references

Deselect Unresolved references

  • Click Apply button and PyCharm will disable showing the Unresolved references warning altogether.
Apply the changes

Apply the changes

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.

Tip: Place the caret where the issue is and press the Alt+Enter shortcut to see the suggested fixes by PyCharm.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads