VSCode mouse-hover on Python Keywords not working - python

While beginning to teach a friend how to code in Python, I observed something in his VSCode that was different than my own - he could mouse-hover on a Python keyword such as as import or def, and it would show the Python Manual definition for the keyword. My own VSCode does not do this.
I have tried everything I can think of to find why this won't work for me, and I'm at a complete loss. I have compared my installation process to my friend's, followed the exact same steps I provided him for setting up his environment, and do not get the same results. I tried completely removing Python, VSCode and all residual VSCode files from my laptop, and reinstalling in the same order he did, still to no avail.
I followed those same steps on a different laptop at work, and I can get the MouseHover to work there. The only feasible difference I can think of is that my work machine is not using the same Windows account as both my home PC and laptop - but I do not have Settings Sync enabled so I can't see why that should matter anyway.
Any thoughts for what I'm overlooking to get this functionality working properly in VSCode for myself? I don't particularly need it, but I'd like to be in the same environment as my friend while I teach him.
We're both on VSCode 1.55.2, Python 3.9.4, using a virtual environment created from VSCode's Powershell terminal, then restarted VSCode to get it to detect the virtual environment and use it as default for Terminals, and finally pip installed and enabled Pylint as the linter.
Editing to add steps followed - I just went through all of this again on my home laptop, after completely removing all traces of VisualStudio2019 and Code, and Python. Note: this was on Windows 10, with all updates applied
Install VSCode
Install VSCode Python extension, MS version
Install Python 3.9.4
Open a new folder in VSCode, then open Terminal
python -m venv venv
create a something.py in the Explorer pane
VSCode should detect the venv and offer to load it. Say yes
VScode may also indiacte that pylint is not installed, Allow it to install, or manually pip install pylint from the venv.
in something.py type out import random
Mouse over import -- this is the discrepancy. On both of my home machines, there is no mousehover anything. on both of my friend's machines, as well as my machine at work, the following is shown:

With help from riov8's comment on the original post, I was able to compare the Experiment Groups that were in effect for each of the environments.
In VSCode, with a Python file loaded, switch to the Output panel (in same area as Terminal) and change the dropdown to Python.
Scroll to the top of this panel, and examine the Experiment Groups you belong to.
Most of the experiment groups do not apply to this issue.
Look specifically for either pythonJediLSP, pythonJediLSPcf or if
you have neither.
If you are experiencing the issue I describe, then at the bottom of this same panel, when you first mouse over a python keyword after starting VSCode, you will get errors similar to the following:
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the line is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
%UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the column is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Deprecated since version 0.17.0. Use the project API instead, which means Script(project=Project(dir, sys_path=sys_path)) instead.
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:626: DeprecationWarning: Deprecated since version 0.16.0. Use Script(...).infer instead.
script.goto_definitions(), request["id"]
The following scenarios resolved the issue for me:
User belongs to experiment group pythonJediLSPcf:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python> Experiments: Opt Out From, click Edit in settings.json
in the section "python.experiments.optOutFrom", add "pythonJediLSPcf" (quoted)
if it does not exist, add another section "python.experiments.optInto"
In the OptInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User does not belong to either pythonJediLSPcf or pythonJediLSP:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python › Experiments: Opt Into, click Edit in settings.json
In the "python.experiments.optInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User belongs to pythonJediLSP:
It was my experience that this was the experiment required in order for the feature to work properly. If you have this experiment enabled already, then ensure you do not also have pythonJediLSPcf. Disable it if you do, following the instructions in the first sub-heading. If it continues to not work, then there may be another conflict that I did not experience myself.
I am posting a GitHub issue regarding this problem and will update this answer with the link when done.

Related

Integrating Python and Jupyter Notebook with Visual Studio Code

