vscode "no refactorings available" for python - python

Ctrl + alt + R for extension python.python 2020.1.58038 always gives me a "No refactorings available" message.
Probably a configuration issue but I've tried several times to uninstall/reinstall and restart vs code to no avail.
Thought maybe I was missing python-rope but python -m pip install --upgrade rope
Requirement already up-to-date: rope in c:\users\cdoyle\appdata\roaming\python\python37\site-packages (0.16.0)
VS Code Version info :
Version: 1.42.0 (user setup)
Commit: ae08d5460b5a45169385ff3fd44208f431992451
Date: 2020-02-06T10:51:34.058Z
Electron: 6.1.6
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Windows_NT x64 10.0.18362
Also I don't see any open issues
https://github.com/Microsoft/vscode-python/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+refactoring
Not sure what to try next.
Thanks

I'm new to programming Python in VS Code and ran into the same problem. I successfully used the "Rename Symbol" command to rename a function and update all its references. More detail at https://code.visualstudio.com/docs/editor/refactoring . Hope this helps!

I would like to add further:
If you want to change a variable name / function name at one go, you can use "Rename Symbol" in VS Code editor by right clicking that particular name.
If your code has something related to both variable name and function name and you want to apply the change at both places, then you can use "Change all Occurrences".
Example of above-
test = "Hello"
def test_func():
print("World")
print(test)
test_func()
If i want to change variable "test" to "new" and function "test_func" to "new_func", i can use "Change all Occurrences".
But if i want to change only say variable name, then i have too use "Rename symbol".

Visual Studio Code and its Python extension is (at the time of writing this) very limited when it comes to refactoring. Out of the box it provides two refactoring actions: Extract Method and Extract Variable. The reason you're seeing "No refactorings available" is because the currently selected text doesn't match any of these actions. Try selecting a line of text and then press [Ctrl][Shift][R].
Rope support was removed in VSCode in favour of Language Server Protocol integration which is used by their Pylance language server extension. Pylance is now installed automatically with vscode-python extension, and it's actively developed - maybe they will implement more refactorings in the future. If you're looking for more robust refactoring, check out PyCharm refactoring, it is currently much more sophisticated.

For Python 3.10.2 use right-click, Change All Occurrences (CTRL-F2) on VSCode version 1.65.2

I think #Wayne Barrrass-Brown's answer is the best answer available, but just wanted to add something I've found helpful in similar situations.
Double click the variable you want to change, then CTRL+D (which will select the next occurrence of that text) until you've selected all the occurrences you want to change, then type in the new text you want for the selected variable/text.

Related

How can I programmatically uninstall my MSIX python app?

