Visual Studio Code (Mac OS) rename symbol doesn't work - python

When I right click and try to rename a variable name in Microsoft Visual Studio Code on Mac OS Mojave, it prompts for the new name, I hit enter and nothing happens.
I have Python extension and Latex extension installed.
Usually there are no errors, no nothing.
Sometimes, there's a little box that pops up saying "No Result. No Result".
The python interpreter I selected was a Conda install.
I ensured rope, and pylint were installed.
Expected behaviour:
right click > rename symbol > type new name > enter > all instances of variable renamed.
Observed behaviour
right click > rename symbol > type new name > enter > variable has same name everywhere, including spot of renaming.

It turns out the solution was simple, but frustrating. To do refactoring in VS Code (at least for python) you need to be in a workspace.
I solved the problem by first closing the open folder I was in:
File > Close Folder.
then navigating to a sub folder of .py file I was editing, and opening it. In my case it was
Click the little document icon in the upper left of screen
In the welcome screen, choose "open folder"
navigate to my desktop folder (where my .py file is)
Now refactoring works as expected
Click on file icon in upper left of screen > in the welcome screen

I use Pylance with a workspace and had the same issue. For me the solution was to restart the language server (from the Command Palette). I find it faster than closing and opening the folder.
Ctrl+Shift+P > Python: Restart Language Server

Choosing a folder did not solve the issue for me. I switched to a new language server called Pylance and everything works fine now.

I just found out that if you use Pylance (Pyright) and have a pyrightconfig.json or a [tool.pyright] section in pyproject.toml, this can cause refactoring to fail.
In my case I had not put the localtion of the script into the include array in pyproject.toml so it was giving the "No results" error.

In my case the solution was to reset the Python Language Server setting:
"python.languageServer": "Default",

Related

VS Code Opens Folder of Modules

I, probably, have a weird question but it really annoys me. I have created Venv and installed some modules. When I click the module file to scroll it, VS code shows the location of module file in the side explorer. I guess this feature can be turned off in preferences; but, unfortunately, I cannot find it.
Add the following code to your setting.json:
"explorer.autoReveal": false
Then vscode will not open the floder when you open or click a tab or other things.

Visual Studio Code, ReStructurdText extension can't find Python

I'm trying to use the LeXtudio ReDtructuredText extension for Visual Studio Code. It's not working, and the reason is not clear.
After I installed the extension I tried to use it by clicking the "split screen" icon below the menu bar, and I got a popup message that said, "Please review Python installation on this machine before using this extension."
I clicked the "Learn more" link and learned that the extension depends on Python 3.5 or later. My system runs Python 2.7 because our production environment uses tools that depend on it, but I have Python 3.8 installed too. I followed a "learn more" suggestion and added python.pythonPath to the VS Code settings file. Then I tried again, and got the same message.
Did I modify the settings file right? I think so; here's how it looks so others can check. (Line 2 has been added.)
{
"python.pythonPath": "C:\\Program Files\\Python38\\",
"editor.minimap.enabled": false,
"breadcrumbs.enabled": true
}
Did I put the right value in the string? I think so; when I examine C:\Program Files\Python38\ in File Explorer I see that it's a Python installation's top level directory, and python.exe is in it.
Did I modify the right file? I did; when I changed "breadcrumbs.enabled" from true to false, the editor's breadcrumb display disappeared instantly -- I didn't even have to restart the editor.
Any ideas?
The extension instructions also recommend using Microsoft's Python extension to change the setting. It says that this "really makes configuring Python environment easier," although it doesn't explain how.
It says, "Once you have that Python extension installed, open conf.py in VSCode." It's not clear what this is for, since the settings file is named settings.json, but I hoped that when I opened the file that would become clear. But the instructions don't say where this file is, and I can't find it. I searched my user directory and both Program Files directories, and the only files I found with the name conf.py were in Sphinx projects... not a likely place to put VS Code settings.
Any ideas?

problem on setting up python on visual studio code