Summary:
I am trying to set up the Jupyter Notebook extension for VSCode but I can’t get my Python code snippets to run. There is no "Run" button to select. I can't select a Python/ Jupyter kernel to work with.
The problem in greater detail:
If you check out this YouTube clip at exactly 1 minute and 46 seconds (here - - you can pause it to see the still) this is what I am trying to accomplish.
But here is what my VSCode actually looks like:
As you can see in my screenshot, the “Play” icon to the left of my code snippet is missing. Along the top border, the other “Play All” button is missing.
In Microsoft’s official VSC doc for setting up Jupyter Notebooks, right near the top under this specific heading: “Create or open a Jupyter Notebook” their interface has way more options at the top of the cells when compared to mine. They have buttons named: “Variables”, “Restart”, “Interrupt”. That is expected for Jupyter Notebook funcionality. Mine is missing all of that. Clearly there is something wrong with my setup.
Other SO users report having ms-python extension installed (which includes Jupyter functionality) in addition to stand-alone Jupyter Notebook extensions installed. Apparently in situations when you have multiple Python extensions installed, they can interfere with each other. So after much wrangling, I resolved to uninstalling all my Jupyter extensions and keeping only the main ms-python extension.
The issue persists.
Here is a complete list of all my VSC installed extensions:
$ code --list-extensions --show-versions
arcticicestudio.nord-visual-studio-code#0.19.0
batisteo.vscode-django#1.8.0
bibhasdn.django-html#1.3.0
Compulim.indent4to2#0.1.2
Equinusocio.vsc-community-material-theme#1.4.4
Equinusocio.vsc-material-theme#33.5.0
equinusocio.vsc-material-theme-icons#2.3.1
esbenp.prettier-vscode#9.8.0
formulahendry.code-runner#0.11.8
GulajavaMinistudio.mayukaithemevsc#3.2.3
icrawl.discord-vscode#5.8.0
janisdd.vscode-edit-csv#0.6.10
lonefy.vscode-JS-CSS-HTML-formatter#0.2.3
m-thorsen.vscode-materia#0.4.4
ms-python.python#2022.12.1
NikolaosGeorgiou.html-fmt-vscode#0.6.0
PKief.material-icon-theme#4.20.0
samuelcolvin.jinjahtml#0.17.0
streetsidesoftware.code-spell-checker#2.7.0
yzhang.markdown-all-in-one#3.5.0
zamerick.black-ocean#1.0.2
zhuangtongfa.material-theme#3.15.2
There is a bit of noise in that list. I'm not sure what all of them refer to. But for certain there is only one Python app (ms-python) and nothing related to Jupyter.
Some SO members elsewhere have suggested selecting / setting the base Python version/environment. When I click the “Select Kernel” button at the top right (as illustrated in the screenshot below), it reveals an empty menu (see at the top left):
Here is “Python v3.8.5 (conda)” showing as selected at the bottom and slightly to the left:
Another exhaustive question and answer thread elsewhere on Stack Overflow involving an issue similar to mine includes many, many creative potential solutions: jupyter server : not started, no kernel in vs code
In that Stack Overflow question, one prominent member answers:
How I solved it:
Press Command+Shift+P to open a new command pallete
Type >Python: Select Intepreter to start jupyter notebook server
Open the notebook again
Someone else suggests:
Press Command+Shift+P on mac, Ctrl+Shift+p on windows
Type Jupyter: Select Interpreter to start Jupyterserver
It would show you a dropdown of python versions installed.
I chose python 3.7.5 and it worked for me. you can choose the python version installed on your machine.
Another member says:
I have seen all possible solutions but not work, finally I just
upgrade jupyter, notebook, and jupyterlab, like pip install -U jupyterlab, and I can choose the kernel in VScode!
None of the above resolves my issue.
Here is a quirky new twist: After installing jupyterlab with pip, after restarting VSC, I encountered this weird error:
It’s a condition box. I clicked: “Yes”.
Jupyter is still not integrated properly.
UPDATED: Based on JialeDu’s answer, I followed his instructions. I created a video shared below. It doesn’t quite capture every step, but I did follow all the steps. What you can see in the gif video is that my current working directory is a folder called Test, a new terminal has been opened, and there is an active virtual environment with $ source .venv/bin/activate. Next, as you can see in the .gif, I am able to select the latest Python interpreter v3.10.5 showing as “Recommended”. So that is progress. I couldn’t do that before. Going the virtual environment route was a great idea. However, I am still not able to get the Jupyter Notebook interface as the end product shared by JialeDu. When I invoke Ctrl + Shift + P again and type > create jup a new Jupyter Notebook file is created but the kernel I selected previously does not show and the expected Jupyter interface buttons and options are still not showing. Here is the video demo in .gif formatting:
Please note: Here you can see a list of 4 available Jupyter extensions:
I avoided installing any of these separate Jupyter extensions in the store because according to the instructor (and as confirmed on the official Microsoft Python extension ms-python page which I already have), it includes Jupyter built in. From the ms-python extension entry, it explains: "This Python extension will automatically install the Pylance and Jupyter extensions to give you the best experience when working with Python files and Jupyter notebooks."
Even though other Stack Overflow users indicated that mixing the above extensions such as ms-python with ms-toolsai was a bad idea, I experimented with having them both installed anyway. Didn’t fix the issue. I tried uninstalling ms-python and keeping ms-toolsai. Still no dice.
By the way, I’ve also got the latest version (as of this writing) of VSCode v1.70.1 installed:
VS Code has prompted you to install the suggested extension. Python and Jupyter.
Also it helps to make sure your vscode is up to date.
The play button of the interface is provided by Jupyter, but if you want to run the code (select the kernel), you must also install the Python extension.
It’s a condition box. I clicked: “Yes”.
You don't have to click Yes because there is a time delay. By the time you click Yes, the environment may not be ready.
If you are using a virtual environment, you can select the virtual environment's interpreter and then create a new terminal to activate the environment.
Complete creation steps:
Prerequisites:
The machine has python installed,
and vscode has Python and Jupyter extensions installed.
steps
Create a new working folder Test
Open the folder Test with vscode
new terminal
Type the command python -m venv .venv to create a virtual environment
After the creation is complete, use the command .venv/scripts/activate to activate the virtual environment
Or Ctrl+Shift+P --> Python:Select Interpreter, select the interpreter of the virtual environment and then create a new terminal to activate the environment
Create new jupyter file using command palette command Create:New Jupyter Notebook
Or right click --> New File --> name ends with .ipynb
The virtual environment you just selected will be enabled as the kernel by default.
I discovered a very simple answer: I installed the VSC package in the official Snap repo.
VSC and Python-Jupyter look to be all playing nice together now:

