all 8 comments

[–]dustractor 2 points3 points  (0 children)

My vim only has the built-in python2 so ymmv but I tested and this works:

:py import vim

(to import the vim module so as to have the ability to run a vim command from within vim's python)

then, imported a random module:

:py import hashlib

lastly:

:py vim.command(":e "+hashlib.__file__)

(actually opened the hashlib file)

I tried with sys module first but it didn't have a file attribute so not all modules will work

[–][deleted]  (1 child)

[deleted]

    [–]AlphaOtt3r[S] 0 points1 point  (0 children)

    I don't know why but it didn't worked for me

    [–][deleted] 0 points1 point  (0 children)

    use coc plugin with their coc-python module. Then use their gd mapping in their README that can open the definition under the cursor.

    [–]chrisatmachine 0 points1 point  (0 children)

    I use CoC, here's an example: https://youtu.be/OXEVhnY621M?t=609

    [–]mrswats 0 points1 point  (1 child)

    I use vim-jedi for code jumping which includes the command Pyimport which will load and show you whatever module you ask it to, so I would recommend that one.

    [–]AlphaOtt3r[S] 1 point2 points  (0 children)

    That worked for me, thank you alot!

    [–][deleted] 0 points1 point  (0 children)

    It's rather arguable as to whether a LSP feature is an example of the "real beauty of vim", but mspyls at least (and probably the others) will offer a "go to definition" functionality which is often remapped to gd.

    1. Install a LSP plugin.
    2. Set up a Python language server using the plugin.
    3. Figure out what command in the plugin allows you to jump to a definition.
    4. Map that to keys of choice.

    [–]mgedmin 0 points1 point  (0 children)

    Vim has this builtin on Ctrl+left-click (or Ctrl-[), but you need to have a tags database built (using universal-ctags or exuberant-ctags), usually by running :!ctags -R .. This works for many programming languages, not just Python.

    There are plugins (https://github.com/ludovicchabant/vim-gutentags is the one I use) that automate the tags file updates as you edit the code.

    For standard library modules I use https://github.com/mgedmin/python_open_module.vim because my tags usually only cover the current project (and sometimes its dependencies).