How to run a Python script portably without specifying its full path

Go To StackoverFlow.com

9

Is there a portable way to run a python script from a shell without writing its full path?

For example in Linux, I would like while in my home directory

cd ~

to be able to run a python script called run.py that is in say, ~/long/path/to/run.py, but I want to run it by simply typing

python run.py

instead of

python ~/long/path/to/run.py

I would hope for some kind of search path list that contains several directories just like the PATH variable, so that python run.py runs the first run.py it encounters in one of the directories.

I have considered turning run.py into an executable and adding its directory the system PATH variable, but could not find a portable way of making a python script executable.

EDIT

One year later after asking it, I am a bit less noob, and I see that my question was not very clear and did not make much sense, so after a question upvote I'll clarify some things.

1) Portable.

When I asked this I said portable. However what portable means is not clear in this case, and I did not give much emphasis to it.

  • the platforms: should work on POSIX (Linux, MacOS, etc.) and Windows

  • this still does not make much sense since windows uses cmd.exe, and POSIX uses sh, so each one could run the commands with a different syntax. So let's say that the most portable thing possible would be to feed the same input to both sh and cmd.exe, running the python script in both cases. In this case, you could run the same command from an ANSI C system function, which uses sh on POSIX and cmd on Windows. ANSI C being one of the few things that is common to Windows and POSIX, the question makes some sense in that case.

2) Executable

Next, the phrase turning run.py into an executable, is not very clear. By that I was talking about the Linux strategy of chmod +x run.py, add a shebang #!/usr/bin/env python, and adding its directory the system add ~/long/path/to/ the PATH enviroment variable. But then this won't work for windows because windows does not support an executable file metadata property like Linux and because /usr/bin/env does not necessarily exist in Windows.

3) Extension

Finally, in my head I was hoping for a solution that does not specify what kind of file run is, so that if someday we decide to make it, say, a perl file, no interfaces would change.

Therefore, writing run.py would be bad because it would specify the filetype; it would be better to be able to write just run

2012-04-03 22:21
by XXX


15

If the directory containing run.py is on the module search path (for example, PYTHONPATH environment variable), you should be able to run it like this:

python -m run

Here is the documentation on the -m command line option:

-m module-name
Searches sys.path for the named module and runs the corresponding .py file as a script.

2012-04-03 22:28
by Andrew Clark
Thanks F.J, this is exactly what I was looking for, I feel that it has two advantages over the '#!' approach: - XXX 2012-04-04 10:40
(specific Sorry, half finished comment)... Thanks F.J, this is what I was looking for, I feel that it has two advantages over the '#!' approach: 1) not cluttering scripts 2) not cluttering the global PATH variable, and using the more specific PYTHONPATH instead. The disadvantage is of course having to type "python -m" all the time, which takes up space and worse, could mean rewriting code if you ever decide one day to transform you python scripts into, say, bash scripts - XXX 2012-04-04 11:02
I tried python -m run but it didn't work... I am sure that run.py is in the PYTHONPATH since I can import it and it shows when I do import sys; print sys.path... am I missing something - XXX 2012-04-04 12:48
@ciro Do you see an error message? Is it just "No module named run" or something else - Andrew Clark 2012-04-04 15:45
Accepted because it answers the original question, including the portability requirement (which was not enough emphasized originally - XXX 2013-07-06 08:48


5

You can make a python script executable by adding

#!/usr/bin/env python

to the beginning of the file, and making it executable with chmod +x.

2012-04-03 22:25
by A B
Thanks alberge, I was not sure this works on windows, but it seems that #!/usr/bin/env python will be treated as a comment, and the python installation auto associates running .py files with the python interpreter. This is specially great if you put .py in your EXTPATH, so you can run python files simply by typing "run" instead of "run.py", which means that if you ever decide to transform your python scripts into say, Perl, you won't need to rewrite the script calls. It would be great if there was a simple way to run scripts in Linux without writing the extension and without adding #!.. - XXX 2012-04-04 11:08
This works great with virtualen - Henrik 2017-09-01 15:40


2

Answer after the clarification edit

I prefer the following approach to the one suggested by @F.J. because it does not require users to specify the file type. Please note that this was not specified in the original question, so his answer to the original question was correct.

Lets call the file pytest.py to avoid conflicts with a possible existing run program.

On POSIX (MacOs, linux) do what @Petr said, which is based on what @alberge said:

  • chmod +x
  • add shebang #!/usr/bin/env python
  • create a directory and add it to path. Usual locations on Linux are: ~/bin/ for a single user, /usr/local/bin/ for all users
  • symlink (cp -s) the file under your PATH with basename pytest instead of pytest.py

On windows:

  • create a dir and add it to PATH. AFAIK, there is no conventional place for that, so why not C:\bin\ and ~\bin\?
  • add .PY to the PATHEXT environment variable so that Windows will recognize files with python extension as runnable files without requiring you to type the extension
  • associate python files with the python.exe interpreter (Windows Explorer > right click > check "Always use the selected program"). There is an option on the python installer that does this for you.
  • symlink pytest with extension into the dir under PATH (using Link Shell Extension from Windows Explorer or mklink name dest from cmd )

Now system( "pytest" ); should work in both systems ( sh under Linux, cmd under Windows )

2013-07-06 08:32
by XXX


0

  1. Make python file executable (as "alberge" stated above)
  2. Create some directory and put this directory into your PATH variable
  3. In this directory, create links to your python scripts
2012-04-03 22:34
by Petr Újezdský
Ads