Pycharm is not showing Pygame properties [duplicate]

I want to configure Pylint as an external tool in my entire project directory for a Python project that I'm working on. I've tried to use the repository as a module with __init__.py and without, and it's not working either way.
I'm having difficulty setting up Pylint to run with PyCharm. I know that I should be running it as an external tool, however the settings confuse me.
The authoritative source on their documentation is broken, so I can't check that up either.
You can set up Pylint to work with PyCharm by following the following steps:
Install pylint:
$ pip install pylint
Locate your pylint installation folder:
$ which pylint # MacOS/Linux
/usr/local/bin/pylint # This is just a possible output - check yours
<!-->
$ where pylint # Windows
%LocalAppData%\Programs\Python\Python36-32\Scripts\pylint.exe # Possible location
Open the PyCharm settings window with menu File → Settings, then navigate to menu Tools → External Tools in the sidebar. (Or search "external tools")
Set up an external tool by clicking on the + sign and filling in the fields accordingly. In Program use the path you got when running which pylint. For the other values, you can use the same from the image.
Run pylint from menu Tools → External Tools → pylint:
Look at your output in the PyCharm terminal
For more details, refer to Pylinting with PyCharm.
If you want to use Pylint to check your whole project or a particular file or directory, you can right click on your project root, file or directory, then activate External Tools → pylint as shown below.
Because I didn't find a working ready-made setup, these are the settings I use in PyCharm CE 2018.1 on macOS:
1 - pip install pylint in your project virtualenv or globally
2 - Add new external tool and configure:
Program: pylint
Arguments: "--msg-template='{abspath}:{line:5d},{column:2d}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\,\s*$COLUMN$:
Notice the required double quotes for the msg-template, and the escape chars for the output filters. The output filter allows to click on the file path and open the location in the IDE source editor.
Only missing feature would be the output filters to plot the lint descriptions directly into the source view, as is done with the builtin linter. No such feature at this time though.
You can try this Pylint PyCharm plugin:
It provides both real-time and on-demand scanning of Python files with Pylint from within PyCharm/IntelliJ IDEA.
Once you have it installed, the real-time inspection works automatically.
For the on-demand you have several options that go from just checking the current open file to scan the entire project:
(Just for the sake of transparency, I am the developer.)
I now use and recommend the PyCharm plugin which didn't exist when I first wrote this answer.**
A colleague pointed me towards pylint-pycharm on GitHub. It's a wrapper around Pylint with output formatted for PyCharm.
Here's how I set it up:
git clone https://github.com/perses76/pylint-pycharm.git
cd pylint-pycharm
python setup.py build
This creates build/scripts-2.7/pylint-pycharm
Then, in PyCharm, create a new External Tool with these settings:
Program: path to your installation of pylint-pycharm
Arguments: --virtualenv=$PyInterpreterDirectory$/.. $FileName$
Working directory: $FileDir$
Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
Now run it from menu Tools* → External Tools → PyLintPyCharm. Each line of output will be hyperlinked to the relevant position in the source code.
A note on the previous answers.
I was searching for a method to make PyCharm aware of the output syntax so I can directly jump to the file locations. That works without using additional tools.
Pylint can be configured to output messages in a specific format using the msg-template option in the pylintrc file or the CLI option --msg-template.
I set it to:
msg-template='{abspath}:{line}:{column}: {msg_id} {msg}'
In the External Tools settings, the Output filters: can be set to
$FILE_PATH$:$LINE$:$COLUMN$: .*
so PyCharm shows links to directly jump to the reported locations.
This can be combined with output-format=colorized so I get this:
PyCharm does not recognize the column despite having it configured. But having the file and line is enough for me.
At first install Pylint with pip:
pip install pylint
You have to open “Settings → Tools → External Tools“ and press the “+” button at PyCharm.
Here are an example with good settings.
Roberto Leinardi has created a Pylint plugin for PyCharm which really works and integrates well into the IDE:
Easy to install from the repositories, full instructions under:
pylint-pycharm
I have a short, yet a happy, experience with it so far! Kudos to Roberto.
Thanks to information here, and updated documentation from PyCharm, I've been able to get this to work nicely to also use the virtual environment for the project (ensuring that any packages can be deployed within the virtual environment and do not need to be deployed globally).
Taking what lkraider provided earlier, but with slight modifications:
Ensure you install Pylint within the virtual environment. Note, make sure that when you created the virtual environment you did not select "Inherit global site-packages". If you do then Pylint will end up being globally and this will not work.
Add a new external tool and configure. This is slightly different compared to what lkraider provided. For one I wanted it to look more like normal Pylint output, hence my msg-template (and Output filter) is a bit different. Each to their own.
The second change is the more critical one for executing Pylint based on the virtual environment. That is the program parameter where I use $PyInterpreterDirectory$.
Program: $PyInterpreterDirectory$/pylint
Arguments: "--msg-template='{abspath}:{line:5d}:{column}: {msg_id}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\:\s*$COLUMN$:
External tool for Pylint

