10 examples of 'python module directory' in Python

Every line of 'python module directory' 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
this disclaimer
39def module_dir(module):
40 """
41 Find the name of the directory that contains a module, if possible.
42
43 Raise ValueError otherwise, e.g. for namespace packages that are split
44 over several directories.
45 """
46 # Convert to list because _NamespacePath does not support indexing on 3.3.
47 paths = list(getattr(module, '__path__', []))
48 if len(paths) == 1:
49 return paths[0]
50 else:
51 filename = getattr(module, '__file__', None)
52 if filename is not None:
53 return os.path.dirname(filename)
54 raise ValueError("Cannot determine directory containing %s" % module)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11def setup_module(module):
12 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
67@staticmethod
68def get_module_directory(module_name):
69 try:
70 module = import_module(module_name)
71 except ImportError as e:
72 # I hate doing this, but I don't want to squash other import
73 # errors. Might be better to try a directory check directly.
74 if "No module named" in str(e) and FIXTURES_MODULE_NAME in str(e):
75 return
76 raise
77
78 try:
79 directory = os.path.dirname(module.__file__)
80 except AttributeError:
81 # No __file__ available for module
82 return
83
84 return directory
111def load_external_module(base: str, module: str) -> None:
112 current = os.path.dirname(os.path.abspath(base))
113 module_dir = join(os.path.dirname(current), module)
114 sys.path.insert(0, module_dir)
12def find_modules():
13 res = list()
14 for entry in sorted(os.listdir(os.getcwd())):
15 init_py = os.path.join(entry, "__init__.py")
16 if os.path.exists(init_py):
17 res.append(entry)
18 if entry.endswith(".py"):
19 res.append(entry)
20 res.remove("tasks.py")
21 return res
27def find_matching_path_dirs(moduledir):
28 if moduledir is '':
29 return sys.path
30
31 ds = []
32 for dir in sys.path:
33 test_path = os.path.join(dir, moduledir)
34 if os.path.exists(test_path) and os.path.isdir(test_path):
35 ds.append(test_path)
36 return ds
192def scan_for_modules(start, options):
193 modules = []
194
195 skip_tests = []
196 if options.skip_tests:
197 skip_tests = options.skip_tests.split()
198
199 for dirpath, subdirs, filenames in os.walk(start):
200 # Only look in and below subdirectories that are python modules.
201 if '__init__.py' not in filenames:
202 if options.full:
203 for filename in filenames:
204 if filename.endswith('.pyc'):
205 os.unlink(os.path.join(dirpath, filename))
206 # Skip all subdirectories below this one, it is not a module.
207 del subdirs[:]
208 if options.debug:
209 print 'Skipping', dirpath
210 continue # Skip this directory.
211
212 # Look for unittest files.
213 for fname in filenames:
214 if fname.endswith('_unittest.py'):
215 if fname in skip_tests:
216 continue
217 path_no_py = os.path.join(dirpath, fname).rstrip('.py')
218 assert path_no_py.startswith(ROOT)
219 names = path_no_py[len(ROOT) + 1:].split('/')
220 modules.append(names)
221 if options.debug:
222 print 'testing', path_no_py
223 return modules
23def chdir(module):
24 os.chdir(os.path.dirname(module.__file__))
72def ispymodule(self):
73 '''Check if this :class:`Path` is a python module.'''
74 if self.isdir():
75 return os.path.isfile(os.path.join(self, '__init__.py'))
76 elif self.isfile():
77 return self.endswith('.py')
23@property
24def python(self):
25 return self.get_binary('python')

Related snippets