Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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


People also ask

How do I run a Python script from the path?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!

What command is used to run a Python script in the background without any hangups?

Run nohup python bgservice.py & to get the script to ignore the hangup signal and keep running.

How do I run a Python code from a specific line?

Open the parent script in an interpreter like PyCharm and select the lines you want to execute & then right click -> Execute selection in console.


1 Answers

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.

like image 165
Andrew Clark Avatar answered Sep 21 '22 13:09

Andrew Clark