Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64122 > unrolled thread

Compiling main script into .pyc

Started bySam <lightaiyee@gmail.com>
First post2014-01-16 17:01 -0800
Last post2014-01-17 17:04 +0000
Articles 9 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  Compiling main script into .pyc Sam <lightaiyee@gmail.com> - 2014-01-16 17:01 -0800
    Re: Compiling main script into .pyc bob gailer <bgailer@gmail.com> - 2014-01-16 21:56 -0500
    Re: Compiling main script into .pyc Ned Batchelder <ned@nedbatchelder.com> - 2014-01-16 22:07 -0500
    Re: Compiling main script into .pyc MRAB <python@mrabarnett.plus.com> - 2014-01-17 03:19 +0000
    Re: Compiling main script into .pyc Dave Angel <davea@davea.name> - 2014-01-16 23:43 -0500
      Re: Compiling main script into .pyc Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-17 08:40 +0000
        Re: Compiling main script into .pyc Grant Edwards <invalid@invalid.invalid> - 2014-01-17 20:12 +0000
    Re: Compiling main script into .pyc Terry Reedy <tjreedy@udel.edu> - 2014-01-16 23:45 -0500
    Re: Compiling main script into .pyc Alister <alister.ware@ntlworld.com> - 2014-01-17 17:04 +0000

#64122 — Compiling main script into .pyc

FromSam <lightaiyee@gmail.com>
Date2014-01-16 17:01 -0800
SubjectCompiling main script into .pyc
Message-ID<61a24c80-12fd-4c7a-89ed-41e997b66545@googlegroups.com>
One thing I observe about python byte-code compiling is that the main script does not gets compiled into .pyc. Only imported modules are compiled into .pyc. 

May I know how can I compile the main script into .pyc? It is to inconvenience potential copy-cats.

[toc] | [next] | [standalone]


#64134

Frombob gailer <bgailer@gmail.com>
Date2014-01-16 21:56 -0500
Message-ID<mailman.5620.1389927400.18130.python-list@python.org>
In reply to#64122
On 1/16/2014 8:01 PM, Sam wrote:
> One thing I observe about python byte-code compiling is that the main script does not gets compiled into .pyc. Only imported modules are compiled into .pyc.
>
> May I know how can I compile the main script into .pyc?
Duh? Just import it!

[toc] | [prev] | [next] | [standalone]


#64135

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-01-16 22:07 -0500
Message-ID<mailman.5621.1389928080.18130.python-list@python.org>
In reply to#64122
On 1/16/14 8:01 PM, Sam wrote:
> One thing I observe about python byte-code compiling is that the main script does not gets compiled into .pyc. Only imported modules are compiled into .pyc.
>
> May I know how can I compile the main script into .pyc? It is to inconvenience potential copy-cats.
>

The standard library has the compileall module that can be used to 
create .pyc files from .py files, but as we've been discussing in 
another thread, you may not want .pyc files.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#64136

FromMRAB <python@mrabarnett.plus.com>
Date2014-01-17 03:19 +0000
Message-ID<mailman.5622.1389928778.18130.python-list@python.org>
In reply to#64122
On 2014-01-17 02:56, bob gailer wrote:
> On 1/16/2014 8:01 PM, Sam wrote:
>> One thing I observe about python byte-code compiling is that the main script does not gets compiled into .pyc. Only imported modules are compiled into .pyc.
>>
>> May I know how can I compile the main script into .pyc?
> Duh? Just import it!
>
What if you want to just compile it? Importing will run it!

[toc] | [prev] | [next] | [standalone]


#64137

FromDave Angel <davea@davea.name>
Date2014-01-16 23:43 -0500
Message-ID<mailman.5623.1389933663.18130.python-list@python.org>
In reply to#64122
 MRAB <python@mrabarnett.plus.com> Wrote in message:
> On 2014-01-17 02:56, bob gailer wrote:
>> On 1/16/2014 8:01 PM, Sam wrote:
>>> One thing I observe about python byte-code compiling is that the main script does not gets compiled into .pyc. Only imported modules are compiled into .pyc.
>>>
>>> May I know how can I compile the main script into .pyc?
>> Duh? Just import it!
>>
> What if you want to just compile it? Importing will run it!
> 
> 

Importing will only run the portion of the code not protected by

if __name__ == "__main__":


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#64143

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-01-17 08:40 +0000
Message-ID<52d8ec89$0$29999$c3e8da3$5496439d@news.astraweb.com>
In reply to#64137
On Thu, 16 Jan 2014 23:43:02 -0500, Dave Angel wrote:

> MRAB <python@mrabarnett.plus.com> Wrote in message:
>> On 2014-01-17 02:56, bob gailer wrote:
>>> On 1/16/2014 8:01 PM, Sam wrote:
>>>> One thing I observe about python byte-code compiling is that the main
>>>> script does not gets compiled into .pyc. Only imported modules are
>>>> compiled into .pyc.
>>>>
>>>> May I know how can I compile the main script into .pyc?
>>> Duh? Just import it!
>>>
>> What if you want to just compile it? Importing will run it!
>> 
>> 
>> 
> Importing will only run the portion of the code not protected by
> 
> if __name__ == "__main__":


Nevertheless, there is no need to run *any* of the code just to compile 
it.


[steve@ando ~]$ cat sample.py
print("Hello!")

[steve@ando ~]$ ls sample.pyc
ls: sample.pyc: No such file or directory
[steve@ando ~]$ python -m compileall sample.py
Compiling sample.py ...
[steve@ando ~]$ ls sample.p*
sample.py  sample.pyc
[steve@ando ~]$ python sample.pyc
Hello!


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#64191

FromGrant Edwards <invalid@invalid.invalid>
Date2014-01-17 20:12 +0000
Message-ID<lbc2s7$itl$2@reader1.panix.com>
In reply to#64143
On 2014-01-17, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> On Thu, 16 Jan 2014 23:43:02 -0500, Dave Angel wrote:

> [steve@ando ~]$ cat sample.py
> print("Hello!")
>
> [steve@ando ~]$ ls sample.pyc
> ls: sample.pyc: No such file or directory
> [steve@ando ~]$ python -m compileall sample.py
> Compiling sample.py ...
> [steve@ando ~]$ ls sample.p*
> sample.py  sample.pyc
> [steve@ando ~]$ python sample.pyc
> Hello!

Cool!  Now I can distribute my application as a pre-compiled "binary"
just like I do for <insert language/platform/OS here>.

[That was meant ironically, BTW]

-- 
Grant Edwards               grant.b.edwards        Yow! I want EARS!  I want
                                  at               two ROUND BLACK EARS
                              gmail.com            to make me feel warm
                                                   'n secure!!

[toc] | [prev] | [next] | [standalone]


#64138

FromTerry Reedy <tjreedy@udel.edu>
Date2014-01-16 23:45 -0500
Message-ID<mailman.5624.1389933964.18130.python-list@python.org>
In reply to#64122
On 1/16/2014 10:19 PM, MRAB wrote:
> On 2014-01-17 02:56, bob gailer wrote:
>> On 1/16/2014 8:01 PM, Sam wrote:
>>> One thing I observe about python byte-code compiling is that the main
>>> script does not gets compiled into .pyc. Only imported modules are
>>> compiled into .pyc.
>>>
>>> May I know how can I compile the main script into .pyc?
>> Duh? Just import it!
>>
> What if you want to just compile it? Importing will run it!

Write the main script as

def main(): ...
if __name__ == '__main__':  main()

The difference between merely compiling 'def main' and executing it is 
trivial.

Or don't bother compiling and write main.py as one line:
   from realmain import start; start()

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#64181

FromAlister <alister.ware@ntlworld.com>
Date2014-01-17 17:04 +0000
Message-ID<EodCu.52414$T%2.47770@fx23.am4>
In reply to#64122
On Thu, 16 Jan 2014 17:01:51 -0800, Sam wrote:

> One thing I observe about python byte-code compiling is that the main
> script does not gets compiled into .pyc. Only imported modules are
> compiled into .pyc.
> 
> May I know how can I compile the main script into .pyc? It is to
> inconvenience potential copy-cats.

if you want to stop casual programmers reading your code to see how you 
have achieved something don't bother.

1) your code is not that 'Clever'(Hopefully)
2) 'Clever' code is seldom worth copying *

* 'Clever' code usually makes use of obscure functions, side effects & 
other features that make it difficult to follow & maintain
just skim through www.thedailywtf.com for examples of 'Clever' code that 
should never have seen the light of day.

if you have found a straight forward way to achive complex goal such that 
it is worth copying rest assured it will be copied no mater how hard you 
try to protect it.





-- 
#define SIGILL 6         /* blech */
             -- Larry Wall in perl.c from the perl source code

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web