How to manage a Python script portable without specifying its full path

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

+6
source share
4 answers

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

 python -m run 

The following is the documentation on the -m command line:

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

+14
source

You can make python script executable by adding

 #!/usr/bin/env python 

to the top of the file and make it executable with chmod +x .

+4
source

The answer after editing the explanation

I prefer the following approach to the sentence pointed out by @FJ because it does not require users to specify a file type. Please note that this was not indicated in the original question, so his answer to the original question was correct.

Lets you call the pytest.py file 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 the path. Common locales on Linux are: ~ / bin / for a single user, /usr/local/bin/ for all users
  • symlink ( cp -s ) file under your PATH with basename pytest instead of pytest.py

In the windows:

  • create a directory and add it to PATH. AFAIK, there’s no usual place for this, so why not C:\bin\ and ~\bin\ ?
  • add .PY to the .PY environment variable so that Windows recognizes files with the python extension as executable files without the need to enter the extension
  • associate python files with the python.exe interpreter (Windows Explorer), right-click> check the box "Always use the selected program"). There is an option in python installer that does this for you.
  • symlink pytest with the extension to the directory under PATH (using the shell command-line extension from Windows Explorer or mklink name dest from cmd)

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

+1
source
  • Make python executable (as above "alberge")
  • Create a directory and put this directory in your PATH variable
  • In this directory, create links to your python scripts
0
source

Source: https://habr.com/ru/post/912344/


All Articles