compile shebang into pyc file

Discussion in 'Python' started by Joerg Schuster, Apr 26, 2005.

  1. Hello,

    is there a way to compile a python file foo.py to foo.pyc (or foo.pyo)
    such that foo.pyc can be run with 'foo.pyc' (as opposed to 'python
    foo.pyc') on the command line?

    Jörg Schuster
     
    Joerg Schuster, Apr 26, 2005
    #1
    1. Advertising

  2. Joerg Schuster

    Guest

    Is there some reason why you want to run the .pyc file, rather than the
    ..py file? If you start the script with

    #! /usr/bin/env python

    Then if the file has the execution permission set, typing the file name
    (foo.py) will make the script call up the Python interpreter on its
    own.
     
    , Apr 27, 2005
    #2
    1. Advertising

  3. On 26 Apr 2005 18:15:51 -0700,
    <> wrote:
    > Is there some reason why you want to run the .pyc file, rather than the
    > .py file? If you start the script with


    a. It's more efficient, since the code doesn't need to be compiled
    before it's run.
    b. What if you want to ship closed-source?

    Mike

    --
    Michael P. Soulier <>
    http://www.digitaltorque.ca
    http://opag.ca python -c 'import this'
     
    Michael Soulier, Apr 27, 2005
    #3
  4. Michael Soulier wrote:

    > > Is there some reason why you want to run the .pyc file, rather than the
    > > .py file? If you start the script with

    >
    > a. It's more efficient, since the code doesn't need to be compiled
    > before it's run.
    > b. What if you want to ship closed-source?


    #!/usr/bin/env python
    import app

    hardly qualifies as "hard to compile" or "open source"

    </F>
     
    Fredrik Lundh, Apr 27, 2005
    #4
  5. > #!/usr/bin/env python
    > import app


    Yes, of course this is a possibility. But it implies having (or giving
    away) two files.

    I think having one file is always better than having two files. Because
    if you have two files, you need a third one: a README that tells you
    what to do with the two files and that one of the files must either be
    in $PYTHONPATH, or in the same directory as the other one and '.' must
    be in your $PYTHONPATH , and so on. Actually, some people will also
    need a fourth file: One that contains an explanation of terms like
    "$PYTHONPATH" and the like.

    Jörg
     
    Joerg Schuster, Apr 27, 2005
    #5
  6. Joerg Schuster

    Steve M Guest

    I just happened across the page linked to below, and remembered this
    thread, and, well... here you go:

    http://www.lyra.org/greg/python/

    Executable .pyc files

    Ever wanted to drop a .pyc file right into your web server's
    cgi-bin directory? Frustrated because the OS doesn't know what to do
    with a .pyc? (missing the #! line)

    Look no further! :) Below is a tiny Bash script to do this. "cat"
    your .pyc onto the end of this and drop it wherever you need direct
    execution of that .pyc
     
    Steve M, Apr 27, 2005
    #6
  7. On Wed, 27 Apr 2005 10:37:11 -0400, Michael Soulier <>
    declaimed the following in comp.lang.python:
    > a. It's more efficient, since the code doesn't need to be compiled
    > before it's run.
    > b. What if you want to ship closed-source?
    >

    You stuff the real code into a module that gets imported (hence
    cached as a .pyc), and the main program in source looks like:

    import thedirtystuff
    thedirtystuff.main()

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Apr 27, 2005
    #7
  8. Joerg Schuster wrote:

    > > #!/usr/bin/env python
    > > import app

    >
    > Yes, of course this is a possibility. But it implies having (or giving
    > away) two files.


    yeah, think of all the disk space you'll waste!

    > Because if you have two files, you need a third one: a README
    > that tells you what to do with the two files


    so you're saying that the set of people that can deal with no more than one
    file at a time but knows how to install and configure Python (which in itself
    comes with a few thousand files) is larger than zero?

    > I think having one file is always better than having two files.


    so you don't ever use modules?

    you haven't distributed many Python programs, have you?

    </F>
     
    Fredrik Lundh, Apr 27, 2005
    #8
  9. On 2005-04-27, Fredrik Lundh <> wrote:

    > so you're saying that the set of people that can deal with no
    > more than one file at a time but knows how to install and
    > configure Python (which in itself comes with a few thousand
    > files) is larger than zero?


    There are a lot of Linux users who already have python
    installed but don't know it. Python was always a required package for
    a RedHat install. It's not required on some distros, but it's
    installed by default and you've got to go out of your way to
    de-selected it when you install.

    I've found that giving Linux users a single file "executable"
    Python script works wonderfully, but it's really only an option
    for fairly small applications -- and I don't care if they have
    source code.

    --
    Grant Edwards grante Yow! Look!! Karl Malden!
    at
    visi.com
     
    Grant Edwards, Apr 27, 2005
    #9
  10. Joerg Schuster

    rbt Guest

    Fredrik Lundh wrote:
    > Joerg Schuster wrote:
    >
    >> > #!/usr/bin/env python
    >> > import app

    >>
    >> Yes, of course this is a possibility. But it implies having (or giving
    >> away) two files.

    >
    >
    > yeah, think of all the disk space you'll waste!
    >
    >> Because if you have two files, you need a third one: a README
    >> that tells you what to do with the two files

    >
    >
    > so you're saying that the set of people that can deal with no more than one
    > file at a time but knows how to install and configure Python (which in
    > itself
    > comes with a few thousand files) is larger than zero?


    On Windows, installing Python takes only a few clicks. No brain cycles
    needed ;)
     
    rbt, Apr 27, 2005
    #10
  11. On Wed, 27 Apr 2005 16:48:35 -0400, rbt <>
    declaimed the following in comp.lang.python:


    > On Windows, installing Python takes only a few clicks. No brain cycles
    > needed ;)


    Until you try to set it so that ActiveState Python(win) doesn't
    conflict with Plone-2 Python (at least I'm still on 2.3.x -- If it
    worked, I removed ASP, installed P2, then told ASP to install into the
    P2 Python directory...)

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Apr 28, 2005
    #11
  12. > so you're saying that the set of people that can deal with
    > no more than one
    > file at a time but knows how to install and configure Python
    > (which in itself
    > comes with a few thousand files) is larger than zero?


    Take me as an example: Very often, I needed software that could solve a
    specific problem. I used to find, say, 10 different packages. Usually
    none of the programs REALLY did the job as I wanted it to be done. Yet,
    in order to find this out (or in order to find the package that suited
    my needs best), I had to try out all of these packages. This used to
    take an unnecessary amount of time.

    I would have found (say) one java program, one c++ program, one Python
    program, one perl program, ...

    I know that java, c++, Python and Perl are all installed on our system.
    And if they weren't, I would know how to install them myself: By using
    yast or by saying 'apt-get install <package>'.

    Yet: I don't want to have to know how to install all these packages in
    all these different languages. I think a program should work out of the
    box. And the should be no fumbling it out of the box.

    Jörg
     
    Joerg Schuster, Apr 28, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ernesto

    Compile C program -> .pyc file

    Ernesto, Oct 21, 2005, in forum: Python
    Replies:
    5
    Views:
    460
    Grant Edwards
    Oct 21, 2005
  2. Replies:
    7
    Views:
    616
    David Wahler
    Mar 1, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    870
    Lionel B
    Mar 1, 2007
  4. dmitrey
    Replies:
    1
    Views:
    341
    Arnaud Delobelle
    Sep 2, 2007
  5. Sam
    Replies:
    7
    Views:
    66
    Grant Edwards
    Jan 17, 2014
Loading...

Share This Page