We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
daemon: address dvc directly
Our daemon is using `sys.executable -m dvc` to launch a new dvc process, but a more proper way would be to address dvc directly, by dynamically computing its location ourselves from `__file__`. See discussion https://github.com/iterative/dvc/issues/1509#issuecomment-457415307 .

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dvc/daemon.py`
Content:
```
1 """Launch `dvc daemon` command in a separate detached process."""
2 
3 import os
4 import sys
5 from subprocess import Popen
6 
7 import dvc.logger as logger
8 from dvc.utils import is_binary, fix_env
9 
10 
11 CREATE_NEW_PROCESS_GROUP = 0x00000200
12 DETACHED_PROCESS = 0x00000008
13 
14 
15 def _spawn_windows(cmd):
16     from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW
17 
18     creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS
19 
20     startupinfo = STARTUPINFO()
21     startupinfo.dwFlags |= STARTF_USESHOWWINDOW
22 
23     Popen(cmd,
24           env=fix_env(),
25           close_fds=True,
26           shell=False,
27           creationflags=creationflags,
28           startupinfo=startupinfo).communicate()
29 
30 
31 def _spawn_posix(cmd):
32     # NOTE: using os._exit instead of sys.exit, because dvc built
33     # with PyInstaller has trouble with SystemExit exeption and throws
34     # errors such as "[26338] Failed to execute script __main__"
35     try:
36         pid = os.fork()
37         if pid > 0:
38             return
39     except OSError:
40         logger.error("failed at first fork")
41         os._exit(1)  # pylint: disable=protected-access
42 
43     os.setsid()
44     os.umask(0)
45 
46     try:
47         pid = os.fork()
48         if pid > 0:
49             os._exit(0)  # pylint: disable=protected-access
50     except OSError:
51         logger.error("failed at second fork")
52         os._exit(1)  # pylint: disable=protected-access
53 
54     sys.stdin.close()
55     sys.stdout.close()
56     sys.stderr.close()
57 
58     Popen(cmd, env=fix_env(), close_fds=True, shell=False).communicate()
59 
60     os._exit(0)  # pylint: disable=protected-access
61 
62 
63 def daemon(args):
64     """Launch a `dvc daemon` command in a detached process.
65 
66     Args:
67         args (list): list of arguments to append to `dvc daemon` command.
68     """
69     cmd = [sys.executable]
70     if not is_binary():
71         cmd += ['-m', 'dvc']
72     cmd += ['daemon', '-q'] + args
73 
74     logger.debug("Trying to spawn '{}'".format(cmd))
75 
76     if os.name == 'nt':
77         _spawn_windows(cmd)
78     elif os.name == 'posix':
79         _spawn_posix(cmd)
80     else:
81         raise NotImplementedError
82 
83     logger.debug("Spawned '{}'".format(cmd))
84
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/dvc/daemon.py b/dvc/daemon.py
--- a/dvc/daemon.py
+++ b/dvc/daemon.py
@@ -2,6 +2,7 @@
 
 import os
 import sys
+import inspect
 from subprocess import Popen
 
 import dvc.logger as logger
@@ -12,7 +13,7 @@
 DETACHED_PROCESS = 0x00000008
 
 
-def _spawn_windows(cmd):
+def _spawn_windows(cmd, env):
     from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW
 
     creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS
@@ -21,14 +22,14 @@
     startupinfo.dwFlags |= STARTF_USESHOWWINDOW
 
     Popen(cmd,
-          env=fix_env(),
+          env=env,
           close_fds=True,
           shell=False,
           creationflags=creationflags,
           startupinfo=startupinfo).communicate()
 
 
-def _spawn_posix(cmd):
+def _spawn_posix(cmd, env):
     # NOTE: using os._exit instead of sys.exit, because dvc built
     # with PyInstaller has trouble with SystemExit exeption and throws
     # errors such as "[26338] Failed to execute script __main__"
@@ -55,7 +56,7 @@
     sys.stdout.close()
     sys.stderr.close()
 
-    Popen(cmd, env=fix_env(), close_fds=True, shell=False).communicate()
+    Popen(cmd, env=env, close_fds=True, shell=False).communicate()
 
     os._exit(0)  # pylint: disable=protected-access
 
@@ -71,12 +72,16 @@
         cmd += ['-m', 'dvc']
     cmd += ['daemon', '-q'] + args
 
