Friday, December 27, 2024
Google search engine
HomeLanguagesHow to fix Unresolved reference issue in PyCharm

How to fix Unresolved reference issue in PyCharm

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

RELATED ARTICLES

Most Popular

Recent Comments