Timeline for answer to How do I override a Python import? by Anurag Uniyal
Current License: CC BY-SA 3.0
Post Revisions
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 3, 2025 at 5:33 | comment | added | Karl Knechtel |
@AnuragUniyal The problem is that your code will not ordinarily be used, because module imports are cached by default. Running import Mod_1 the second time simply retrieves a value from sys.modules; hooking into the import machinery doesn't matter. (The API has changed since 2010, but the caveat still applies.)
|
|
| Nov 2, 2014 at 2:01 | history | edited | Peter Mortensen | CC BY-SA 3.0 |
Copy edited.
|
| Jun 20, 2010 at 7:46 | comment | added | Anurag Uniyal | @Evan Plaice, I thought replacing a module import by a class object was example enough. | |
| Jun 20, 2010 at 7:17 | comment | added | Evan Plaice | @Anurag it could, but you didn't create an example illustrating how. | |
| Jun 20, 2010 at 6:51 | comment | added | Evan Plaice | @Anurag If you run a self consuming example like the one in the question, the .pyc output should be stripped of all unnecessary meta-data having to do with the preprocessor including code that activates the preprocessor itself. There's no point in mentioning all it's features here. I'll just say, it's not your standard preprocessor. If you want to know more about it check out the project. | |
| Jun 20, 2010 at 6:45 | comment | added | Anurag Uniyal | @Evan Plaice, but why can't import hook be used to change loading of old module and instead you load new modules? | |
| Jun 20, 2010 at 6:44 | comment | added | Evan Plaice | @Anurag To answer "why does python need a preprocessor?". Lets say you have python 2 and python 3 code in the same file. So, you sprinkle 'if py2x' statements all over the code. Then python 4 comes out and you decide to drop support for 2, now you have to find all the if statements for py2x in the code. In my preprocessor, it's as easy as telling it to remove all code blocks under '#ifdef py2x'. It's not geared toward functionality it's for maintainability. I'm trying to create a better alternative to support 2x and 3x code to give library writers more incentive to support 3x. | |
| Jun 20, 2010 at 6:39 | comment | added | Evan Plaice | @Anurag almost... that mimics the current default import functionality. What I needed, was something that could get rid of the old import and load a new one under the old one's name. | |
| Jun 19, 2010 at 7:14 | history | answered | Anurag Uniyal | CC BY-SA 2.5 |