Is there a portable way to run a python script from a shell without writing the full path?
For example, on Linux, I would like in my home directory
cd ~
to be able to run a python script called run.py which is in the file ~ / long / path / to / run.py, but I want to run it just by typing
python run.py
instead
python ~/long/path/to/run.py
I would like to hope for some list of search paths that contains several directories, like the PATH variable, so that python run.py runs the first run.py, it appears in one of the directories.
I considered running run.py in an executable file and adding its directory to the PATH system variable, but couldn't find a portable way to create a python script executable.
EDIT
A year after I ask about it, I am a little less noob, and I see that my question was not very clear and did not make much sense, so after raising the question I will clarify some things.
1) Portable.
When I asked this, I said portable. However, what vehicles are not clear in this case, and I did not pay much attention to this.
platforms: should work on POSIX (Linux, MacOS, etc.) and Windows
this still doesn’t make much sense, since Windows uses cmd.exe
and POSIX uses sh
, so everyone can run commands with a different syntax. Therefore, we say that the most portable feature would be to provide the same input for both sh
and cmd.exe
, running a python script in both cases. In this case, you can run the same command from the ANSI C system
function, which uses sh
on POSIX and cmd
on Windows. ANSI C is one of the few things that is typical for Windows and POSIX. In this case, the question makes sense.
2) Executable
Further, the phrase turning run.py into an executable
not very clear. Thus, I talked about the Linux strategy chmod +x run.py
, added shebang #!/usr/bin/env python
and added my directory to the system by adding the environment variable PATH ~ / long / path / to /. But then this will not work for windows, because windows do not support an executable metadata property such as Linux, and because / usr / bin / env does not necessarily exist on Windows.
3) Extension
Finally, in my head I was hoping for a solution that does not determine what type of file works, so that if we ever decide to do this, say, a perl file, the interfaces would not change.
Therefore, the run.py
entry will be bad because it will indicate the type of file; it would be better to write just run