0

I have a python script for generating templates that's being invoked by this line in my vimrc file:

autocmd BufNewFile ~/Documents/wiki/diary/*.mkd :silent 0r !vimwiki-diary-template.py

That works. Vim uses that script in creating a template for each new vimwiki diary file.

But I'm trying to make it more robust and it's not working:

Adding

from tasklib import TaskWarrior

causes

ImportError: No module named tasklib

But this from tasklib import TaskWarrior works in both my REPL as well as in my taskwiki plugin, just not in my custom template script.

I can eliminate that import error by setting

sys.path = ['', '/home/alec/.local/bin', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/alec/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']

before trying to import tasklib but then it just produces a different error:

Traceback (most recent call last):
  File "/home/alec/bin/vimwiki-diary-template.py", line 6, in <module>
    from tasklib import TaskWarrior
  File "/usr/local/lib/python3.7/dist-packages/tasklib/__init__.py", line 1, in <module>
    from .backends import TaskWarrior
  File "/usr/local/lib/python3.7/dist-packages/tasklib/backends.py", line 2, in <module>
    import copy
  File "/usr/lib/python3.7/copy.py", line 52, in <module>
    import weakref
  File "/usr/lib/python3.7/weakref.py", line 50
    .format(type(meth))) from None
                            ^
SyntaxError: invalid syntax

...which doesn't make any sense to me. A syntax error in /usr/lib/python3.7/weakref.py?

Vim is compiled with python3 support:

+comments          +libcall           -python            +visualextra
+conceal           +linebreak         +python3           +viminfo
Linking: gcc   -Wl,-E  -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim   -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl  -L/usr/lib -llua5.2 -Wl,-E  -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/x86_64-linux-gnu/perl/5.28/CORE -lperl -ldl -lm -lpthread -lcrypt  -L/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu -lpython3.7m -lcrypt -lpthread -ldl -lutil -lm -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -ldl -lz -lpthread -lm     

What's going on here?

How do I use this tasklib library in my vimwiki-diary-template.py template?

5
  • 1
    Doing !file will just execute the file; nothing relevant to vim's support of python3 there. Does running the file at the command line like ./vimwiki-diary-template.py work the way you want? – D. Ben Knoble May 28 '20 at 22:00
  • 1
    What's the shebang line in your vimwiki-diary-template.py script? What does :!python3 --version says (or the binary in the shebang line)? I imagine it might not be running the python 3.7 interpreter that you think it is... – filbranden May 28 '20 at 23:05
  • 1
    omg, thank you! yes, the script was simply using #!/usr/bin/python which was version 2.7. Thanks for point out the shebang! – alec May 29 '20 at 0:37
  • 1
    @filbranden you and I had the same thought; you just articulated better :) – D. Ben Knoble May 29 '20 at 13:32
  • nice, I didn't even realize (or had only vaguely heard) that it was possible to do :! some-script.py from inside vim... which I presume is what !file was referring to. – alec May 29 '20 at 17:11
1

Specifying the python version in the script like #!/usr/bin/python3 (instead of just #!/usr/bin/python) solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.