9 examples of 'how to set path in python' in Python

Every line of 'how to set path in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
300def set_path_env_variable():
301 DEFAULT_PATH_ENV = "/usr/local/bin:/usr/bin/:/bin"
302 if os.environ.get('PATH') is None or os.environ["PATH"] == DEFAULT_PATH_ENV:
303 os.environ["PATH"] = ":".join(["/var/lang/bin", DEFAULT_PATH_ENV])
313def configure_python_path(opts):
314 if not opts['--exclude-working-directory']:
315 sys.path.insert(0, os.getcwd())
6def _add_to_python_path(p):
7 if p not in sys.path: sys.path.append(p)
15def set_local_path():
16 """ Prepend local directory to sys.path.
17
18 The caller is responsible for removing this path by using
19
20 restore_path()
21 """
22 f = sys._getframe(1)
23 if f.f_locals['__name__']=='__main__':
24 testfile = sys.argv[0]
25 else:
26 testfile = f.f_locals['__file__']
27 local_path = os.path.normpath(os.path.dirname(os.path.abspath(testfile)))
28 sys.path.insert(0,local_path)
29 return
15def patch_sys_path():
16 """
17 Add self sources to sys.path, so that directly using this script
18 from the sources works
19
20 """
21 this_dir = os.path.dirname(__file__)
22 to_add = os.path.join(this_dir, "../python/")
23 to_add = os.path.abspath(to_add)
24 sys.path.insert(0, to_add)
42def addSysPythonPath():
43 import appinfo
44 import os
45
46 def addpath(p):
47 try:
48 p = os.path.normpath(p)
49 p = os.path.abspath(p)
50 except OSError: return
51 if not os.path.exists(p): return
52 if p not in sys.path: sys.path += [p]
53
54 paths = os.environ.get("PYTHONPATH", "").split(":")
55 for p in paths:
56 addpath(p.strip())
57
58 versionStr = ".".join(map(str, sys.version_info[0:2]))
59
60 if sys.platform == "darwin":
61 addpath("/usr/local/Frameworks/Python.framework/Versions/%s/lib/python%s/lib-dynload/" % (versionStr, versionStr))
62 addpath("/System/Frameworks/Python.framework/Versions/%s/lib/python%s/lib-dynload/" % (versionStr, versionStr))
63
64 # This will add other custom paths, e.g. for eggs.
65 import site
66 site.main()
67
68 def addsitedir(d):
69 try:
70 d = os.path.normpath(d)
71 d = os.path.abspath(d)
72 except OSError: return
73 if os.path.exists(d):
74 site.addsitedir(d)
75
76 # We still might miss some site-dirs.
77 addsitedir("/usr/local/lib/python%s/site-packages" % versionStr)
78 addsitedir("/usr/lib/python%s/site-packages" % versionStr)
79 if sys.platform == "darwin":
80 addsitedir("/Library/Python/%s/site-packages" % versionStr)
81
82 if not appinfo.args.forkExecProc:
83 print("Python paths after: %r" % sys.path)
11def setup_module(module):
12 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
29@staticmethod
30def setPath(path):
31 """
32 :type path: basestring
33 """
34 if not(isinstance(path, basestring)):
35 raise TypeError
36
37 if not os.path.exists(path) and os.path.isdir(path):
38 raise Exception("The provided path does not exist")
39 AppVars.applicationPath = path
94def fix_paths():
95 import site
96 if not hasattr(site, "USER_BASE"): return # We are running py2app
97 if os.path.basename(__file__) != 'run.py': return # Not running from source
98
99 # Fix import path: add parent directory(so that we can
100 # import vistrails.[gui|...] and remove other paths below it (we might have
101 # been started from a subdir)
102 # A better solution is probably to move run.py up a
103 # directory in the repo
104 old_dir = os.path.realpath(os.path.dirname(__file__))
105 vistrails_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
106 i = 0
107 while i < len(sys.path):
108 rpath = os.path.realpath(sys.path[i])
109 if rpath.startswith(old_dir):
110 del sys.path[i]
111 else:
112 i += 1
113 if vistrails_dir not in sys.path:
114 sys.path.insert(0, vistrails_dir)

Related snippets