executing python code

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Nov 5 18:14:25 EST 2004


>>>>> "Darren" == Darren Dale <dd55 at cornell.edu> 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






More information about the Python-list mailing list