I've just written my first MSIX app in python 3. I use pyinstaller to generate an EXE. Then I use WiX Toolset to generate an MSI. I then use MSIX Packaging Tool to create the MSIX. There's probably an easier way to go from code to MSIX, but that's what I've gotten to work so far.
Ideally, I'd like to capture an onuninstall event and throw a GUI prompt asking the user why they are uninstalling. I can do this in the MSI. However, my understanding is that MSIX offers no onuninstall event. Please let me know if you know differently!
Since I apparently can't catch the MSIX uninstall event, my next preference is to offer the user a way to uninstall the app from the tray icon. The user selects an uninstall tray menu button from my app's icon, which pops up a window where the app then asks them why they are uninstalling. They type in an answer, and then click the submit button. Then, the app should completely uninstall itself. This also works well in the MSI. However, I can't get it to work in the MSIX.
Here's what works in python with the MSI installed:
subprocess.call('msiexec.exe /x {' + myguid + '}', shell=True)
However, the MSIX, which is built from the MSI, throws this popup error message when that line runs, and never actually uninstalls the app:
This action is only valid for products that are currently installed.
I've tried using the GUID from my WXS file's <Product> entry, hard-coded, just to see if it would work. That one worked for uninstalling the MSI, but not the MSIX. I also tried getting the GUID dynamically, but that didn't work for either MSI or MSIX, both producing the same error as above. Here is how I got the GUID dynamically:
from System.Runtime.InteropServices import Marshal
from System import Reflection
myguid = str(Marshal.GetTypeLibGuidForAssembly(
Reflection.Assembly.GetExecutingAssembly()
)).upper()
While running the MSI (where I have far better logging than in the MSIX), it appears as though GetExecutingAssembly() gets an assembly with a FullName of Python.Runtime, which is certainly something I don't want to uninstall. GetCallingAssembly() produces the same result. GetEntryAssembly() produces a null value.
I looped through AppDomain.CurrentDomain.GetAssemblies() to see what was listed and my app was not listed, though I saw many libraries that it uses.
So, any thoughts on how I can get the app to programmatically uninstall? Maybe a suggestion on how I can get the correct GUID for the MSIX app, if that's the problem? DotNet code should be fine. I can probably figure out how to translate it to python.
Or better yet, any idea how I can catch the MSIX uninstall event and run some custom code?
Thanks in advance!
From what I know at this moment catching the uninstall event is not possible. I don't recommend implementing the approach you suggested (the tray icon) but to ask your question, you can use the MSIX PowerShell commandlets to install and uninstall MSIX packages programmatically.
Also, I noticed you are really torturing yourself to create the MSIX package.
The MSIX Packaging Tool was created by Microsoft for IT professionals that don't have access to the source code. Developers can either use the Windows Application Packaging Project project template (if they use Visual Studio) or other third-party tools, like Advanced Installer or Wix (from what I know there was a Wix extension that can be used to build MSIX packages).
Here is a quick tutorial on how to create an MSIX from scratch, easier with Advanced Installer:
https://www.advancedinstaller.com/create-msi-python-executable.html
Disclaimer: I work on the team building Advanced Installer.
I used Bogdan's Powershell suggestion and came up with the following code, which seems to work well:
import subprocess
powershellLocation = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
try:
# Use the name of the app (which I found manually using Get-AppPackage)
# to get the full name of the app, which seems to have some random numbers
# on the end of it.
powershell_tuple = subprocess.Popen([
powershellLocation,
"Get-AppPackage",
"-name",
'"' + myAppPackageName + '"'
],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE).communicate()
appStrings = powershell_tuple[0].decode('utf-8').strip()
except Exception as e:
pass
for appStr in appStrings.splitlines():
# Find the full name of the app
if appStr.startswith('PackageFullName'):
colonIndex = appStr.index(':') + 1
fullName = appStr[colonIndex:].strip()
if fullName:
# The MSIX package was found, and I now have its full name
subprocess.call(powershellLocation + ' Remove-AppPackage -Package "' + fullName + '"', shell=True)

How do I import Pandas library into PyCharm?

I have downloaded Pandas library with pip install pandas through the command prompt, when I try to import pandas as pd PyCharm returns an error : ModuleNotFoundError: No module named 'pandas'
I have tried to uninstall and install again many times but nothing seems to work. Does anybody know a solution to this?
You can try downloading the library from PyCharm settings:
File -> Settings
then, Project: -> Python Interpreter
Click a + sign to the right,
Search for the pandas library,
and finally, press 'Install Package'
I think you have to choose the right python interpreter. Check my screenshot
You likely have multiple copies of Python installed on your system. PyCharm can be configured to use any version of Python on your system, including any virtual environments you've defined. The solution is to match up the version of Python you've installed Pandas into with the version of Python that PyCharm is using to run your code.
There are two places where you specify a Python version. First of all, your Project has a version associated with it. Check the "Python Interpreter" section of the "Project" section of your Preferences for that. That version is used for syntax highlighting, code completion, etc.
By default, the abovementioned Python version will also be used to run your code. But you can change the version of Python that your code is run with by creating or modifying a Run Configuration. To do this, check the menu next to the Run and Debug toolbar buttons near the top-left of your PyCharm window.
When you do get into the Python Interpreter section of the Preferences, you'll find that you can see all of the modules installed for each Python version that PyCharm knows about. You can use this to check to see if Pandas is installed for a particular Python version.
I would suggest you get comfortable with all that I've said above. It will save you many headaches in the future.

eric6 can't compile in Python form (could not start pyuic5.exe)

