VS code python attribute suggestion - python

I have a problem with Intellisense. Even though Python, Pylance and even Visual Studio IntelliCode extensions are installed, VS code is still unable to suggest object attributes.
Could you please advise any settings.json parameters which will trigger python attribute suggestion globally ?
Sometime it's tedious to remember all object attributes without documentation being opened.

You can use ctrl + space for windows for activating intelliSense in VsCode.
However it also depends on processing speed of computer.

Intellisense was provided by the Language Server. The search paths depend on the python interpreter you have selected.
If you modify the PYTHONPATH manually to import the package, you need to add these locations to the Language Server too(settings.json), like this:
"python.autoComplete.extraPaths": [
"C:/Program Files (x86)/Google/google_appengine",
"C:/Program Files (x86)/Google/google_appengine/lib/flask-0.12"]
You can refer to here.
Or, there are some problems of your Language Server, you can have a look at the
OUTPUT panel --> Python Language Server channel. And attach the error message on the question.

Open the Command Palette (Command+Shift+P on macOS and Ctrl+Shift+P on Windows/Linux) and type:
Python: Select Interpreter
then choose Interpreter.

Related

How to get Intellisense on Visual Studio (VS) Code to work on Python

I have Visual Studio Code on my laptop and setting it up for the first time and am having trouble making Intellisense work. I downloaded the python extension. When I tried to type a syntax, I don't see a drop down of the available methods.
What I did so far was press F1> type 'open settings'> and see that the option is turned on. It looks like the setting is on, screenshot below line 18. I'm not sure if there is something I'm missing. Would anyone happen to have an idea of how I can have the auto complete functionality to work?
Intellisense was provided by the Language Server, The default value of it is Default.
Default: Automatically select a language server: Pylance if installed
and available, otherwise fallback to Jedi.
So, it's a little weird of your problem, could you try to install the Pylance and set it as the Language Server? Add this in the settings.json file:
"python.languageServer": "Pylance",
Have you tried Ctrl+Space or hovering some code?
Also do you have "Visual Studio IntelliCode" extension installed?
If neither Ctrl+Space nor hovering work, it is recommended to restart VS Code (https://code.visualstudio.com/docs/editor/intellisense#_troubleshooting)

How to disable instant linting from [Python (parser)] in Visual Studio Code

The Python parser in Visual Studio Code 1.25.1 lints any error directly while typing.
How can I turn that off?
I would like to disable the live-parser-linting.
I do not want to completely disable linting from pep8 or pylint too.
Maybe the issue got adressed here too:
https://github.com/Microsoft/vscode-python/issues/2270
I found a setting for "List of surpressed diagnostic message" but I do not know, what to insert here (or if this is even the right place) nor do I find any documentation for the python.analysis.disabled settings on the web.
Help appreciated.
"python.analysis.disabled": [
"",
],
Is there any workaround?
[I am assuming you have the latest version of the Python extension for VS code installed as this won't have anything directly to do with VS Code 1.25.1]
If you are using the new language server from the Python extension then errors as you type do not turn off at the moment (you referenced the specific issue suggesting having a setting to turn that off).
If you're not using the new language server than the extension only runs linters on save, which would mean you have automatic saving as you type turned on and that's triggering the constant linting of your code. Tweak your settings to not automatically save and that will stop constant linting (and you can specify settings in VS Code for specific languages if you only want to change this for Python).

How to enable Intellisense of my own .Net library for a Python application in Visual Studio 2017?

I am using pythonnet ( https://github.com/pythonnet/pythonnet ) and have referenced a .Net library within the same solution space but it does not seem to pull in any information to use for Intellisense. In fact, VS gives an error:
"Unable to resolve "ReferenceLibarary". Intellisense may be missing for this module.
So how do I add intellisense to my "module"?
Even though there is an error, the script can still be run (in or out of VS). I have thorough XML already set up as there is also a sandcastle project for a chm output. Can the sandcastle project output the appropriate content that is missing?
Peter Stevens answer seems fine to me. His solution uses IronPython to generate stubs. These stubs are used in turn by the CPython auto-completion mechanism, in for example, Visual Studio Code.
Let's say you have a c# dll located in c:\dev\HTL\bin\release\HTL.dll you want to access from Python and need auto-completion to help code. With IronPython installed and the ironpython-stubs module downloaded, you can do the following:
ipy -m ironstubs make HTL --path="c:\\dev\\HTL\\bin\\release\\HTL.dll" -output stubs.min
Then, as in Peter's response above, in VS Code use File | Preferences | Settings to add the full stubs.min path to "python.autoComplete.extraPaths". Having imported the target dll into your *.py file in the VS Code editor you should now be able to dot to auto-completion for the classes contained in that dll.
Use the ironpython-stubs module.
Make sure your dll is in sys.path, then run from ironpython
ipy -m ironstubs make --output stubs.min
then set "python.autoComplete.extraPaths" to \ironpython-stubs\release\stubs.min
Works like a charm

Python auto import extension for VSCode

Is there a Python auto import extension/plugin available for Visual Studio Code?
By auto import I mean, auto import of python modules. Eclipse and Intellij has this feature with Java.
VSCode team recently released Pylance
Features
Docstrings
Signature help, with type information
Parameter suggestions
Code completion
Auto-imports (as well as add and remove import code actions)
As-you-type reporting of code errors and warnings (diagnostics)
Code outline
Code navigation
Code lens (references/implementations)
Type checking mode
Native multi-root workspace support
IntelliCode compatibility
Jupyter Notebooks compatibility
No, but it will soon be a part of vscode-python: https://github.com/Microsoft/vscode-python/pull/636
EDIT: See answer by #Eric, who built such an extension.
EDIT 2: See answer by #Eyal Levin, mentioning such an extension (Pylance).
I have built an automatic import extension that supports Python. It lets you fully customize how the imports get written to the file, modifying import paths, names, sorting relative to other imports. The Python plugin even lets you "group" imports together with extra line breaks.
From https://github.com/microsoft/python-language-server/issues/19#issuecomment-587303061:
For those who wonder how to trigger auto-importing as I did, here are
the steps.
Enable Microsoft Python Language Server by removing the check of
Python: Jedi Enabled in your settings.
Reload the VSCode window.
Hover your mouse over the variable that you want to import, and click Quick
fix...
For the last step, if it shows No quick fixes available or
Checking for quick fixes, you may need to wait for a while until the
extension has finished code analysis. It is also possible to set a
shortcut that triggers a quick fix.
I use this package it works very well
https://marketplace.visualstudio.com/items?itemName=codeavecjonathan.importmagic
You can find it in VSCode extension store. it's name is IMPORTMAGIC. It works fantastic. It will include all modules which you use in your script.
It has code action ctrl + . , which will also import library.
This is supported in the official Microsoft python extension, but for some reason I found it was recently disabled or no longer default. The setting I had to toggle was
"python.analysis.autoImportCompletions": true,
(Updated answer as of January 2023)
These three together did it for me:
"python.analysis.autoImportCompletions": true,
"python.analysis.autoImportUserSymbols": true,
"python.analysis.indexing": true,
If that is slowing down your computer too much because it's indexing too many files, then look into specifying patterns and depths of directories to include in the indexing using "python.analysis.packageIndexDepths".
Note that I am using Pylance (currently the default, as of January 2023).
Check out the VSCode python settings reference for more info on each of those settings.
You might find This Python-Based Module useful if you wish to auto import and auto download missing modules from a script or sub-scripts. Not only for VSCode, but also for any IDE or Editor.

How to debug C extensions for Python on Windows

I have a problem with a segfault in pyodbc and would like to debug it in Windows XP x86. However, the information online seems primarily Linux-centric. What is the best way to go about this?
So I was able to successfully resolve my issue by using Visual Studio 2008. I loosely followed the steps listed here -
http://www.velocityreviews.com/forums/t329214-debugging-python-extensions.html
And some tips on workarounds here -
Compiling python modules whith DEBUG defined on MSVC
Here is my version of the steps for anyone else who may encounter this problem.
In case you haven't already, be sure to setup the Python header and libs directories in VS
a. Go to Tools > Options > Projects and Solutions > VC++ Directories. Be sure to add your include and libs path to the Include and Library files' path, respectively. (e.g. C:\Python27\include, C:\Python27\libs)
Go to your Python include folder (once again, e.g. C:\Python27\include) and edit pyconfig.h. Comment out the line # define Py_DEBUG and save. Go to your libs folder (e.g. C:\Python27\libs) and make a copy of python27.lib. Name the copy python27_d.lib.
Create a new project. Choose Win32 Project and name it the module name (in my case pyodbc. Click Next then choose DLL for Application type and check Empty Project.
In the Solution Explorer, right-click on Header Files and choose Add > Existing Item. Select all of the header files that you need. Do the same for Source Files.
Go to Project > Properties, then under Configuration Properties -
a. General - ensure that you are using the correct Character Set. For me it was Use Multi-Byte Character Set. Python 3 probably needs Use Unicode Character Set.
b. Debugging - enter the path to Python in the Command field. (e.g. C:\Python27\python.exe). Then set Attach to Yes.
c. Linker > General - change the Output File to end in .pyd instead of .dll.
Ensure that your configuration is set to Debug. Go to Build > Build Solution.
Open a cmd and cd into the directory where your pyd file was compiled. Start python from the cmd window. To attach debugger on this running python process, go back to Visual Studio and click the green play button to start debugging. You can also use Debugging -> Attach to Process... Now go back to Python and import your module. Play, test, and try to break it!
Debugging workflow with WinDbg
This workflow will create debugging information for a Release build, so you
don't have to mess with the original include and library files of Python.
Download and install Debugging Tools for Windows
Get the symbol files for your Python version and extract them. For Python
2.7.3 this would be http://www.python.org/ftp/python/2.7.3/python-2.7.3-pdb.zip.
Modify setup.py to generate debugging files. You have to add '/Zi' to
extra_compile_args and '/DEBUG' to extra_link_args. Example:
ext_modules = [Extension('pyuv', sources=['src/pyuv.c'],
extra_compile_args=['/Zi'],
extra_link_args=['/DEBUG'])
]
Build the extension as always (python setup.py ...).
Start WinDbg and specify the Symbol Search Path (Ctrl + S).
C:\Path\To\Extension_pdb
C:\Path\To\Extracted\python-2.7.3-pdb
srv*;SRV*c:\tmp*http://msdl.microsoft.com/download/symbols
The last line will download and cache required symbols for Windows modules.
Start the Python executable (Ctrl + E). You can directly execute a
script or run in interactive mode.
Skip the initial breakpoint with "Go" (F5).
If there is a Segmentation fault the execution will break and you will
see something like Access violation - code c0000005 (first chance)
in the WinDbg console.
You can get detailed exception information by typing !analyze -v
in the WinDbg console and the current stack trace with kb.
Here is an
example of such an output.
You should be able to combine this approach with pyrospade's
answer to debug with
Visual Studio if you omit his second step and build the project with
Release configuration.
A further tutorial for WinDbg could be found here.
Segfaults are especially mysterious, as there is no way to trap for them from your Python code, or even to get much stacktrace information on the C side of things. One thing that can give you at least a little more info is to use the Google breakpad C library to report a C stack trace when the segfault occurs.
You may want to try David Malcolm's tool CPyChecker which statically analyses C extensions for memory leaks and other bugs. The tool is documented here.

Categories