« first day (2721 days earlier)      last day (2225 days later) » 

11:12 PM
Anyone know how I can use importlib to import a module as __main__? When I try to override the name on a spec object, I get a weird "loader doesn't support __main__" error
spec = importlib.util.find_spec('MODULE_TO_IMPORT')
spec.name = '__main__'
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
Traceback (most recent call last):
  File "untitled.py", line 8, in <module>
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 750, in get_code
  File "<frozen importlib._bootstrap_external>", line 398, in _check_name_wrapper
ImportError: loader for MODULE_TO_IMPORT cannot handle __main__
 
python can't handle your tomato
 
8
Q: How to import a module as __main__?

xorsystI have a module that has the usual if __name__ == '__main__': do stuff... idiom. I'd like to import that from another module, and fool it into running that code. Is there any way of doing this? I should mention, for reasons I won't go into here, I'm not in a position to change the code ...

 
@VictorA. The answers there don't use importlib though
 
code smell?
 
11:17 PM
I don't smell, I'm just curious :P
 
@Aran-Fey I think they added that check in 3.4 or so to make it a lot easier to debug some problems with the new import machinery; if you search b.p.e you can probably find it. But it might be easier to just step into the import machinery and track down where it's checking.
 
If they're still using the _call_with_frames_removed hack, you'll want to disable that before debugging.
 
@VictorA. Sorry, can't find anything that would help me there
 
11:24 PM
If I pass a different name to __import__ or importlib.import_module, it'll import a different module. That doesn't help me. I want to import foo.py but pretend that its name is __main__ so that it'll execute its if __name__ == '__main__': block
 
Nope, looks like the import __main__ check was reverted. The error you're seeing is a wrapper that checks to see that the name of the module is one of the names the loader was built for. Which of course it isn't. Since you know that it's safe in this case (or maybe it isn't, but you know that you're interested in seeing what happens if it's not safe, at least…), you want to subvert that check.
The easiest hack is to subclass the loader and override the handful of @_check_name decorated methods. They're all one-liners, so that's pretty trivial. (But at least one of them just calls super, so make sure to skip over the immediate parent.)
 
@Aran-Fey is trevor belmont in your avatar?
 
Or, if you want to get nasty, monkeypatch spec.loader before you call its exec_module method.
 
@VictorA. nope, it's a guy from a card game
@abarnert I don't think I'll go that far. I'll probably just go to bed instead.
I have to focus my attention on making Martijn undo his downvote on my pprint answer; don't have energy to spare to wrestle with importlib
 
bed is good
 
11:35 PM
Excellent even :)
 
@AndrasDeak bed is good jajaja
 
especially on DST week
 
@Aran-Fey how can play that game? look good
 
@VictorA. It's on steam and also on mobile
 
@Aran-Fey good!
 

« first day (2721 days earlier)      last day (2225 days later) »