After installing Python and Visual Studio code as followed on this link:
https://www.youtube.com/watch?v=dNFgRUD2w68&lc=UgxjG9Bd_Uwm9duwpld4AaABAg
I can't see the reload button in the VS but there are two button; one is disable, and the other is uninstall. Do you know why I don't have reload button and how to resolve this issue?
You don't have to reload the page. Python is already installed.
Just close the current window near the "Extension: Python".
If for some reason VSCode still can't recognize python files, just close it and open it again.
After finishing your Python file in VS Code, save all changes, head to the terminal and enter the directory of the python file(A directory is where you've saved your file), and type python example_file.py.
Of course, you can change the name example_file.py to your actual file name.

PyCharm + SQLAlchemy , unresolved reference SQLAlchemy [duplicate]

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.
File | Invalidate Caches... and restarting PyCharm helps.
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)
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.
Create a new virtual environment in a new location -- outside of your project folder -- and switch to that environment's interpreter.
Switch to another interpreter altogether; don't switch back.
If you are using Docker, take note:
Make sure you are using pip3 not pip, especially with remote docker and docker-compose interpreters.
Avoid influencing PYTHONPATH. More info here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python .
If the above did not work for you, but you did find another trick, then please leave a comment.
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"
You have to mark your root directory as:
SOURCE ROOT (red),
and your applications:
EXCLUDED ROOT (blue).
Then the unresolved reference will disappear. If you use PyChram pro it do this for you automatically.
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.
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.
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!
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.
Much simpler action:
File > Settings > Project > Project Interpreter
Select "No interpreter" in the "Project interpreter" list
Apply > Set your python interpreter again > Click Apply
Profit - Pycharm is updating skeletons and everything is fine.
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
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.
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.
It could also be a python version issue. I had to pick the right one to make it work.
None of the answers solved my problem.
What did it for me was switching environments and going back to the same environment. File->Settings->Project interpreter
I am using conda environments.
Mine got resolved by checking inherit global site-packages in PyCharm
File -> Settings -> Project Interpreter -> Add Local Interpreter -> Inherit global site-packages
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.
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.
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.
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.
My problem is that Flask-WTF is not resolved by PyCharm. I have tried to re-install and then install or Invalidate Cache and Restart PyCharm, but it's still not working.
Then I came up with this solution and it works perfectly for me.
Open Project Interpreter by Ctrl+Alt+S (Windows) and then click Install (+) a new packgage.
Type the package which is not resolved by PyCharm and then click Install Package. Then click OK.
Now, you'll see your library has been resolved.
In PyCharm 2020.1.4 (Community Edition) on Windows 10 10.0. Under Settings in PyCharm: File > Settings > Project Structure
I made two changes in Project Structure:
main folder marked as source and
odoo folder with all applications I excluded
Screenshot shows what I did.
After that I restarted PyCharm: File > Invalidate Caches / Restart...
Unresolved references error was removed
Invalidating the cache as suggested by other answers did not work for me. What I found to be the problem in my case was that PyCharm was marking init.py files of Python packages as text and thus not including them in the analysis which means python resolving was not working correctly.
The solution for me was to:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files
or Find Text and delete __init__.py from the list of text files
To add yet another one: None of the solutions involving the Python Interpreter tab helped, however, I noticed I had to set Project Dependencies: In the project that had unresolved reference errors, none of the dependencies were checked. Once I checked them, the relevant errors disappeared. I don't know why some are checked to begin with and others are not.
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
I have a project where one file in src/ imports another file in the same directory. To get PyCharm to recognize I had to to go to File > Settings > Project > Project Structure > select src folder and click "Mark as: Sources"
From https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
Source roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports
I had to go to File->Invalidate Caches/Restart, reboot Ubuntu 18.04 LTS, then open Pycharm and File-> Invalidate Caches/Restart again before it cleared up.
For me it helped:
update your main directory "mark Directory as" -> "source root"
#kelorek works for me, but before, in interpereter paths I had to add some path.
lets say
from geometry_msgs.msg import Twist
is underline as error, then in remote machine in python run:
help("geometry_msgs")
at the end there will be path lets say :
/opt/ros/foxy/lib/python3.8/site-packages/geometry_msgs/__init__.py
so to Your intepreter pycharm path add
/opt/ros/foxy/lib/python3.8/site-packages
Hope it will help You and it helps me :)
I had the same symptoms. In my instance the problem source was that I had set
idea.max.intellisense.filesize=50 in the custom properties.
I could resolve it by setting it to 100.
Help->Edit Custom Properties

Eclipse: "Python not configured"

Here is a screenshot of whats happening:
Basically, I execute everything via command line but edit the source in eclipse.
This error prevents autocomplete which is annoying.
I have gone to window>preferences>PyDev>Interpreter - Python and set the correct values but they don't stay and even when I hit apply they don't seem to take for the current session.
Any idea on what could cause this error message?
What also works is RClick --> PyDev --> Remove PyDev Project Config.
Please note that the error marker may not be removed even after a clean/build, you may have te delete the marker manualy (just select it and delete) then clean, build and run
P.S.: I couldn't find the "project nature" option mentioned in the voted answer.
Go to Window → Preferences. In the Preferences window, expand PyDev and select Interpreter-Python. Click "New..." and type Python32 for the Interpreter name. For the Interpreter executable, browse to your copy of Python (C:\Program Files\Python32\python.exe), and press Open.
Project Properties > Project Natures and uncheck the python thing.
1. Select your project and right click and click on Properties
2. Select PyDev-Interpreter/Grammer,click on "Click here to configure an
Interpreter not listed" Link.
On Mac,
open eclipse application,
click preferences
click PyDev
Click Interpreter
Click Python Interpreter (below Figure 1)
Click Choose from List button (below Figure 2)
Select what ever Python version you want to use
Click Apply
Click OK
Exit Eclipse
ReOpen it and all fixed.
The permssion issue (mentioned for .pydevproject in "How to persist PYTHONPATH setting of an Eclipse Pydev project?") can easily happen in a DropBox settings when used by multiple users.
See for instance "How to fix Drop Box permissions issues in 10.5" (if you are on a Mac, but the general idea applies for other unix-like OS too)
The permanent fix to this problem is to add the proper ACL to the Drop Box folder.
To do that, open Terminal, and enter this command—note that you can’t just copy and paste it as is, as you need to replace the two occurrences of short_username with your user’s actual short username.
For that reason, I suggest copying this into TextEdit, editing it as needed, and then pasting it into Terminal.
chmod +a "short_username allow list,add_file,search,delete,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,writesecurity,chown,file_inherit,directory_inherit" /Users/short_username/Public/Drop\ Box
After running this command, you should find that your Drop Box works as expected, and you have full rights to files that others leave for your use

Categories