D
D
dsemen2015-06-30 17:42:41
vim
dsemen, 2015-06-30 17:42:41

How to reimport a module before running :PymodeRun in python-mode for vim?

Let's say there are two simple files open in vim:
file1.py
value = 'test'
and file2.py

from file1 import value
print(value)

Run :PymodeRun, get "test", change file1.py
value = 'new test'
save, jump to buffer with file2.py, run :PymodeRun, still get "test".
If you run through :python% everything works fine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dsemen, 2015-07-26
@dsemen

I got it myself
1. The solution "on the forehead"
in the file file2.py do not use from and temporarily add reload:

import file1 as f

import imp
imp.reload(f)

print(f.value)

It’s crooked, but it works and you don’t need to change anything in python-mode
2 itself. Slightly edit python-mode (made at home)
Find the file python-mode/pymode/run.py and add a couple of lines (framed lines 37 - 40):
+       m_keys = sys.modules.keys()
37      code = compile('\n'.join(lines) + '\n', env.curbuf.name, 'exec')
38      sys.path.insert(0, env.curdir)
39      exec(code, context) # noqa
40      sys.path.pop(0)
+       for k in set(sys.modules) - set(m_keys):
+           del sys.modules[k]

Wrote an issue on github.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question