Thanks to everyone for your thoughtful responses. David, this sounds like it is exactly the solution that I was looking for. I was hoping for an OS-agnostic method and this sounds like it will do the trick. I'm going to test this out soon and get back to the group.
Thanks again, - James On Apr 8, 2011, at 5:44 PM, David Goldsmith <[email protected]> wrote: >> Date: Fri, 8 Apr 2011 14:02:00 -0700 >> From: James Fort <[email protected]> >> Subject: [SEAPY] Execute script from anywhere? >> To: Seattle Python Interest Group <[email protected]> >> Message-ID: <[email protected]> >> Content-Type: text/plain; charset="iso-8859-1" >> >> Hello SeaPIG, >> >> I wonder if anyone knows how to address the following issue. I looked in a >> book and Googled for a while and couldn't find what I'm looking for. >> >> I want to have a directory or set of directories that contain Python scripts >> which I can execute from anywhere in my file system. Right now, I'm >> concerned with Windows XP, but I may want to do the same in Mac OSX and >> Linux in the future. Essentially, I want to by able to type: >> >> currentDir>>>python script.py arg1 arg2 >> >> Where arg1 and arg2 might be files in the current directory that act as >> inputs to the script and script.py is a script that resides in another >> directory. >> >> Here are some ideas I considered: >> >> - Setting the PYTHONPATH variable to include a directory where script.py >> is stored. This did not work. It seems to only work for importing >> modules once the Python interpreter is already invoked. >> - I read online and in a book that you can set "#!/usr/bin/python" as the >> first line in a script, assuming that this is the full path to the >> interpreter, put the script in /usr/bin or any other directory to which >> PATH >> points, and run the script using ">>>script.py". I read that this is >> only an option in Unix, however, and it doesn't allow me to specify the >> version of Python I want to use as would be possible if I prefixed the >> script submission command with ">>>python26 script.py". This also >> precludes me from using ">>>run script.py" from within Ipython. >> >> Does anyone know how to handle this? I noticed that I run the same script >> by copying the script into the directories in which I want to run it, but >> when I make revisions to the script, I have different versions sitting in >> different directories, which is becoming very difficult to manage. >> >> Thanks, >> >> James > > I'm probably missing something about what you want to do that makes > this not the right answer, but why not just put the scripts in a > package in your site-packages directory? Then run python -m > <package-name>.<script-name>. For example, in my site-packages > directory I have a directory utils with an empty file in it called > __init__.py (this is all you need to make a directory a package); > inside utils I have a little script I wrote, ssg.py. to test Python's > tkinter on my XP computer; if I open a command prompt in, say, My > Documents, and type "python -m utils.ssg", it runs just fine. (Note > that this approach makes your scripts not only available from the > command line anywhere on your file system, it also makes them > available to import into your any of your scripts, anywhere; indeed, > that's why this works, 'cause basically all the -m modifier does is, > quoting from http://docs.python.org/using/cmdline.html, "When [python > is] called with -m module-name, the given module is located on the > Python module path [customarily %Pythonpath%\Lib\site-packages] and > executed as a script"). (Thanks is owed to SeaPIG members Mike Orr > and Chris Barker for teaching me the elements of this approach; also, > I'm still using 2.6: the <package-name>.<script-name> syntax may need > to be modified if you're running an earlier/later version of Python.) > > HTH, > DG