I am trying to create a GUI using eric6 for a Python script I coded recently, however when I right click in eric6 on the new *.ui file I create and I try to compile it (choosing 'Compile Form'), the following error is displayed - could not start puyic5.exe:
I have tried the (very few) online solutions but without any luck (for example, checking that in the pyuic5.bat file, the mentioned directory is without any space, and also tried to re-install Qt5). Furthermore, even though both Python and PyQt are correctly installed, pyuic5.exe is missing in the directory specified in the error. Also, everything seems setup ok in the PATH in the system environmental variables.
Versions info:
Python 3.4.5
Qt 5.4.1
PyQt 5.4.1
eric6 17.06
EDIT: if it can help debugging my issue, I would like to add that if I open a command prompt and I invoke pyuic5 mainform.ui -o mainform.py it works without problems. Instead, in eric6, such error message is generated.
Thank you in advance for any precious help and guidance you may offer !
Sincerely,
Paolo
Solved downgrading to an older version of eric6 (precisely to v 17.04.1). Thought it might be useful to share in case somebody encounters the same problem.

How to enable intellisense for python in Visual Studio Code with anaconda3?

I've been using Visual Studio Community for a few months now, but the sluggishness and project-based interface is not to my liking and having seen that Visual Studio Code has a python extension, I decided to try Code again.
There are two problems with Code, that if possible to solve I'd promote it to my main editor.
First is intellisense. I have downloaded the python extension for Code, which promises intellisense. However, outside of the core python packages (like "math") intellisense is not working. I've tried to set up the extra environment variable as described in the link, but that didn't work either.
After this I re-crated an anaconda environment, just in case. Each anaconda environment basically operates as a separate python install and has the modules like "scipy" etc under Lib/site-packages to properly be able to reference them.
As you might guess, intellisense for VS Code still isn't working. Maybe there's a way to refresh its database like for VS Community but I couldn't find it.
My second issue is lack of auto-indentation for python, which wasn't the case when I first installed Code some 6 months ago. The issue was present 2-3 months ago, I hoped it was fixed by now but apparently not. There's nothing new written about it on the Internets either.
How do I get VS Code to work well for me with python?
I have installed python 3.6.5 and Django 2.0.4 on windows 10 OS.
In vs code open command palette (Ctrl+Shift+p).
There select Python:Select Interpreter
Close your vs code and then try. If it still does not work. Try again by changing to:-
Python:Build Workspace symbols and re-open the vs code. That's all i did and got intelligence enabled.
Old question that worth another working answer:
In settings.json, set the value of python.jediEnabled to false (see some more details here).
P.S. You can also try and move back to Jedi (just remove python.jediEnabled or set it to true), and in my case, Intellisense keeps working, though much slower.
This is how I got Python intellisense to start working in VS Code:
open up the Terminal (View menu => Terminal)
type conda init powershell
restart VS Code
After that, Python intellisense started working correctly for me.
On your VS code use the command ctr + shift + P then search for Python: Select Lint and choose your preferred linting tool. This worked for me.
Bro, instead of
import matplotlib.pyplot as plt
Try:
from matplotlib import pyplot as plt
I've tried it in VSCode and it works for the second statement but not the first statement.
This is how I got it to work:
Open command palette (Ctrl + Shift + P).
Type "Python: Build Workspace Symbols" and hit Enter.
Done.

Running Python 3 from Light Table

I am trying Light Table and learning how to use it. Overall, I like it, but I noticed that the only means of making the watches and inline evaluation work in Python programs uses Python 2.7.8, making it incompatible with some of my code. Is there a way to make it use Python 3 instead?
I looked on Google and GitHub and I couldn't find anything promising.
I am using a Mac with OS X 10.10.2. I have an installation of Python 3.4.0 that runs fine from the Terminal.
I had the same problem with using a syntax that was only valid on Python3.3.
- Go to Settings:User Behaviour
- add the line (find the real path of your python binary):
[:app :lt.plugins.python/python-exe "/usr/bin/python3.4"]
- Save and test in your lighttable
It worked for me :)
Hope it helps
Hit Ctrl + Space to bring up the control pane. Then start typing Set Syntax and select Set Syntax to Python. Start typing your Python then press Ctrl + Shift + Enter to build and run the program.
I got the same problem. It worked for me after saving the file with a .py extension and then typing Cmd+Enter.
Besides changing
Ctrl+Space Settings:User Behaviour
[:app :lt.plugins.python/python-exe "/path_to_your_custom_bin/bin/python3.4"]
There is an issue with Unicode ( 'unicode' is not defined), and I also needed to modify ltmain.py to work with Python3 see here:
https://github.com/eduardflorinescu/lighttable_python3_patch/blob/master/ltmain.py
Make sure you backup the orginal ltmain.py, you can diff the two to see what are the differences and what was changed:

Categories

Resources