Repository: pallets/werkzeug

Issue:
Reloader using wrong python executable
Python version: `3.6.5`
Werkzeug version: `0.15.4`

The change made as a result of this PR, https://github.com/pallets/werkzeug/pull/1242, causes inconsistent behaviour when specifying how a script should be run.

`sys.executable` might not (validly) equal the file's shebang.
For example, a developer might want to test their program with a different version of python, or run under a different environment to where the program would be deployed.

The biggest use case is running a script with a debugger, as seen with this issue: https://github.com/pallets/werkzeug/issues/1493

The developer shouldn't be forced to change shebangs or to change the execute permissions of the file to make it work.

In summary, if an executable is specified, then that executable should be used; regardless of the file's shebang.

Since the intention was for this to work with NixOS, I propose that if the functionality is to remain then it should be exclusive to NixOS (where it was pointed out on the PR that it seems to be a problem with the OS), similarly to Windows. 


Discussion:
Remove the interpreter line and only use the desired `python` commands. Or use tox to configure venvs for tests, where `python` will always refer to the correct interpreter.
I agree that there are ways to set this up so that it works correctly.

As mentioned in this issue, https://github.com/pallets/werkzeug/issues/1482 , the solution to make it work on NixOS doesn't feel right over all.

The file being executed shouldn't have the final say on _how_  it should be executed. 

I'm happy to open a PR, but at this stage I don't know how to satisfy all users without adding in an additional option.
I also encountered this issue while trying to upgrade. In my case, I have a main script that starts with `#!/usr/bin/env python`. Until werkzeug 0.15, I could run it with either of these:
```bash
source venv/bin/activate
./run.py ...
```
```bash
venv/bin/python run.py ...
```
Both were equivalent. After #1242, this is no longer possible.

About the suggested fixes, tox doesn't fit because I don't have multiple venvs, and chmod -x run.py would fix the second way but break the first way. Also, the app doesn't have a setup.py (just requirements.txt) so I can't use entry_points.

Resolving patch:
diff --git a/src/werkzeug/_reloader.py b/src/werkzeug/_reloader.py
--- a/src/werkzeug/_reloader.py
+++ b/src/werkzeug/_reloader.py
@@ -61,11 +61,8 @@ def _find_observable_paths(extra_files=None):
 
 
 def _get_args_for_reloading():
-    """Returns the executable. This contains a workaround for windows
-    if the executable is incorrectly reported to not have the .exe
-    extension which can cause bugs on reloading.  This also contains
-    a workaround for linux where the file is executable (possibly with
-    a program other than python)
+    """Determine how the script was executed, and return the args needed
+    to execute it again in a new process.
     """
     rv = [sys.executable]
     py_script = sys.argv[0]
@@ -91,11 +88,6 @@ def _get_args_for_reloading():
             ):
                 rv.pop(0)
 
-        elif os.path.isfile(py_script) and os.access(py_script, os.X_OK):
-            # The file is marked as executable. Nix adds a wrapper that
-            # shouldn't be called with the Python executable.
-            rv.pop(0)
-
         rv.append(py_script)
     else:
         # Executed a module, like "python -m werkzeug.serving".


Test patch: