Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Mark Bourne Newsgroups: comp.lang.c Subject: Re: Python recompile Date: Fri, 7 Mar 2025 20:34:18 +0000 Organization: A noiseless patient Spider Lines: 22 Message-ID: References: <20250304092827.708@kylheku.com> <20250306115048.606@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 07 Mar 2025 21:34:21 +0100 (CET) Injection-Info: dont-email.me; posting-host="f0ce4a01166aa473ecb851db25d1c853"; logging-data="3915469"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SF/ieczV8eGc+uOvsw/VY" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 SeaMonkey/2.53.20 Cancel-Lock: sha1:0ln6xWENVvT3G/rYPbKSlUS8jA8= In-Reply-To: <20250306115048.606@kylheku.com> Bytes: 2864 Kaz Kylheku wrote: > When imports are being compiled automatically to .pyc files, > their statements are being executed; like if you have a top-level > print('hello'), you will see the hello output, and you will see > a .pyc file in __pycache__ for that file. Was that hello printed > after the .pyc file was produced, or during? I suspect during. No, the top-level code in the module is executed when the import is executed, not when it is compiled. If the imported module hasn't already been compiled to bytecode, that needs to be done before the import can be completed. In your example, "hello" will be printed when the module is imported, without any functions etc. within the module being called, but it's done when the bytecode is executed, not when it's compiled compilation. If you ran the same program again (after the bytecode had been cached in a ..pyc file) without modifying the source (so it doesn't need to be recompiled), you'd still get that output printed, even though the imported module isn't compiled again. -- Mark.