-    logger.debug("Trying to spawn '{}'".format(cmd))
+    env = fix_env()
+    file_path = os.path.abspath(inspect.stack()[0][1])
+    env['PYTHONPATH'] = os.path.dirname(os.path.dirname(file_path))
+
+    logger.debug("Trying to spawn '{}' with env '{}'".format(cmd, env))
 
     if os.name == 'nt':
-        _spawn_windows(cmd)
+        _spawn_windows(cmd, env)
     elif os.name == 'posix':
-        _spawn_posix(cmd)
+        _spawn_posix(cmd, env)
     else:
         raise NotImplementedError

{"golden_diff": "diff --git a/dvc/daemon.py b/dvc/daemon.py\n--- a/dvc/daemon.py\n+++ b/dvc/daemon.py\n@@ -2,6 +2,7 @@\n \n import os\n import sys\n+import inspect\n from subprocess import Popen\n \n import dvc.logger as logger\n@@ -12,7 +13,7 @@\n DETACHED_PROCESS = 0x00000008\n \n \n-def _spawn_windows(cmd):\n+def _spawn_windows(cmd, env):\n     from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW\n \n     creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS\n@@ -21,14 +22,14 @@\n     startupinfo.dwFlags |= STARTF_USESHOWWINDOW\n \n     Popen(cmd,\n-          env=fix_env(),\n+          env=env,\n           close_fds=True,\n           shell=False,\n           creationflags=creationflags,\n           startupinfo=startupinfo).communicate()\n \n \n-def _spawn_posix(cmd):\n+def _spawn_posix(cmd, env):\n     # NOTE: using os._exit instead of sys.exit, because dvc built\n     # with PyInstaller has trouble with SystemExit exeption and throws\n     # errors such as \"[26338] Failed to execute script __main__\"\n@@ -55,7 +56,7 @@\n     sys.stdout.close()\n     sys.stderr.close()\n \n-    Popen(cmd, env=fix_env(), close_fds=True, shell=False).communicate()\n+    Popen(cmd, env=env, close_fds=True, shell=False).communicate()\n \n     os._exit(0)  # pylint: disable=protected-access\n \n@@ -71,12 +72,16 @@\n         cmd += ['-m', 'dvc']\n     cmd += ['daemon', '-q'] + args\n \n-    logger.debug(\"Trying to spawn '{}'\".format(cmd))\n+    env = fix_env()\n+    file_path = os.path.abspath(inspect.stack()[0][1])\n+    env['PYTHONPATH'] = os.path.dirname(os.path.dirname(file_path))\n+\n+    logger.debug(\"Trying to spawn '{}' with env '{}'\".format(cmd, env))\n \n     if os.name == 'nt':\n-        _spawn_windows(cmd)\n+        _spawn_windows(cmd, env)\n     elif os.name == 'posix':\n-        _spawn_posix(cmd)\n+        _spawn_posix(cmd, env)\n     else:\n         raise NotImplementedError\n", "issue": "daemon: address dvc directly\nOur daemon is using `sys.executable -m dvc` to launch a new dvc process, but a more proper way would be to address dvc directly, by dynamically computing its location ourselves from `__file__`. See discussion https://github.com/iterative/dvc/issues/1509#issuecomment-457415307 .\n", "before_files": [{"content": "\"\"\"Launch `dvc daemon` command in a separate detached process.\"\"\"\n\nimport os\nimport sys\nfrom subprocess import Popen\n\nimport dvc.logger as logger\nfrom dvc.utils import is_binary, fix_env\n\n\nCREATE_NEW_PROCESS_GROUP = 0x00000200\nDETACHED_PROCESS = 0x00000008\n\n\ndef _spawn_windows(cmd):\n    from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW\n\n    creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS\n\n    startupinfo = STARTUPINFO()\n    startupinfo.dwFlags |= STARTF_USESHOWWINDOW\n\n    Popen(cmd,\n          env=fix_env(),\n          close_fds=True,\n          shell=False,\n          creationflags=creationflags,\n          startupinfo=startupinfo).communicate()\n\n\ndef _spawn_posix(cmd):\n    # NOTE: using os._exit instead of sys.exit, because dvc built\n    # with PyInstaller has trouble with SystemExit exeption and throws\n    # errors such as \"[26338] Failed to execute script __main__\"\n    try:\n        pid = os.fork()\n        if pid > 0:\n            return\n    except OSError:\n        logger.error(\"failed at first fork\")\n        os._exit(1)  # pylint: disable=protected-access\n\n    os.setsid()\n    os.umask(0)\n\n    try:\n        pid = os.fork()\n        if pid > 0:\n            os._exit(0)  # pylint: disable=protected-access\n    except OSError:\n        logger.error(\"failed at second fork\")\n        os._exit(1)  # pylint: disable=protected-access\n\n    sys.stdin.close()\n    sys.stdout.close()\n    sys.stderr.close()\n\n    Popen(cmd, env=fix_env(), close_fds=True, shell=False).communicate()\n\n    os._exit(0)  # pylint: disable=protected-access\n\n\ndef daemon(args):\n    \"\"\"Launch a `dvc daemon` command in a detached process.\n\n    Args:\n        args (list): list of arguments to append to `dvc daemon` command.\n    \"\"\"\n    cmd = [sys.executable]\n    if not is_binary():\n        cmd += ['-m', 'dvc']\n    cmd += ['daemon', '-q'] + args\n\n    logger.debug(\"Trying to spawn '{}'\".format(cmd))\n\n    if os.name == 'nt':\n        _spawn_windows(cmd)\n    elif os.name == 'posix':\n        _spawn_posix(cmd)\n    else:\n        raise NotImplementedError\n\n    logger.debug(\"Spawned '{}'\".format(cmd))\n", "path": "dvc/daemon.py"}], "after_files": [{"content": "\"\"\"Launch `dvc daemon` command in a separate detached process.\"\"\"\n\nimport os\nimport sys\nimport inspect\nfrom subprocess import Popen\n\nimport dvc.logger as logger\nfrom dvc.utils import is_binary, fix_env\n\n\nCREATE_NEW_PROCESS_GROUP = 0x00000200\nDETACHED_PROCESS = 0x00000008\n\n\ndef _spawn_windows(cmd, env):\n    from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW\n\n    creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS\n\n    startupinfo = STARTUPINFO()\n    startupinfo.dwFlags |= STARTF_USESHOWWINDOW\n\n    Popen(cmd,\n          env=env,\n          close_fds=True,\n          shell=False,\n          creationflags=creationflags,\n          startupinfo=startupinfo).communicate()\n\n\ndef _spawn_posix(cmd, env):\n    # NOTE: using os._exit instead of sys.exit, because dvc built\n    # with PyInstaller has trouble with SystemExit exeption and throws\n    # errors such as \"[26338] Failed to execute script __main__\"\n    try:\n        pid = os.fork()\n        if pid > 0:\n            return\n    except OSError:\n        logger.error(\"failed at first fork\")\n        os._exit(1)  # pylint: disable=protected-access\n\n    os.setsid()\n    os.umask(0)\n\n    try:\n        pid = os.fork()\n        if pid > 0:\n            os._exit(0)  # pylint: disable=protected-access\n    except OSError:\n        logger.error(\"failed at second fork\")\n        os._exit(1)  # pylint: disable=protected-access\n\n    sys.stdin.close()\n    sys.stdout.close()\n    sys.stderr.close()\n\n    Popen(cmd, env=env, close_fds=True, shell=False).communicate()\n\n    os._exit(0)  # pylint: disable=protected-access\n\n\ndef daemon(args):\n    \"\"\"Launch a `dvc daemon` command in a detached process.\n\n    Args:\n        args (list): list of arguments to append to `dvc daemon` command.\n    \"\"\"\n    cmd = [sys.executable]\n    if not is_binary():\n        cmd += ['-m', 'dvc']\n    cmd += ['daemon', '-q'] + args\n\n    env = fix_env()\n    file_path = os.path.abspath(inspect.stack()[0][1])\n    env['PYTHONPATH'] = os.path.dirname(os.path.dirname(file_path))\n\n    logger.debug(\"Trying to spawn '{}' with env '{}'\".format(cmd, env))\n\n    if os.name == 'nt':\n        _spawn_windows(cmd, env)\n    elif os.name == 'posix':\n        _spawn_posix(cmd, env)\n    else:\n        raise NotImplementedError\n\n    logger.debug(\"Spawned '{}'\".format(cmd))\n", "path": "dvc/daemon.py"}]}