On Fri, Oct 20, 2017 at 3:58 AM, Simon King <si...@simonking.org.uk> wrote:
> This is a subtle python gotcha based on how the import system works. > It doesn't have anything to do with SQLAlchemy. > > <http://python-notes.curiousefficiency.org/en/ > latest/python_concepts/import_traps.html#executing-the-main-module-twice> > > When you import a module, it gets cached in sys.modules under its > "fully qualified module name", so it doesn't matter how many times you > run "import foo", the module is only loaded once. > > However, the script that you initially *execute* gets the special name > "__main__". If it gets subsequently imported via an "import" > statement, it is not found in the cache, so it is executed again, > meaning you get duplicate definitions of everything in the module. > > Hope that helps, > > Simon > Thank you. This was the explanation I'd been looking for. So - trying to work around this - the following works (after moving exiting __main__ code into main() function): $ python -c "from myoper import main; import sys; main(*sys.argv[1:])" ext_check 1 But it seems like the best solution to this is to probably move any test code that calls out to an external module OUT of myoper (probably into myoper_test itself, which is what I'm going to end up doing). Are there any other common solutions? Thanks again, Taz -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.