可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using PyCharm to work on a project. The project is opened and configured with an interpreter, and can run successfully. The remote interpreter paths are mapped properly. This seems to be the correct configuration, but PyCharm is highlighting my valid code with \"unresolved reference\" errors, even for built-in Python functions. Why don\'t these seem to be detected, even though the code runs? Is there any way to get PyCharm to recognize these correctly?
This specific instance of the problem is with a remote interpreter, but the problem appears on local interpreters as well.
回答1:
File | Invalidate Caches... and restarting PyCharm helps.
回答2:
Dmitry\'s response didn\'t work for me.
I got mine working by going to Project Interpreters, Selecting the \"Paths\" tab, and hitting the refresh button in that submenu. It auto-populated with something called \"python-skeletons\".
edit: screenshot using PyCharm 3.4.1 (it\'s quite well hidden)
回答3:
There are many solutions to this, some more convenient than others, and they don\'t always work.
Here\'s all you can try, going from \'quick\' to \'annoying\':
- Do
File
-> Invalidate Caches / Restart
and restart PyCharm.
- You could also do this after any of the below methods, just to be sure.
- First, check which interpreter you\'re running:
Run
-> Edit Configurations
-> Configuration
-> Python Interpreter
.
- Refresh the paths of your interpreter:
File
-> Settings
Project: [name]
-> Project Interpreter
-> \'Project Interpreter\': Gear icon -> More...
- Click the \'Show paths\' button (bottom one)
- Click the \'Refresh\' button (bottom one)
- Remove the interpreter and add it again:
File
-> Settings
Project: [name]
-> Project Interpreter
-> \'Project Interpreter\': Gear icon -> More...
- Click the \'Remove\' button
- Click the \'Add\' button and re-add your interpeter
- Delete your project preferences
- Delete your project\'s
.idea
folder
- Close and re-open PyCharm
- Open your project from scratch
- Delete your PyCharm user preferences (but back them up first).
~/.PyCharm50
on Mac
%homepath%/.PyCharm50
on Windows
- Switch to another interpreter, then back again to the one you want.
- Create a new virtual environment, and switch to that environments\' interpreter.
- Switch to another interpreter altogether, don\'t switch back.
回答4:
I find myself removing and re-adding the remote interpreter to fix this problem when Invalidating Caches or Refreshing Paths does not work.
I use vagrant and every once and awhile if I add a new VM to my multi-vm setup, the forwarded port changes and this seems to confuse PyCharm when it tries to use the wrong port for SSH. Changing the port doesn\'t seem to help the broken references.
回答5:
In my case it was the directories structure.
My project looks like this:
+---dir_A
+---dir_B
+app
|
\\-run.py
So right click on dir_b > \"mark directory as\" > \"project root\"
回答6:
If none of the other solutions work for you, try (backing up) and deleting your ~/.PyCharm40 folder, then reopening PyCharm. This will kill all your preferences as well.
On Mac you want to delete ~/Library/Caches/Pycharm40 and ~/Library/Preferences/PyCharm40.
And on Windows: C:\\Users\\$USER.PyCharm40.
回答7:
Tested with PyCharm 4.0.6 (OSX 10.10.3)
following this steps:
- Click PyCharm menu.
- Select Project Interpreter.
- Select Gear icon.
- Select More button.
- Select Project Interpreter you are in.
- Select Directory Tree button.
- Select Reload list of paths.
Problem solved!
回答8:
Sorry to bump this question, however I have an important update to make.
You may also want to revert your project interpreter to to Python 2.7.6 if you\'re using any other version than that This worked for me on my Ubuntu installation of PyCharm 4.04 professional after none of the other recommendations solved my problem.
回答9:
You might try closing Pycharm, deleting the .idea
folder from your project, then starting Pycharm again and recreating the project. This worked for me whereas invalidating cache did not.
回答10:
I finally got this working after none of the proposed solutions worked for me. I was playing with a django rest framework project and was using a virtualenv I had setup with it. I was able to get Pycharm fixed by marking the root folder as the sources root, but then django\'s server would throw resolve exceptions. So one would work when the other wouldn\'t and vice versa.
Ultimately I just had to mark the subfolder as the sources root in pycharm. So my structure was like this
-playground
-env
-playground
That second playground folder is the one I had to mark as the sources root for everything to work as expected. That didn\'t present any issues for my scenario so it was a workable solution.
Just thought I\'d share in case someone else can use it.
回答11:
It could also be a python version issue. I had to pick the right one to make it work.
回答12:
I closed all the other projects and run my required project in isolation in Pycharm. I created a separate virtualenv from pycharm and added all the required modules in it by using pip. I added this virtual environment in project\'s interpreter. This solved my problem.
回答13:
Geeze what a nightmare, my amalgamation of different StackOVerflow answers:
- Switch to local interpreter /usr/bin/pythonX.X and apply
- View paths like above answer
- Find skeletons path. Mine was (/home/tim/Desktop/pycharm-community-2016.2.3/helpers/python-skeletons)
- Switch back to virt interpreter and add the skeletons path manually if it didn\'t automatically show up.
回答14:
None of the above solutions worked for me!
If you are using virtual environment for your project make sure to apply the python.exe
file that is inside your virtual environment directory as interpreter for the project (Alt + Ctrl + Shift + S)
this solved the issue for me.
回答15:
In my case the inspection error shows up due to a very specific case of python code.
A min function that contains two numpy functions and two list accesses makes my code inspection give this kind of errors.
Removing the \'d=0\' line in the following example gives an unresolved reference error as expected, but readding doesn\'t make the error go away for the code inspector. I can still execute the code without problems afterwards.
import numpy as np
def strange(S, T, U, V):
d = 0
print min(np.abs(S[d]), np.abs(T[d]), U[d], V[d])
Clearing caches and reloading list of paths doesn\'t work. Only altering the code with one of the following example patches does work:
- Another ordering of the \'min\' parameters: schematically S U T V but not S T U V or T S U V
- Using a method instead of the function: S[d].abs() instead of np.abs(S[d])
- Using the built-in abs() function
- Adding a number to a parameter of choice: U[d] + 0.
回答16:
If you are using vagrant
the error can be caused by wrong python interpreter.
In our vagrant
we are using pyenv
so I had to change Python Interpreter path
path from /usr/bin/python
to /home/vagrant/.pyenv/versions/vagrant/bin/python
回答17:
If you want to ignore only some \"unresolved reference\" errors, you can also tell it PyCharm explicitly by placing this in front of your class/method/function:
# noinspection PyUnresolvedReferences
回答18:
Are you using virtualenv?
if so, you need to notify PyCharm for every change in the location of the the desired python.exe (merely ./activate is not enough for PyCharm)
Make sure Pycharm points to the correct interpetor and packages:
File -> Settings -> Project -> Project Interpreter. Click the gear and choose python.exe under virtualenv\'s Scripts folder