executing python code

Discussion in 'Python' started by Darren Dale, Nov 5, 2004.

  1. Darren Dale

    Darren Dale Guest

    Some time ago I asked about executing a python program or script. For
    windows, I was informed that the .py extension could be added to some list
    of executable extensions, and then I could just type "mycode" instead of
    "python mycode.py". Great advice, worked like a charm.

    I recently jumped ship, and have been running Gentoo Linux for about two
    months. Is it possible to get the same behavior on Linux? I thought it
    would have something to do with adding the #!/usr/bin/env python line to my
    code, but I'm not sure what this is supposed to do (it didnt work, that
    much I know.)

    Could I get some advice?

    Thanks,
    Darren
    Darren Dale, Nov 5, 2004
    #1
    1. Advertising

  2. Darren Dale

    Michael Fuhr Guest

    Darren Dale <> writes:

    > Some time ago I asked about executing a python program or script. For
    > windows, I was informed that the .py extension could be added to some list
    > of executable extensions, and then I could just type "mycode" instead of
    > "python mycode.py". Great advice, worked like a charm.
    >
    > I recently jumped ship, and have been running Gentoo Linux for about two
    > months. Is it possible to get the same behavior on Linux? I thought it
    > would have something to do with adding the #!/usr/bin/env python line to my
    > code, but I'm not sure what this is supposed to do (it didnt work, that
    > much I know.)


    How didn't it work? What did you do and what happened? What errors
    were printed? Did you use chmod to make the script executable? Is
    the script in a directory that's in your PATH? Is Python installed
    on the system?

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    Michael Fuhr, Nov 5, 2004
    #2
    1. Advertising

  3. Darren Dale

    John Hunter Guest

    >>>>> "Darren" == Darren Dale <> writes:

    Darren> Some time ago I asked about executing a python program or
    Darren> script. For windows, I was informed that the .py extension
    Darren> could be added to some list of executable extensions, and
    Darren> then I could just type "mycode" instead of "python
    Darren> mycode.py". Great advice, worked like a charm.

    Darren> I recently jumped ship, and have been running Gentoo Linux
    Darren> for about two months. Is it possible to get the same
    Darren> behavior on Linux? I thought it would have something to do
    Darren> with adding the #!/usr/bin/env python line to my code, but
    Darren> I'm not sure what this is supposed to do

    '#!/usr/bin/env python' will try and run the script using the default
    python in your environment. If you wanted to specify a specific
    python, you could use, for example

    #!/usr/bin/python2.1

    Darren> it didnt work, that much I know.

    You need to set the executable bit on the file. windows determines
    whether a file is executable based on the extension, and linux based
    on the file mode

    > chmod +x myscript.py
    > ./myscript.py


    The ./ part says to use the file in the current working directory, and
    may be required if the current directory is not in your PATH. You can
    copy myscript anywhere in your PATH (eg /usr/local/bin), resource your
    bashrc file (or open an new terminal) and the just do

    > myscript.py


    or with tab completion ....

    > myscTAB # should complete myscript.py


    Finally, you may want to add a pybin dir to your home dir where your
    personal scripts reside. If you do that, just add that to your path,
    eg in your .bashrc file.

    JDH
    John Hunter, Nov 5, 2004
    #3
  4. Darren Dale

    Ian Bicking Guest

    Darren Dale wrote:
    > Some time ago I asked about executing a python program or script. For
    > windows, I was informed that the .py extension could be added to some list
    > of executable extensions, and then I could just type "mycode" instead of
    > "python mycode.py". Great advice, worked like a charm.
    >
    > I recently jumped ship, and have been running Gentoo Linux for about two
    > months. Is it possible to get the same behavior on Linux? I thought it
    > would have something to do with adding the #!/usr/bin/env python line to my
    > code, but I'm not sure what this is supposed to do (it didnt work, that
    > much I know.)


    chmod +x mycode.py
    ../mycode.py

    Note: . (the present directory) probably isn't in your $PATH, so you
    have to use ./ to tell the shell to look in the present directory. Do
    "echo $PATH" to see where you can put it where it's always accessible.
    You also have to include the extension. If you want, you can rename it
    to leave off the extension, and that will work fine. You can also make
    a symlink, like (as root):

    cd /usr/local/bin
    ln -s /path/to/mycode.py mycode

    ~/bin is also often in the $PATH, instead of putting it in /usr/local/bin.

    --
    Ian Bicking / / http://blog.ianbicking.org
    Ian Bicking, Nov 5, 2004
    #4
  5. Darren Dale

    Darren Dale Guest

    Michael Fuhr wrote:

    > Darren Dale <> writes:
    >
    >> Some time ago I asked about executing a python program or script. For
    >> windows, I was informed that the .py extension could be added to some
    >> list of executable extensions, and then I could just type "mycode"
    >> instead of "python mycode.py". Great advice, worked like a charm.
    >>
    >> I recently jumped ship, and have been running Gentoo Linux for about two
    >> months. Is it possible to get the same behavior on Linux? I thought it
    >> would have something to do with adding the #!/usr/bin/env python line to
    >> my code, but I'm not sure what this is supposed to do (it didnt work,
    >> that much I know.)

    >
    > How didn't it work? What did you do and what happened? What errors
    > were printed? Did you use chmod to make the script executable? Is
    > the script in a directory that's in your PATH? Is Python installed
    > on the system?
    >


    Yes, I did chmod, the script is in the current directory.

    $ mycode.py
    bash: mycode.py: command not found

    I just tried:

    $ ./mycode.py

    and that will execute.
    Darren Dale, Nov 5, 2004
    #5
  6. Darren Dale

    Michael Fuhr Guest

    Darren Dale <> writes:

    > Yes, I did chmod, the script is in the current directory.
    >
    > $ mycode.py
    > bash: mycode.py: command not found
    >
    > I just tried:
    >
    > $ ./mycode.py
    >
    > and that will execute.


    That means the directory isn't in your PATH environment variable,
    neither as a full path (/path/to/the/directory) nor as ".", which
    means the current directory. For security reasons it's usually
    wise to leave "." out of your PATH, so if you don't want to type
    ../mycode.py each time then put your scripts in a directory that
    PATH knows about. As others have mentioned, a common place for
    a user's private scripts is in ~/bin, that is, the "bin" directory
    under your home directory. If it doesn't exist then you can use
    mkdir to create it, and if it's not in your PATH then you'll need
    to add it.

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    Michael Fuhr, Nov 5, 2004
    #6
  7. Darren Dale

    Mike Meyer Guest

    (Michael Fuhr) writes:

    > Darren Dale <> writes:
    >> Yes, I did chmod, the script is in the current directory.
    >> $ mycode.py
    >> bash: mycode.py: command not found
    >>
    >> I just tried:
    >> $ ./mycode.py
    >> and that will execute.

    >
    > That means the directory isn't in your PATH environment variable,
    > neither as a full path (/path/to/the/directory) nor as ".", which
    > means the current directory. For security reasons it's usually
    > wise to leave "." out of your PATH, so if you don't want to type


    Actually, those reasons relate to having "." early in your path. If
    it's the last thing on your path, you won't trigger any trojan horses.

    > ./mycode.py each time then put your scripts in a directory that
    > PATH knows about. As others have mentioned, a common place for
    > a user's private scripts is in ~/bin, that is, the "bin" directory
    > under your home directory. If it doesn't exist then you can use
    > mkdir to create it, and if it's not in your PATH then you'll need
    > to add it.


    I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    get to invoke it without having to specify the extension, and don't
    have to reinstall the file if I change it.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Nov 6, 2004
    #7
  8. Darren Dale

    Michael Fuhr Guest

    Mike Meyer <> writes:
    > (Michael Fuhr) writes:
    > > For security reasons it's usually wise to leave "." out of your PATH

    >
    > Actually, those reasons relate to having "." early in your path. If
    > it's the last thing on your path, you won't trigger any trojan horses.


    Putting "." lessens the chances of tripping over a trojan but it
    doesn't eliminate the possibility entirely. Think about typos or
    commands that you thought were earlier in your path but aren't --
    the latter might happen, for example, if you move to a different
    OS and aren't aware that some commands are in a location that's not
    in your default path.

    Relying on "." being in your path might also lead you to write code
    that breaks for somebody who doesn't have "." in their path. An
    example would be a Makefile or build script that runs other commands
    in the same directory and assumes those commands will be in the
    user's path. It's sloppy to make that assumption, but people get
    careless and sloppy things slip by.

    But this is getting off topic for Python.

    > I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    > get to invoke it without having to specify the extension, and don't
    > have to reinstall the file if I change it.


    I sometimes do that as well.

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    Michael Fuhr, Nov 6, 2004
    #8
  9. * Darren Dale (2004-11-05 23:47 +0200)
    > Some time ago I asked about executing a python program or script. For
    > windows, I was informed that the .py extension could be added to some list
    > of executable extensions, and then I could just type "mycode" instead of
    > "python mycode.py". Great advice, worked like a charm.


    "PATHEXT"

    > I recently jumped ship, and have been running Gentoo Linux for about two
    > months. Is it possible to get the same behavior on Linux?


    No.

    > I thought it
    > would have something to do with adding the #!/usr/bin/env python line to my
    > code, but I'm not sure what this is supposed to do (it didnt work, that
    > much I know.)
    >
    > Could I get some advice?


    You're confusing a lot of things. Something like "PATHEXT" doesn't
    exist on Linux. So you always have to type the full name of the script
    (or use tab completion).

    You can type "./myapp.py" if the script has the "she bang" first line
    mentioned above and the file is executable.

    You can type "myapp.py" if the script has the "she bang" first line
    mentioned above and the file is executable and the current directory
    is in your path (which it is /not/ the default under Linux - contrary
    to Windows - and which is not advised).

    You can type myapp.py - even if the script is not executable and not
    in your path and doesn't have a "#!" line if you use an advanced shell
    like zsh (Z Shell) and add a suffix alias ("alias -s py=python") to
    your .zshrc

    Just typing "myapp" won't work.

    Thorsten
    Thorsten Kampe, Nov 6, 2004
    #9
  10. According to Mike Meyer <>:
    > I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    > get to invoke it without having to specify the extension, and don't
    > have to reinstall the file if I change it.


    I find that I hardlink them. I'm also more likely to edit ~/bin/mycode than
    ~/src/mycode.py.

    Cheers.

    --
    Ng Pheng Siong <>

    http://sandbox.rulemaker.net/ngps -+- M2Crypto, ZServerSSL for Zope, Blog
    http://www.sqlcrypt.com -+- Transparent AES Encryption For SQLite
    Ng Pheng Siong, Nov 8, 2004
    #10
  11. Darren Dale

    Mike Meyer Guest

    (Ng Pheng Siong) writes:

    > According to Mike Meyer <>:
    >> I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    >> get to invoke it without having to specify the extension, and don't
    >> have to reinstall the file if I change it.

    >
    > I find that I hardlink them. I'm also more likely to edit ~/bin/mycode than
    > ~/src/mycode.py.


    I keep the src in CVS, so I want to edit it. Given that, hardlinks
    have a tendency to be pointing at the wrong thing.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Nov 8, 2004
    #11
  12. Darren Dale

    Steve Holden Guest

    Mike Meyer wrote:

    > (Michael Fuhr) writes:
    >
    >
    >>Darren Dale <> writes:
    >>
    >>>Yes, I did chmod, the script is in the current directory.
    >>>$ mycode.py
    >>>bash: mycode.py: command not found
    >>>
    >>>I just tried:
    >>>$ ./mycode.py
    >>>and that will execute.

    >>
    >>That means the directory isn't in your PATH environment variable,
    >>neither as a full path (/path/to/the/directory) nor as ".", which
    >>means the current directory. For security reasons it's usually
    >>wise to leave "." out of your PATH, so if you don't want to type

    >
    >
    > Actually, those reasons relate to having "." early in your path. If
    > it's the last thing on your path, you won't trigger any trojan horses.
    >

    Sorry, but the advice still stands. As long as commands are typed
    correctly everything is OK, but someone (like me) who makes a lot of
    typing mistakes runs the risk of triggering a trojan whose name is a
    misspelling of a command. So the best advice about putting "." on your
    path is "don;t do that".

    >
    >>./mycode.py each time then put your scripts in a directory that
    >>PATH knows about. As others have mentioned, a common place for
    >>a user's private scripts is in ~/bin, that is, the "bin" directory
    >>under your home directory. If it doesn't exist then you can use
    >>mkdir to create it, and if it's not in your PATH then you'll need
    >>to add it.

    >
    >
    > I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    > get to invoke it without having to specify the extension, and don't
    > have to reinstall the file if I change it.
    >
    > <mike


    That works too. Of course under Linux/Unix there's no particular reason
    to use the .py extension in the first place.

    regards
    Steve
    --
    http://www.holdenweb.com
    http://pydish.holdenweb.com
    Holden Web LLC +1 800 494 3119
    Steve Holden, Nov 8, 2004
    #12
  13. Darren Dale

    Mike Meyer Guest

    Steve Holden <> writes:

    > Mike Meyer wrote:
    >> I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    >> get to invoke it without having to specify the extension, and don't
    >> have to reinstall the file if I change it.
    >> <mike

    >
    > That works too. Of course under Linux/Unix there's no particular
    > reason to use the .py extension in the first place.


    Some editors use the extension to determine the language mode to run
    in. And distutils uses .py extensions.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Nov 8, 2004
    #13
  14. Darren Dale

    Steve Holden Guest

    Mike Meyer wrote:

    > Steve Holden <> writes:
    >
    >
    >>Mike Meyer wrote:
    >>
    >>>I find that symlinking ~/src/python/mycode.py to ~/bin/mycode means I
    >>>get to invoke it without having to specify the extension, and don't
    >>>have to reinstall the file if I change it.
    >>> <mike

    >>
    >>That works too. Of course under Linux/Unix there's no particular
    >>reason to use the .py extension in the first place.

    >
    >
    > Some editors use the extension to determine the language mode to run
    > in. And distutils uses .py extensions.
    >
    > <mike


    There is that. I was thinking only of the execution scenario.

    regards
    Steve
    --
    http://www.holdenweb.com
    http://pydish.holdenweb.com
    Holden Web LLC +1 800 494 3119
    Steve Holden, Nov 9, 2004
    #14
    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. =?Utf-8?B?QXNoYQ==?=

    executing client side code from code behind.

    =?Utf-8?B?QXNoYQ==?=, Aug 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    =?Utf-8?B?QWJL?=
    Aug 12, 2004
  2. TheDustbustr
    Replies:
    2
    Views:
    353
    Christian Tismer
    Aug 17, 2003
  3. sarmin kho
    Replies:
    1
    Views:
    362
    Cameron Laird
    Jun 4, 2004
  4. Tim Rau

    Executing other python code

    Tim Rau, Jan 29, 2008, in forum: Python
    Replies:
    6
    Views:
    260
    Guyon Morée
    Jan 31, 2008
  5. SiWi
    Replies:
    11
    Views:
    840
Loading...

Share This Page