How to use prune_sys_dot_modules method in Contexts

Best Python code snippet using Contexts

Run Contexts automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

__init__.py

Source: __init__.py Github

copy
1import importlib
2import os
3import sys
4
5
6class Importer(object):
7    def initialise(self, args, env):
8        return True
9
10    def import_module(self, dir_path, module_name):
11        filename = resolve_filename(dir_path, module_name)
12        prune_sys_dot_modules(module_name, filename)
13        if module_name in sys.modules:
14            return
15
16        loader = self.get_loader(module_name, filename)
17        return loader.load_module(module_name)
18
19    def get_loader(self, module_name, filename):
20        return importlib.machinery.SourceFileLoader(module_name, filename)
21
22    def __eq__(self, other):
23        return type(self) == type(other)
24
25
26def resolve_filename(dir_path, module_name):
27    filename = os.path.join(dir_path, *module_name.split('.'))
28    if os.path.isdir(filename):  # it's a package
29        filename = os.path.join(filename, '__init__.py')
30    else:
31        filename += '.py'
32    return filename
33
34
35def prune_sys_dot_modules(module_name, module_location):
36    if module_name in sys.modules:
37        existing_module = sys.modules[module_name]
38        if not same_file(existing_module.__file__, module_location):
39            del sys.modules[module_name]
40
41
42def same_file(path1, path2):
43    return os.path.realpath(path1) == os.path.realpath(path2)
44
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Contexts on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)