getting equal behavior for scripts and modules ?

Stef Mientki stef.mientki at gmail.com
Sun Oct 11 22:40:31 CEST 2009


Stephen Hansen wrote:
>
>
> On Sun, Oct 11, 2009 at 10:50 AM, Stef Mientki <stef.mientki at gmail.com 
> <mailto:stef.mientki at gmail.com>> wrote:
> [...] 
>
>     In languages like Delphi, you get an error message, trying to use
>     circular references,
>     but solving them in large programs with a lot of history can be
>     very painful.
>
> [...]
>  
>
>     === solution 1 ===
>
>     Inserting a launcher into the IDE,
>     so instead of running the application as a script,
>     the file will always be executed as a module.
>     """
>
> [...]
>
> Eh. If you must do it this way, just launch the program as 'python -c 
> "import modulename; modulename.main()" <arguments>'. Then the __main__ 
> becomes that little string. The only "cost" then becomes "make sure 
> there's a main function". That's pretty minimal isn't it? Sure you'll 
> make changes to lots of different modules, but only once, and those 
> changes are probably almost boilerplate and simple.
thanks for the tip, your suggestion works equally well,
but I guess it will be somewhat more complicated to implement that as a 
general solution in an IDE.
>
>     === solution 2  ===
>     Prevent execution of the code in this file if the file is ran as a
>     script.
>
>
> AHH RUN!
>
> RUN FOR YOUR LIFE :)
>
> Really.. at a certain point all these hacks to get Python to work in 
> some weird-other-unpythony-way WILL come back and make your "large 
> program with lots of history" descend into a pit of chaos and either 
> rise up again and conquer the world in its evil ways, or, just decay 
> and die under the weight of its own hacks :)
ok, I admit it's a little bit tricky, ...
... on the other hand,
I don't see any invalid python statement in the code
>
> [ I'm still slightly reeling, I admit, for the module you posted the 
> other day which recursively added package directories to PYTHONPATH :) ]
Well that was / is a very rare case:
Design specifications:
- an general purpose builder / distributor / ...
- minimalistic input from the user
- all settings also available from a GUI

So the most minimalistic input I can think of is :

  Exe_Files.append ( New_Program.py )

There is one simpeler line I can think of, namely  :

  New_Program

But as real life is somewhat more complicated, the last line isn't 
discriminative enough.

In the final situation, given the above input, a distro should be build 
(through py2exe + lots of exceptions due to standard libs ;-) ), an inno 
setup installer should be created, the zip file should be uploaded and 
an announcement should be made. So without having to build a parser 
myself, the above code should be pure Python code.

So if there's a better way to implement that one line of code (or in 
real life it might grow to 10 lines with user content) in a script 
performing these tasks, please let me know.

thanks,
Stef




More information about the Python-list mailing list