Migrating from PyCharm to VS Code: setting paths

I want to switch over to VS Code. I did the following to enable python with the packages I typically use, including a local utilities package on this machine:
Searched for the setting python.pythonPath and made it PycharmProjects/Project/venv/Scripts/python.exe for both this workspace and for my user
Changed my Windows user PYTHONPATH to PycharmProjects/Project/venv/Scripts/python.exe
Tried PycharmProjects/python.exe;local-utilities-directory
However, the folder I'm trying to manually set won't show up as a possible option when I click on my interpreter in the bottom right. Instead I'm defaulting to an older interpreter that doesn't have all the packages I've amassed.
While I can access some packages, while trying to pull in my personal utilities library, pip fails (the term pip is not recognized...)
I see that there are a few other ways to change the python path, however, to my understanding, whether you do it in a .json or via the IDE UI, shouldn't actually matter.
Any help would be appreciated.
So, I think I messed things up by offering too many paths.
I had to double check that the exact same path showed up first in:
Work setting pythonPath
User setting pythonPath
Windows profile path
Windows profile PYTHONPATH
I deleted the other "backup" paths, and it ended up working.

Adding Python 2.7 & 3.x Interpreters to PyCharm

I'm having issues adding a project interpreter to PyCharm from a new Anaconda environment. I have Anaconda2 installed with one Python 2.7 environment (C:\Anaconda2\python.exe) that I've been using on Pycharm without issue for several months.
I am attempting to add a second Python 3.6 interpreter (from C:\Anaconda2\envs\py36\python.exe) to my PyCharm. After adding the Local Interpreter to Pycharm, I run into a MS Visual C++ Runtime Error R6034 "An application has made an attempt to load the C runtime library incorrectly".
From cursory googling, it seems that there could be a runtime DLL conflict (potentially msvcr90.dll) between Python 2 & 3. All fixes I see involve editing the executable path of the application, but I don't think this is feasible for my Pycharm use case. How do I get rid of this error, or just generally be able to use both Python 2 & 3 interpreters through my PyCharm?
I think that's the problem with Anaconda and different msvc dll in the computer.
You can test the conda command in the command line, to see if R6034 happens. If it happens, try the following solution:
I had a similar problem with Anaconda3 and Python27. I solved this problem via executing the following command in cmd, outside of any conda environment:
conda install msvc_runtime
After installing the packages, open a new command and test if the R6034 error still appears.
I had a similar issue and was able to resolve it by selecting:
File --> Invalid Caches / Restart...
from PyCharm's main menu.
You may also want to double check that any Conda Environments that you have defined as Python Interpreters in PyCharm are properly configured per the docs
This issue was absolutely maddening. Million R6034 error windows would just keep popping up one after another if I just wanted to get help on a function. I researched it for months, on/off, opened tickets with JetBrains to no avail.
If you need to have multiple versions of Anaconda, and if you have Anaconda paths in your PATH, before launching PyCharm, delete all Anaconda paths from PATH, and then start PyCharm. You need to create a separate wrapper launcher script for PyCharm to fix PATH before PyCharm is started. Note that alternative of starting PyCharm and then fixing interpreter and python console PATHS inside PyCharm do not really work. Because PyCharm may be using a system path to access python to read documentation etc. So the only clean fix is to fix the system PATH before PyCharm starts.
Once you understand what needs to be done, then you can use your own steps/tools. This worked for me:
Create a script that modifies PATH. I used Python for that, sed or any other tools are fine too. The script simply examines each path element and removes it if it refers to Anaconda, and then puts it back together:
path_cleanup.py:
path_old = os.environ['PATH']
path_python_removed = [loc for loc in path_old.split(pathsep) if not ('python' in loc or 'Ana' in loc)]
print(pathsep.join(path_python_removed))
Create Powershell script to fix PATH and start PyCharm from that clean environment. To find PyCharm path, the simplest is to start it up the usual way, and head to Task Manager, right mouse click on pycharm64.exe process and select "open file location" to get the full path.
pycharm_clean.ps1
$Env:Path=python path_cleanup.py # call the script to fix the PATH
start-process $PYCHARM_PATH\pcharm64.exe -WindowStyle Hidden # enter your full path to pycharm and put it into background.
You can create a shortcut to launch pycharm_clean.ps1 + you can add it to your windows start up folder to be launched upon login: %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\
If you use BASH inside Cygwin, then steps for path clean up require a bit more tuning, but nothing you cannot do. If you need help, put a comment and I can add that script as well.

Configuring python interpretor with pycharm

Whenever I am configuring python interpreter, on compiling a pop-up is popping up to edit, modify, or uninstall python ,below is the screenshot
The Auto Config feature of the IDE looks for it in the PATH set by the user.
Better way to do it is add it in your system variables from where the IDE could pick it up.
For Mac its under the Library\Frameworks\Python.framework\Versions\X.Y\bin\
For Windows its mostly under C:\PythonXY\python.exe
& For more 'what ifs' you can find it documented here.

Categories

Resources