Cannot find declaration to go to, error in PyCharm
Asked Answered
G

8

34

I changed my project code from Python 2.7 to 3.x.

After these changes when I hover over any method and press Ctrl I get a message:

cannot find declaration to go to

I'm trying to update PyCharm from 2017.3 to 18.1, I removed the .idea directory but my issue still persists.

Gunner answered 10/4, 2018 at 9:23 Comment(5)
Hope this could help: #25696303Sumach
I tried this solution but did not not fixGunner
Did you update/set your project interpreter and its libraries for python3?Foulard
I changed project interpreter to python 3Gunner
Solution: #37891688Gunner
C
85

Right click on the folders where you believe relevant code is located ->Mark Directory as-> Sources Root

Note that the menu's wording "Sources Root" is misleading: the indexing process is not recursive. You need to mark all the relevant folders.

Cartel answered 14/5, 2018 at 16:37 Comment(2)
Note that for Django projects, you only have to mark the Django root directory as "Sources root" (the one that has manage.py) and it will automatically detect your project structure.Volant
I had to invalidate my cache and reindex the project for these changes to take effect: "File" -> "Invalidate Caches..." -> "Invalidate and Restart"Suspensor
U
11

I had same issue and invalidating cache or reinstalling the app didn't help. As it turned out the problem was next: for some reasons *.py files were registered as a text files, not python ones. After I changed it, code completion and other IDE features started to work again.

To change file type go Preferences -> Editor -> File types

Unpopular answered 1/2, 2022 at 22:21 Comment(0)
B
2

The solution for me: remember to add an interpreter to the project, it usually says in the bottom right corner if one is set up or not. Just an alternate solution than the others.

This happened after reinstalling PyCharm and not fully setting up the ide.

Bellow answered 27/8, 2021 at 21:32 Comment(0)
G
1

I had a case where the method was implemented in a base class and Pycharm couldn't find it.

I solved it by importing the base class into the module I was having trouble with.

Goingover answered 10/10, 2018 at 22:6 Comment(0)
R
0

Go to settings. --> Plugins. Uninstall the unnecessary plugins. In my case I had 5 plugins. I disabled and uninstalled all plugins except Intellibot plugin. Others create unnecessary extensions.

Then, go to Settings. --> File Types --> Select Robot Featured Files. Add ".resource". Then Ok. Again, Add ".robot". Then Ok.

Apply and Ok settings. Wait for indexing or sny other other process on applying changes. Maybe after a minute restart your pycharm.

This worked for me.

(There is a star sign before .resource and .robot. Not sure why it didn't appear when i posted answer).

Regeneration answered 6/1, 2023 at 17:14 Comment(0)
G
0

I can report the same problem: after one or more updates of PyCharm Community Edition, the code navigation features do not work any more.

As we use a Community Edition, changes without testing in all productive environments are quite common.

Workarounds

They can be combined at will.

Not destructive

In PyCharm's Project view, right-click your sources directory and select Mark directory as -> Sources Root (see mithunpaul's answer)

Little destructive (removes Python virtual environment)

  1. Shutdown all running PyCharm instances.
  2. Remember all packages of your Python virtual environment, e.g., in a requirements.txt file.
  3. Backup and remove the Python virtual environment in your project home, e.g., by mv venv/ /tmp/.
  4. Restart PyCharm and rebuild the virtual environment.

Medium destructive (removes project settings)

  1. Shutdown all running PyCharm instances.
  2. Backup and remove the project folder in your project home, e.g., by mv .idea/ /tmp/.
  3. Restart PyCharm and reload the project.

Most destructive (removes all PyCharm settings)

  1. Shutdown all running PyCharm instances.
  2. Backup and remove the PyCharm folder in your home, e.g., by mv ~/.config/JetBrains /tmp/.
  3. Restart PyCharm and re-create the project.

Combining all destructive workarounds should work. If not, you should post a bug report.

Godderd answered 11/12, 2023 at 12:57 Comment(0)
P
0

In my case, restarting PyCharm from the File menu fixed the issue.

Go to File > Invalidate Caches...

Then click on Just Restart.

Previously, I just had to configure the Interpreter for my project. See Axisnix's answer

Polak answered 22/3 at 11:26 Comment(0)
H
0

I also got this error. However, it affected only part of the code, not all of it.

Then I noticed that I had the following line of code somewhere in the middle of the script:

sys.exit()

with the intention of forcing the script to stop at that line.

I commented that statement, and the navigation to all declarations started working for all code below.

It looks like PyCharm has a logic not to navigate to declarations for code that is not reachable.

You should check if your code is reachable at the line where you are using a declaration you want to navigate to.

Hua answered 3/7 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.