Split files when writing vim plugin in python

background

Write a vim plugin in Python

I am making a Vim (Neovim) plugin by referring to the article linked above. When the Python file got bigger and I tried to split it, I stumbled a bit so I'll share the solution.

In this article, as an example, let's create a plugin that displays the title of a web page using the requests module.

Directory structure and file contents

sample-vim-plugin
  plugin/
    sample_vim_plugin.vim
  src/
    sample_vim_plugin.py
    requests_caller.py

plugin/sample_vim_plugin.vim


scriptencoding utf-8

if exists('g:loaded_sample_vim_plugin')
	finish
endif
let g:loaded_sample_vim_plugin = 1

let s:save_cpo = &cpo
set cpo&vim

py3file <sfile>:h:h/src/sample_vim_plugin.py

function! sample_vim_plugin#print_title(url)
	py3 sample_vim_plugin_print_title(vim.eval('a:url'))
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo

src/sample_vim_plugin.py


import vim
import requests_caller

def sample_vim_plugin_print_title(url):
	print(requests_caller.get_title(url))

requests_caller.py


import requests
from bs4 import BeautifulSoup

def get_title(url):
	response = requests.get(url)
	soup = BeautifulSoup(response.text, 'lxml')
	return soup.title.text

I manage plugins with dein, so when I add the above plugin to the toml file and start neovim ModuleNotFoundError: No module named 'requests_caller' It is said that.

I would like to solve the problem that this module cannot be found.

In "Processing'runtimepath'in Python"

In Python, instead of using the list of paths for'runtimepath', vim.VIM_SPECIAL_PATH A special directory called is used. This directory is used in sys.path When, and when vim.path_hooks is used within sys.path_hooks,'runtimepath' {Rtp} / python2 (or python3) and {rtp} / pythonx (both bars) for each path {rtp} in The module (loaded by John) will be loaded.

There is.

Therefore, there are two possible methods.

Solution 1: Rename the directory

It is said that if there are python2, python3, pythonx directories directly under the runtime path, it will be loaded, so rename the src directory to python3. Of course it is necessary to change the part to be read, so change it as follows

plugin/sample_vim_plugin.vim


- py3file <sfile>:h:h/src/sample_vim_plugin.py
+ py3file <sfile>:h:h/python3/sample_vim_plugin.py

Solution 2: Add to sys.path

If you don't want to rename the directory, add it directly to sys.path.

plugin/sample_vim_plugin.vim


+ let s:sample_vim_plugin_root_dir = expand('<sfile>:p:h:h')
  py3file <sfile>:h:h/src/sample_vim_plugin.py

src/sample_vim_plugin.py


  import vim
+ import sys
+ import os
+ plugin_python_dir = os.path.join(vim.eval('s:sample_vim_plugin_root_dir'), 'src')
+ sys.path.append(plugin_python_dir)
  import requests_caller

Summary

--Put Python files in python2, python3, pythonx directories --Add the directory containing the Python files to sys.path

The plug-in will work in two ways.

Well-known, davidhalter / jedi-vim seems to use Solution 1 (pythonx). I just glanced at the source code, but Shougo / denite.nvim and Shougo / deoplete.nvim seems to use solution 2.

Recommended Posts

Split files when writing vim plugin in python
Write a vim plugin in Python
When writing a program in Python
Write a simple Vim Plugin in Python 3
[Tips] Easy-to-read writing when connecting functions in Python
Reading and writing CSV and JSON files in Python
Character encoding when dealing with files in Python 3
A memorandum when writing experimental code ~ Logging in python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
Attention when os.mkdir in Python
Convenient writing method when appending to list continuously in Python
Transpose CSV files in Python Part 1
Precautions when using pit in Python
Behavior when listing in Python heapq
Automatically format Python code in Vim
Manipulate files and folders in Python
Handling of JSON files in Python
Download Google Drive files in Python
Sort large text files in Python
Read files in parallel with Python
Export and output files in Python
Reading and writing text in Python
Rectangle area element split in Python
When using regular expressions in Python
Extract strings from files in Python
Mode line when you open the appropriate Python code in Vim
Settings when writing Google App Engine / Python apps in Intellij Idea
[Python ORM] Notation when writing SQL using subquery in IN clause in SQLAlchemy
Don't forget shebang when writing Check! Ansible's Dynamic Inventory in python!
When specifying multiple keys in python sort
Create ScriptableObject in Python when building ADX2
Precautions when pickling a function in python
Find files like find on linux in Python
Output tree structure of files in Python
Referencing INI files in Python or Ruby
What Emacs users should know when writing python code in Sublime Text
Template for writing batch scripts in python
When looking at memory usage in Python 3
Automate jobs by manipulating files in Python
Reading and writing JSON files with Python
Sample for handling eml files in Python
Download files in any format using Python
When codec can't decode byte appears in python
Create a Vim + Python test environment in 1 minute
Convert FBX files to ASCII <-> BINARY in Python
Summary of how to import files in Python 3
Study from Python Reading and writing Hour9 files
When I try matplotlib in Python, it says'cairo.Context'
[CpawCTF] Q14. [PPC] Try writing Sort! In Python
A memo about writing merge sort in Python
Precautions when dealing with control structures in Python 2.6
Note on encoding when LANG = C in Python
Handle zip files with Japanese filenames in Python 3
The 18th offline real-time writing problem in Python
How to get the files in the [Python] folder
A Vim plugin that automatically formats Python styles
Get files, functions, line numbers running in python
Reading and writing fits files with Python (memo)
[Question] What happens when I use% in python?
Error when trying to install psycopg2 in Python