Sublime Text 2. Autocomplete python `from` - python

I am using SublimeRope plugin. When I am typing from foo.b it displays the autocomplete dialog with random crap but what I am really looking for is to recognize bar module inside the foo package. However if I type from foo import b it immediately suggest me to import bar as a module. Which means Rope "knows" about that module. How can I configure my Sublime to help me suggest the imports when from foo.b ?
I am doing projects with django so the real example it wont me to autocomplete from django.contrib. but if I type from django.contrib.auth.models import U it suggest me to import user.

You should definitely be using SublimeJEDI for Python autocompletion! There's no way around Jedi awesomeness.
This is just a Sublime Plugin for the Jedi library (which is definitely better than Rope, but I'm biased because I'm the author).

Just adding to what other have said sublimecodeintel can help you with this. However to get it working with Django as you would like you have to add a configuration file pointing to django to your project. The instructions for how to do this are on the github page linked above. You'll add something similar to this:
{
"Django":{
"django":'/Users/bin/python2.7/site-packages/django'
},
}

Have you checked out SublimeCodeIntel? It's available through Package Control, and has this functionality. The initial indexing of your packages may take some time, but once it's all set (you may need to restart Sublime once or twice to get everything loaded) it works like a charm.

Related

how to make vscode detect / auto reload modules after editing them?

I've seen a few questions asking this, but none of the solutions worked for me.
I am developing a few functions/classes in different modules and have a main.py script that calls everything.
The problem is, when I make a change to a function in another module i.e. module1.py, VSCode does not detect the changes when I call the function in main.py after updating, it's still the older version.
I can get around this by doing something like:
from importlib import reload
reload module1
but this gets old real quick especially when I'm importing specific functions or classes from a module.
Simply re-running the imports at the top of my main.py doesn't actually do anything, I can only do that if I kill the shell and reopen it from the begining, which is not ideal if I am incrementally developing something.
I've read on a few questions that I could include this:
"files.useExperimentalFileWatcher" : true
into my settings.json, but it does not seem to be a known configuration setting in my version, 1.45.1.
This is something Spyder handles by default, and makes it very easy to code incrementally when calling functions and classes from multiple modules in the pkg you are developing.
How can I achieve this in VSCode? To be clear, I don't want to use IPython autoreload magic command.
Much appreciated
FYI here are the other questions I saw, but did not get a working solution out of, amongst others with similar questions/answers :
link1
link2
There is no support for this in VS Code as Python's reload mechanism is not reliable enough to use outside of the REPL, and even then you should be careful. It isn't a perfect solution and can lead to stale code lying about which can easily trip you up (and I know this because I wrote importlib.reload() 😁).

Go to definition in VS code doesn't show the body of a function

When I right click on a function and then select "Go to definition" there shows up a module with that function, but it only shows the parameters which have to be passed to it, and I can't see anything about the body of the function.
Here is what's shown when I went to the definition of itertools.dropwhile:
As mentioned in the comments, VSCode can only show you source code it has access to, and many of the Python builtins and stdlib (including the itertools module) are implemented in compiled C -- there's no source code to show you.
Sometimes this happens if you develop code that runs inside an environment whose libraries are not visible in your main OS.
One way to solve this is by opening the terminal in VSCode and doing a pip install <library> to install the library and make VSCode aware of it.

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.

Configure PyCharm IDE to organize the imports the way Eclipse does (Django development)

I am using PyCharm for Django web application development. The tool is great, except that I need to add every import statement manually (i.e. the 'optimize imports' feature doesn't work).
Is there any way to make it work the same way Eclipse does when developing Java applications?
You don't need to type imports manually, there is a relevant documentation page, see:
Creating Imports
Imagine you've written the following code forgetting to import os:
print os.getcwd()
Then, focus the word os and hit Alt+Enter combination, you'll get the popup from which you can choose what to import, choose os from the list.
Also, there is an "Import Assistant" that fires up tooltips on unresolved imports on the fly.
And, FYI, "Optimize Imports" feature works with existing imports and removes unused entries.
Hope this is what you were asking about.

How to make emacs works like "ipython manage.py shell" with django?

I am new to emacs and Django.
I've found ipython quite convenient, especially its TAB completion.
In a ipython Django shell, TAB completion is quit convenient because it also includes Django setting. But when I open a python file, with "emacs -nw models.py" for example, the TAB completion for Django related module no longer works that well.
Can any body help? Thx!
If auto-completion is what you need, then use rope.
Although default auto-complite hotkey is different (M-/), it works pretty clever and provides list of other useful features.
I think you may want to try this:
Emacs as a Django IDE with python-django.el
http://from-the-cloud.com/en/emacs/2013/01/28_emacs-as-a-django-ide-with-python-djangoel.html
with auto-complete and jedi setup you can do the TAB completion just like Ipython with django models and views.
also it has a wonderfun binding for ipdb to trace a bug called pdbtracking.
You will love it. Thanks

Categories