I'm using Pycharm (2019.1.2 x64) from quite a while and I never changed the standard settings.
The standard settings diplay keywords (def, class, if) in orange, pressing tab will auto complete the keyword and add a space, build-in functions (sum(), map(), print()) are displayed in purple and pressing tab will auto complete the function name, add brackets and move the cursor inside the brackets.
This is the desired behavior and it's what I get when opening any project in Pycharm except one.
In a single project this behavior suddently changed, I did nothing but writing and running code.
I noticed because tab suddently started indenting by 8 spaces instead of 4, no big deal I changed it back to 4 in the bottom of the IDE, but I also noticed that print was seen as a keyword (so orange with no brackets) instead as a built-in function, just like in Python2 (I never used it and the project's interpreter was Python 3.7). from __future__ import print_function would turn print into a function again but it's Python2 stuff.
I fixed it by creating a new virtual environment and setting up the interpreter again, so my question is:
What causes this unexpected behavior?
I would like to avoid this when working on my most "serious" projects. Thanks all.
NEW ANSWER
Well, this happened to me just yesterday while I was working on my tkinter project. It showed tkinter in from tkinter import * "Module not supported in Python 2.7", even though I was using Python 3.8.
Solution One
To fix this issue, I just simply went to
File > Settings > Appearance and Behavior > System Settings > Updates
And updated PyCharm to the latest one. It worked for me.
Solution Two
Another method I found out is to go to
Press Shift Key Twice > Typing "Restore Default Settings"
Doing this will also restore all the settings partially and fixes the problem for me.
Why Did This Happen?
This is due to a bug in older versions of PyCharm (probably). I got this bug while testing some Google API that was in Python 2.7. If you ever opened a project containing Python 2.7 Interpreter and opened another project that is in Python 3 and above, PyCharm thinks that the code that is written is in Python 2.7 and just formats the guide according to it. Some example can be "f" in print(f"{some_variable_here}") being shown as "Not Supported In Python 2.7". This is just my theory I may be completely wrong here.
OLD ANSWER
Are You Using macOS? if yes, macOS comes with Python 2 preinstalled. So probably this may be a bug. I suggest going to the bottom right corner and there would be written "Python (Version)". If it is something other than 3 or above (like 2), click on it, and go to "Interpreter Settings" and there, from the drop down menu, select the other Python interpreter that is installed on your device.
If this does not work, then it may be a theme bug, or something to do with PyCharm itself (probably). Reinstalling and clearing the cache should probably work.
Related
The language interpreter is set to a Python 3.9 version:
But a Python scratch file is being parsed by some kind of 3.6 interpreter:
Note that I created in two different scratch files and the same error occurs. Why would this happen and is there a workaround [short of creating an entirely new project from scratch]?
I am on Pycharm Professional 2021.3.1
Update based on answer by #TurePaisson he though maybe the Code is compatible with specific Python were set. That was a shrewd guess - but turns out I have not set that:
Update The following snippet can be used to test python3.6 vs 3.8+
x = (y := 3) + 7
In the preferences dialog, search for "versions" and you will find, under Editor/Inspections, an inspection "Code is compatible with specific Python versions" with checkboxes for which versions to check against.
I ended up creating a new python interpeter with version 3.9 and selecting it for the project interpreter. There's clearly some bug within Pycharm that it does not always cleanly switch among interpeters/versions - so starting from scratch is sometimes apparently needed.
I'll start by saying that I tried googling the exact error message and there aren't any significant hits (that's reason alone to post this leaving it as a roadsign):
"Python version 3.6 does not support assignment expressions"
I tried this with PyCharm versions 2021.2.2, 2021.2.4 and 2022.1 Pro (switching between Python 3.6 and Python 3.9) but I wasn't able to reproduce the exact problem that's shown in the question (I was unable to update to the exact 2021.3.1) and there might be some Project setting that's not obvious.
So I have 2 hunches:
It's possible there's something stuck with the inspection profiles since the OP seems to be working from a repository.
From experience my next guess would be to try and change the interpreter version of the "run configuration" because it's not stated explicitly in the documentation if it supersedes the project-wide interpreter chosen in Settings > Project > Python Interpreter for linter inspections. From my tests it currently doesn't but apparently it worked for the OP... Maybe by rebuilding the index and refreshing the background linter process.
Following up on #bad_coder 's attempt to fix that will be paraphrased as:
check the Run Configuration for pointing to a different python interpreter than the project level one
That fix worked for me:
Bring up the Run [Context menu] | Edit Configurations
Change the Python interpreter to the appropriate one: Shown below is a case where the interpreter is said to the earlier language level. Go to the dropdown and select a correct [python 3.8+] interpeter.
Is there a simple way, preferably in PyCharm (2017.1) but via command-line python (3.5) if necessary, to detect all code places where a statement is referring to an unresolved reference, e.g. because an import statement is missing?
I am new to Python/PyCharm. More generally, any syntax errors or anything in a similar vein would be a bonus. All I am looking for is the kind of errors I would get if I were "compiling" and "linking" in another language.
I have looked at Can PyCharm list all of Python errors in a project? and PyCharm's "Inspect Code". It is way more complex than I had in mind (and takes ages to run). I see that Python Rope: How to Find all missing imports and errors in all sub modules refactoring recommends pylint, but I wasn't looking for lint-like. I just want darn-obvious errors!
I am tasked with porting a fair-size (32K lines) application, which (apparently) runs under Windows, to Linux. The first thing I want to do is get rid of some of the imports all over the place. If my application executes a line which then has an unresolved reference I get a runtime error, but I want to pick them all up at edit-time. And there will be paths of code which are Windows-only, but I still want to know of any errors like this.
To answer my own question:
From Can PyCharm list all of Python errors in a project?, you can indeed use Code|Inspect Code to get all these errors/warnings in PyCharm as a list where you can click to get to the code. It does take a long time, but at least it's built into PyCharm, and the errors reported correspond to what you see in the editor window.
Pycharm will list all errors and 'warnings' for each source file at the right-hand side of the editor window.
They are represented as short lines or small blocks, depending on the size of the error or 'warning'. Errors are shown in red.
You click on them to take you to the place of the problem in the source.
Warnings are mostly Python style-guide violations (PEP 8).
Another option is to use pylint. This is lint for Python. This will detect missing imports.
You can integrate it into PyCharm by following the instructions in https://stackoverflow.com/a/46409649/4459346
I recently upgraded to Python 2.7.10 on MacOSX Yosemite. I also added a Python 3.4.3 installation. At the time I upgraded Tcl/Tk with ActiveTcl 8.5.18 as was suggested on the Python for MacOSX installation page. There is also an ActiveTcl 8.6 package from last year that still shows up in the framework, but I was pretty sure that I said to delete that when I went back to an earlier version of 8.5. Path has settings for all three as well as usr/bin, etc.
At this point, Autocomplete and Call Tips stopped popping up. I checked the preferences and it seemed to be turned on. The delay was set to 2000, so I changed that to 2, but nothing happened. Pressing {ctrl}-Spacebar and then using the down arrow key lets me cycle through the the available functions, but putting a dot after an object has no effect, and if I open parentheses, I get no tips. This is both in the IDLE Shell and Editor windows.
I tried to redo everything by deleting all instances of Python from my drive. I reinstalled them, reloaded all packages, and Autocomplete and Call Tips still are not popping up. Is there any known issues or is there a workaround? Do I possibly need to add or change the new Tk path somewhere so that IDLE can find what it needs to popup autocomplete? Is there a way that I can initiate IDLE Autocomplete and Call Tips during the coding session to manually get them to work?
I have also raised this with ActiveState and Python, but I haven't gotten or found an answer yet that has gotten it to work. I like working with IDLE and would like to get Autocomplete and Call Tips up and running again.
Thank you.
I've been using Eclipse with PyDev (on Windows, mind you) for my Python Tkinter project for about a month now, and up until recently I've had no complaints. I start the source for one module (my GUI) like so:
from Tkinter import *
Now, ever since I updated Eclipse to 3.6.1, it shows an error for every call to the Tkinter module(Frame(), Label(), Tk(), etc.). I thought that something might have gone wrong during the update, so I uninstalled Eclipse, re-downloaded it (Eclipse 3.6.1 Classic), and reinstalled it. Then, seeing that that gave me a bunch of errors with installing PyDev, I removed that, got Eclipse 3.6.0 Classic, installed it, and got the same problem I was trying to fix earlier.
I don't understand what's wrong here. It doesn't make any sense to me why all of a sudden Eclipse would stop recognizing that I had imported Tkinter.
Can anyone offer any suggestions/input? I really want to stick with Eclipse, I'm very happy with it, but I can't deal with it giving me about 200 bogus errors.
You really shouldn't use wild imports. Consider from Tkinter import Frame, Label or import Tkinter as tk instead.
Now to your problem: I have Eclipse Helios, too (3.6.0.v20100602). The problem you describe clearly has to do with PyDev, not with the Eclipse base project. I just tried the same import and didn't get error messages. My PyDev version also retrieves the docstring of Frame correctly, for example.
I think it boils to just installing a newer version of PyDev (this is the solution for many bugs ^^) - add "http://pydev.org/nightly" as an update site and then upgrade to the latest version. I have version 1.6.3.20100922, by the way. If that doesn't help, you should consider reporting it as a bug.
I find the answer to problems like this is usually that you have to update your pythonpath from eclipse. See my answer to a similar question. I think re-installing pydev and/or eclipse usually solves this problem because in doing so, the pythonpath is re-configured.
Which Python version do You use? If > 3.1 try (note lowercase writing):
from tkinter import *
I am using PyDev/Eclipse for several monthes and I get ever and ever the same bugs with imports:
PyDev underline in red an import and say Unresolved import xxx ; Found at yyy. When I click on yyy eclispe find and open the implementation of the module.
(PyDev just inform me that it can't find the module xxx and in the same message that it can find it !)
The module xxx is in the PYTHONPATH of eclipse. When I "explore" the interpreter of the project, I can find it without any problems.
When I try to execute (from eclipse), I don't get any error and it works fine.
Sometimes, the error message will stay for several days and will disappear. Sometimes, it won't. I've tried to refresh the projects but it has not impact on that.
Somtimes, it works well on a project and I can use autocompletion and it don't work in another project (same interpreter) ...
I just can't understand what is happenning ?
So far, I have ignored these bugs because everything was fully fonctionnal but sometimes, it is a bit disturbing to have red markers "errors" when you are working.
Did you find a way to avoid these bugs in PyDev ? Is it "normal" ? Is there a way to force PyDev to "refresh" ?
Thank you.
References :
python 2.4.4 (built from sources)
PyDev v 1.6.0 2010071813
This can happen if new modules are not cached by PyDev. For example, on my new laptop I first set up PyDev/Eclipse and later installed the Django package. That's why Django imports were marked as unresolved. You can refresh it using Pydev > Interpreter - Python > Libraries > Apply. Select the interpreter you want to "restore" (they could have chosen a better word) and click OK. PyDev will then reparse all installed modules.
I'm using the nightly version of PyDev, but any 1.6.x version should work correctly. If that doesn't help, remove the interpreter configure and create a new one.
You can try refreshing your PYTHONPATH in Preferences > Pydev > Interpreter - Python and selecting AutoConfig for your interpreter, then manually choosing the libraries for your PYTHONPATH.
This is pretty radical solution though. Making an insignificant change (like adding a space) and saving file should work in most cases. If not, you can also try temporary delete and then re-add the imports in file that is causing problems.
Just in case anyone else runs into this thread but above answers don't solve the issue, make sure that your script does not have the same name as the library that you are trying to import.