python - Dynamically creating module in sys.modules causes sys to become None -


i'm creating module called qt (github link) alias module (for example pyqt4), when import qt in fact importing pyqt4:

from qt import qtwidgets  # imports pyqt4.qtgui 

example qt module:

# qt.py import sys import pyqt4.qt  sys.modules["qt"] = pyqt4 pyqt4.qtwidgets = pyqt4.qtgui 

however, if add print sys @ end of qt.py, sys equals none:

print sys >>> none 

why sys "break" , can avoid breaking it?

this in python 2.7.11 on os x.


@lol4t0 – here's example i'm using standard modules:

# xyz.py  import os import sys  sys.modules['xyz'] = os print sys  # print "none" 

so importing result in "none printed:

$ python python 2.7.11 (default, jan 22 2016, 08:29:18) [gcc 4.2.1 compatible apple llvm 7.0.2 (clang-700.1.81)] on darwin type "help", "copyright", "credits" or "license" more information. >>> import xyz none 

when there no longer reference module, drop globals replacing them none.

you need remember old verion of sys.modules['xyz'] (i.e. current module) until done executing it.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -