-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code is run not in __main__ module #84
Comments
I'm not sure what can be done, I've tried setting the main module in the compatibility module setup: diff --git a/runtime/autoload/provider/script_host.py b/runtime/autoload/provider/script_host.py
index 1420831..dddded8 100644
--- a/runtime/autoload/provider/script_host.py
+++ b/runtime/autoload/provider/script_host.py
@@ -37,6 +37,7 @@ class ScriptHost(object):
neovim.DecodeHook(
encoding=nvim.options['encoding'].decode('ascii')))
sys.modules['vim'] = self.legacy_vim
+ sys.modules['__main__'] = self.legacy_vim
def setup(self, nvim):
"""Setup import hooks and global streams. but it doesn't work as expected. Do you have any ideas on how to fix this? |
@tarruda ?!
I would highly suggest to use the second variant and do two things:
You already do the second thing. But you are not doing the first which is why my script fails: diff --git a/runtime/autoload/provider/script_host.py b/runtime/autoload/provider/script_host.py
index 1420831..0abb0ae 100644
--- a/runtime/autoload/provider/script_host.py
+++ b/runtime/autoload/provider/script_host.py
@@ -28,6 +28,7 @@ class ScriptHost(object):
self.setup(nvim)
# context where all code will run
self.module = imp.new_module('__main__')
+ sys.modules['__main__'] = self.module
nvim.script_context = self.module
# it seems some plugins assume 'sys' is already imported, so do it now
exec('import sys', self.module.__dict__) |
By the way, diff --git a/runtime/autoload/provider/script_host.py b/runtime/autoload/provider/script_host.py
index 1420831..df3906e 100644
--- a/runtime/autoload/provider/script_host.py
+++ b/runtime/autoload/provider/script_host.py
@@ -4,6 +4,8 @@ import logging
import os
import sys
+from types import ModuleType
+
import neovim
__all__ = ('ScriptHost',)
@@ -27,7 +29,8 @@ class ScriptHost(object):
"""Initialize the legacy python-vim environment."""
self.setup(nvim)
# context where all code will run
- self.module = imp.new_module('__main__')
+ self.module = ModuleType('__main__')
+ sys.modules['__main__'] = self.module
nvim.script_context = self.module
# it seems some plugins assume 'sys' is already imported, so do it now
exec('import sys', self.module.__dict__) This works at least starting from Python-2.6. |
And the whole |
Consider the following script:
. In Vim this code prints
True
, in NeoVim it printsFalse
. Usingimport __main__
can be (and is actually used in powerline) to put some variable into global environment without requiring to specifyglobals()
dictionary, so that code that user needs to run to use powerline is simplywhich is simple and easy.
The text was updated successfully, but these errors were encountered: