def test_load_package(): import testpkg1 pkg = imp.load_package('libcopy', testpkg1.__path__[0]) AreEqual(sys.modules['libcopy'], pkg) pkg = imp.load_package('some_new_pkg', 'some_path_that_does_not_and_never_will_exist') AreEqual(sys.modules['some_new_pkg'], pkg)
def test_load_package(self): import testpkg1 pkg = imp.load_package('libcopy', testpkg1.__path__[0]) self.assertEqual(sys.modules['libcopy'], pkg) with self.assertRaises(AttributeError): # AttributeError: 'NoneType' object has no attribute 'name' imp.load_package('some_new_pkg', 'some_path_that_does_not_and_never_will_exist')
def _load_plugins(self, plugin_dir, namespace): """ Load all modules in plugin_dir """ if not os.path.exists(os.path.join(plugin_dir, "__init__.py")): raise Exception( "The plugin directory %s should be a valid python package with a __init__.py file" % plugin_dir) mod_name = ".".join(namespace.to_path()) imp.load_package(mod_name, plugin_dir) for py_file in glob.glob(os.path.join(plugin_dir, "*.py")): if not py_file.endswith("__init__.py"): # name of the python module sub_mod = mod_name + "." + os.path.basename(py_file).split( ".")[0] # create a namespace for the submodule new_ns = Namespace(sub_mod.split(".")[-1]) new_ns.parent = namespace self.graph.add_namespace(new_ns, namespace) # load the python file imp.load_source(sub_mod, py_file)
def load_plugin(plugin_path): if not os.path.exists(plugin_path): raise IOError('Plugin path not found: {}'.format(plugin_path)) config_path = os.path.join(plugin_path, 'config.json') if not os.path.exists(config_path): raise ImportError('No config found in {}'.format(plugin_path)) config = json.load(open(config_path)) imp.load_package(config['name'], plugin_path) importlib.import_module('taas.strategies.builtin')
def open(self, tracker_home, optimize=0): """Open the tracker. Parameters: tracker_home: tracker home directory optimize: if set, precompile html templates Raise ValueError if the tracker home doesn't exist. """ import imp # sanity check existence of tracker home if not os.path.exists(tracker_home): raise ValueError, 'no such directory: "%s"'%tracker_home # sanity check tracker home contents for reqd in 'config dbinit select_db interfaces'.split(): if not os.path.exists(os.path.join(tracker_home, '%s.py'%reqd)): raise TrackerError, 'File "%s.py" missing from tracker '\ 'home "%s"'%(reqd, tracker_home) if self.trackers.has_key(tracker_home): return imp.load_package(self.trackers[tracker_home], tracker_home) # register all available backend modules backends.list_backends() self.number = self.number + 1 modname = '_roundup_tracker_%s'%self.number self.trackers[tracker_home] = modname # load the tracker tracker = imp.load_package(modname, tracker_home) # ensure the tracker has all the required bits for required in 'open init Client MailGW'.split(): if not hasattr(tracker, required): raise TrackerError, \ 'Required tracker attribute "%s" missing'%required # load and apply the config tracker.config = configuration.CoreConfig(tracker_home) tracker.dbinit.config = tracker.config tracker.optimize = optimize tracker.templates = templating.Templates(tracker.config["TEMPLATES"]) if optimize: tracker.templates.precompileTemplates() return tracker
def setup(base_path, root_module_name="lpt"): if sys.modules.has_key(root_module_name): # already set up return _create_module_and_parents(root_module_name) imp.load_package(root_module_name, base_path) # Allow locally installed third party packages to be found. # This is primarily for the benefit of frontend and tko so that they # may use libraries other than those available as system packages. sys.path.insert(0, os.path.join(base_path, "site-packages"))
def open(self, tracker_home, optimize=0): """Open the tracker. Parameters: tracker_home: tracker home directory optimize: if set, precompile html templates Raise ValueError if the tracker home doesn't exist. """ import imp # sanity check existence of tracker home if not os.path.exists(tracker_home): raise ValueError('no such directory: "%s"' % tracker_home) # sanity check tracker home contents for reqd in 'config dbinit select_db interfaces'.split(): if not os.path.exists(os.path.join(tracker_home, '%s.py' % reqd)): raise TrackerError('File "%s.py" missing from tracker '\ 'home "%s"'%(reqd, tracker_home)) if tracker_home in self.trackers: return imp.load_package(self.trackers[tracker_home], tracker_home) # register all available backend modules backends.list_backends() self.number = self.number + 1 modname = '_roundup_tracker_%s' % self.number self.trackers[tracker_home] = modname # load the tracker tracker = imp.load_package(modname, tracker_home) # ensure the tracker has all the required bits for required in 'open init Client MailGW'.split(): if not hasattr(tracker, required): raise TrackerError('Required tracker attribute "%s" missing' % required) # load and apply the config tracker.config = configuration.CoreConfig(tracker_home) tracker.dbinit.config = tracker.config tracker.optimize = optimize tracker.templates = templating.get_loader(tracker.config["TEMPLATES"]) if optimize: tracker.templates.precompile() return tracker
def _load_plugins(self, plugin_dir, namespace): """ Load all modules in plugin_dir """ if not os.path.exists(os.path.join(plugin_dir, "__init__.py")): raise Exception("The plugin directory %s should be a valid python package with a __init__.py file" % plugin_dir) mod_name = ".".join(namespace.to_path()) imp.load_package(mod_name, plugin_dir) for py_file in glob.glob(os.path.join(plugin_dir, "*.py")): if not py_file.endswith("__init__.py"): sub_mod = mod_name + "." + os.path.basename(py_file).split(".")[0] imp.load_source(sub_mod, py_file)
def load_ais(base_folder="AIs"): ''' Finds packages containing AIs from the root->AIs folder and returns a list of the AI objects they contain. ''' from ..game import PlayerAIBase # We find the AI package folders, which live in the "AIs" folder... ai_folders = [item for item in os.listdir(base_folder)] # We loop through the packages... ais = [] for ai_folder in ai_folders: if not ai_folder.startswith("."): # We load each package... package_folder = os.path.join(base_folder,ai_folder) print(package_folder) ai_package = imp.load_package(ai_folder, package_folder) # We find the classes they expose that are derived from the # base AI class... ais_in_package = _find_derived_classes(ai_package, PlayerAIBase) ais.extend(ais_in_package) return ais
def setup(base_path, root_module_name="caliper"): """ Setup a library namespace, with the appropriate top root module name. Perform all the necessary setup so that all the packages at 'base_path' can be imported via 'import root_module_name,package' :param base_path: Base path for the module :parma root_module_name: Top level name for the module """ if sys.modules.has_key(root_module_name): return _create_module_and_parents(root_module_name) imp.load_package(root_module_name, base_path) # allow locally installed third party packages to be found. sys.path.insert(0, os.path.join(base_path, "site_packages"))
def loader_lp(uri, name=None): mountpoint = get_mount_point() name = name or __new_submod_name() modname = mountpoint + '.' + name mod = imp.load_package(modname, uri) setattr(sys.modules[mountpoint], name, mod) exec 'import %s' %modname ## sanity check return Plugin(mod.__plgname__, mod.__entries__, mod.__extensions__)
def reRaiseECO(self, eco): import json if eco.exceptionmodule: mod = imp.load_package(eco.exceptionmodule, eco.exceptionmodule) else: import __builtin__ as mod Klass = getattr(mod, eco.exceptionclassname, RuntimeError) exc = Klass(eco.errormessage) for key, value in json.loads(eco.exceptioninfo).iteritems(): setattr(exc, key, value) raise exc
def reRaiseECO(self, eco): if eco.exceptionmodule: mod = imp.load_package(eco.exceptionmodule, eco.exceptionmodule) else: import builtins as mod Klass = getattr(mod, eco.exceptionclassname, RuntimeError) exc = Klass(eco.errormessage) for key, value in list( j.data.serializer.json.loads(eco.exceptioninfo).items()): setattr(exc, key, value) raise exc
def _load_plugins(self): module_dir, _ = get_module_info() plugin_dir = os.path.join(module_dir, "plugins") if not os.path.exists(plugin_dir): return if not os.path.exists(os.path.join(plugin_dir, "__init__.py")): raise Exception( "Plugins directory doesn't have a __init__.py file.") result = {} mod_name = os.path.basename(module_dir) imp.load_package("inmanta_plugins." + mod_name, plugin_dir) for py_file in glob.glob(os.path.join(plugin_dir, "*.py")): sub_mod_path = "inmanta_plugins." + mod_name + "." + os.path.basename( py_file).split(".")[0] imp.load_source(sub_mod_path, py_file) sub_mod = importlib.import_module(sub_mod_path) for k, v in sub_mod.__dict__.items(): if isinstance(v, types.FunctionType): result[k] = v return result
def reRaiseECO(self, eco): import json if eco.exceptionmodule: mod = imp.load_package(eco.exceptionmodule, eco.exceptionmodule) else: import builtins as mod Klass = getattr(mod, eco.exceptionclassname, RuntimeError) exc = Klass(eco.errormessage) for key, value in list(json.loads(eco.exceptioninfo).items()): setattr(exc, key, value) raise exc
def setup(base_path, root_module_name=""): """ Perform all the necessary setup so that all the packages at 'base_path' can be imported via "import root_module_name.package". If root_module_name is empty, then all the packages at base_path are inserted as top-level packages. Also, setup all the common.* aliases for modules in the common library. The setup must be different if you are running on an Autotest server or on a test machine that just has the client directories installed. """ setup_client_only = False try: sys.modules[root_module_name] except KeyError: # Hack... Any better ideas? if root_module_name == 'autotest.client': serverdir = os.path.join(os.path.dirname(__file__), '..', 'server') full_source = os.path.exists(serverdir) if full_source: root_module_name = 'autotest' base_path = os.path.abspath(os.path.join(base_path, '..')) else: setup_client_only = True if setup_client_only: _create_module_and_parents(root_module_name) imp.load_package(root_module_name, base_path) else: _create_module_and_parents(root_module_name) _import_children_into_module(root_module_name, base_path) # Allow locally installed third party packages to be found # before any that are installed on the system itself when not. # running as a client. # This is primarily for the benefit of frontend and tko so that they # may use libraries other than those available as system packages. sys.path.insert(0, os.path.join(base_path, "site-packages")) _monkeypatch_logging_handle_error()
def setup(base_path, root_module_name="autotest"): """ Setup a library namespace, with the appropriate top root module name. Perform all the necessary setup so that all the packages at 'base_path' can be imported via "import root_module_name.package". :param base_path: Base path for the module. :param root_module_name: Top level name for the module. """ if sys.modules.has_key(root_module_name): # already set up return _create_module_and_parents(root_module_name) imp.load_package(root_module_name, base_path) # Allow locally installed third party packages to be found. # This is primarily for the benefit of frontend and tko so that they # may use libraries other than those available as system packages. sys.path.insert(0, os.path.join(base_path, "site-packages"))
def load_from_path(plugin_path): packages = setuptools.find_packages(plugin_path) for pkg in packages: manifest_file = Path(plugin_path)/pkg/'manifest.py' if not manifest_file.exists(): continue imp.load_package(pkg, Path(plugin_path)/pkg) __import__(pkg+'.manifest') # There can only one manifest in each plugin if len(manifest_cache) > 1: raise ManifestError('More than one manifest in this plugin') if len(manifest_cache) == 0: raise ManifestError(f'Not found any manifest in {plugin_path}') # TODO manifest = manifest_cache[0][0] caller_info = manifest_cache[0][1] plugin = Plugin(manifest['id'], **manifest) plugin.location = str(Path(caller_info.filename).parent) return plugin
def setup(base_path, root_module_name="autotest"): """ Setup a library namespace, with the appropriate top root module name. Perform all the necessary setup so that all the packages at 'base_path' can be imported via "import root_module_name.package". :param base_path: Base path for the module. :param root_module_name: Top level name for the module. """ if root_module_name in sys.modules: # already set up return _create_module_and_parents(root_module_name) imp.load_package(root_module_name, base_path) # Allow locally installed third party packages to be found. # This is primarily for the benefit of frontend and tko so that they # may use libraries other than those available as system packages. sys.path.insert(0, os.path.join(base_path, "site-packages"))
def _load_codec_module(self, path, expected_class): """ Return and load expected_class from path file. """ if os.path.isfile(path): mod_name, file_ext = os.path.splitext(os.path.basename(path)) if file_ext.lower() == '.py': py_mod = imp.load_source(mod_name, path) elif file_ext.lower() == '.pyc': py_mod = imp.load_compiled(mod_name, path) else: mod_name = os.path.basename(path) py_mod = imp.load_package(mod_name, path) if hasattr(py_mod, expected_class): return getattr(py_mod, expected_class)() return None
def modules(self): """ Return a dict of module names and module info. Each value of the dict is a list of functions and classes as returned by get_functions() and get_classes(). """ modules = {} for node in self._tree.body: for subnode in ast.walk(node): if isinstance(subnode, ast.Import): module_name = subnode.names[0].name elif isinstance(subnode, ast.ImportFrom): module_name = subnode.module imported_name = subnode.names[0].name else: continue try: module_tree = _parse_module(module_name) except TypeError: # .so files loaded_module = imp.load_dynamic( module_name, imp.find_module(module_name)[1]) module_info = dir(loaded_module) except IOError: # directories loaded_module = imp.load_package( module_name, imp.find_module(module_name)[1]) module_info = dir(loaded_module) else: module_info = self._functions() + self._classes() finally: if isinstance(subnode, ast.ImportFrom): if imported_name in module_info: modules[imported_name] = module_info[imported_name] else: modules[module_name] = module_info return modules
def load_ais(): ''' Finds packages containing AIs from the root->AIs folder and returns a list of the AI objects they contain. ''' from ..game import PlayerAIBase # We find the AI package folders, which live in the "AIs" folder... ai_folders = [item for item in os.listdir("AIs")] # We loop through the packages... ais = [] for ai_folder in ai_folders: # We load each package... package_folder = "AIs/" + ai_folder ai_package = imp.load_package(ai_folder, package_folder) # We find the classes they expose that are derived from the # base AI class... ais_in_package = _find_derived_classes(ai_package, PlayerAIBase) ais.extend(ais_in_package) return ais
def getPrjPackage(): """ Так как папка проекта является пакетом Python, то можно получить объект пакета. @return: Объект пакета прикладного проекта. """ prj_dir = getPrjDir() prj_name = getPrjName() if not prj_dir: log.warning( u'Не определена папка проекта для получения объекта пакета Python') return None if not prj_name: log.warning( u'Не определено имя проекта для получения объекта пакета Python') return None try: return imp.load_package(prj_name, prj_dir) except: log.fatal(u'Ошибка создания объекта пакета Python <%s> : <%s>' % (prj_name, prj_dir)) return None
import imp import os.path as osp abs_path = osp.dirname(osp.abspath(__file__)) deep_sort = imp.load_package( 'deep_sort', osp.join(abs_path, 'deep_sort/deep_sort')) application_util = imp.load_package( 'application_util', osp.join(abs_path, 'deep_sort/application_util')) deep_sort_app = imp.load_source( 'deep_sort_app', osp.join(abs_path, 'deep_sort/deep_sort_app.py'))
def _get_subclasses(self, parent=None, parent_class=None, recursive=True): """ Import all submodules of the package containing the present class. Includes subpackages recursively, if specified. :param parent: package/class. If package looks for the classes in submodules. If class, first looks for the package where it is contained :param parent_class: class of which to look for subclasses :param recursive: True/False (go recursively into submodules) """ import pkgutil import imp import inspect import os # If no parent class is specified set it to self.__class parent = self.__class__ if parent is None else parent # Suppose parent is class if inspect.isclass(parent): # Set the package where the class is contained classfile = inspect.getfile(parent) package_path = os.path.dirname(classfile) # If parent class is not specified, assume it is the parent if parent_class is None: parent_class = parent # Suppose parent is a package (directory containing __init__.py). # Check if it contains attribute __path__ elif inspect.ismodule(parent) and hasattr(parent, '__path__'): # Assume path is one element list package_path = parent.__path__[0] # if parent is a package, parent_class cannot be None if parent_class is None: raise TypeError('argument parent_class cannot be None') # Recursively check the subpackages results = {} for _, name, is_pkg in pkgutil.walk_packages([package_path]): # N.B. pkgutil.walk_package requires a LIST of paths. full_path_base = os.path.join(package_path, name) if is_pkg: app_module = imp.load_package(full_path_base, full_path_base) else: full_path = full_path_base + '.py' # I could use load_module but it takes lots of arguments, # then I use load_source app_module = imp.load_source('rst' + name, full_path) # Go through the content of the module if not is_pkg: for fname, obj in inspect.getmembers(app_module): if inspect.isclass(obj) and issubclass(obj, parent_class): results[fname] = obj # Look in submodules elif is_pkg and recursive: results.update( self._get_subclasses(parent=app_module, parent_class=parent_class)) return results
from setuptools import setup, find_packages import os import imp def find_package_data(module, path): """Find all data files to include in the package""" files = [] for dirpath, dirnames, filenames in os.walk(os.path.join(module,path)): for filename in filenames: files.append(os.path.relpath(os.path.join(dirpath,filename),module)) return {module:files} #Setup the application using meta information from #the plugin's own __dist__ object: plugin = imp.load_package(module_name,module_name) setup(name=module_name, description=plugin.__dist__['pypi_description'], version=plugin.__version__, author=plugin.__dist__["author"], url=plugin.__dist__["url"], packages=[module_name], package_data = find_package_data(module_name,"site_src"), include_package_data = True, install_requires =['blogofile'], entry_points = { "blogofile.plugins": ["{module_name} = {module_name}".format(**locals())] } )
def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing pathes. locale_encoding = locale.getpreferredencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { 'utf-8' : b'\xe4', 'cp1250' : b'\x8C', 'cp1251' : b'\xc0', 'cp1252' : b'\xc0', 'cp1253' : b'\xc1', 'cp1254' : b'\xc0', 'cp1255' : b'\xe0', 'cp1256' : b'\xe0', 'cp1257' : b'\xc0', 'cp1258' : b'\xc0', } special_char = known_locales.get(locale_encoding) if special_char: encoded_char = special_char.decode(locale_encoding) temp_mod_name = 'test_imp_helper_' + encoded_char test_package_name = 'test_imp_helper_package_' + encoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: with open(temp_mod_name + '.py', 'w') as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) self.assertNotEquals(None, file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEquals('.py', info[0]) self.assertEquals('U', info[1]) self.assertEquals(imp.PY_SOURCE, info[2]) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEquals(1, mod.a) file.close() mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEquals(1, mod.a) mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc') self.assertEquals(1, mod.a) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w') as file: file.write('b = 2\n') package = imp.load_package(test_package_name, test_package_name) self.assertEquals(2, package.b) finally: support.unlink(temp_mod_name + '.py') support.unlink(temp_mod_name + '.pyc') support.unlink(temp_mod_name + '.pyo') support.unlink(init_file_name + '.py') support.unlink(init_file_name + '.pyc') support.unlink(init_file_name + '.pyo') support.rmtree(test_package_name)
def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing pathes. # the return encoding could be uppercase or None fs_encoding = sys.getfilesystemencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { 'utf-8' : b'\xc3\xa4', 'cp1250' : b'\x8C', 'cp1251' : b'\xc0', 'cp1252' : b'\xc0', 'cp1253' : b'\xc1', 'cp1254' : b'\xc0', 'cp1255' : b'\xe0', 'cp1256' : b'\xe0', 'cp1257' : b'\xc0', 'cp1258' : b'\xc0', } if sys.platform == 'darwin': self.assertEqual(fs_encoding, 'utf-8') # Mac OS X uses the Normal Form D decomposition # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html special_char = b'a\xcc\x88' else: special_char = known_locales.get(fs_encoding) if not special_char: self.skipTest("can't run this test with %s as filesystem encoding" % fs_encoding) decoded_char = special_char.decode(fs_encoding) temp_mod_name = 'test_imp_helper_' + decoded_char test_package_name = 'test_imp_helper_package_' + decoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: # if the curdir is not in sys.path the test fails when run with # ./python ./Lib/test/regrtest.py test_imp sys.path.insert(0, os.curdir) with open(temp_mod_name + '.py', 'w') as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) with file: self.assertIsNotNone(file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEqual(info[0], '.py') self.assertEqual(info[1], 'r') self.assertEqual(info[2], imp.PY_SOURCE) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') if not sys.dont_write_bytecode: mod = imp.load_compiled( temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w') as file: file.write('b = 2\n') with warnings.catch_warnings(): warnings.simplefilter('ignore') package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] for ext in ('.py', '.pyc', '.pyo'): support.unlink(temp_mod_name + ext) support.unlink(init_file_name + ext) support.rmtree(test_package_name) support.rmtree('__pycache__')
def _do_load_module(self, name): real_name = 'canivett_' + name _, path, *_ = imp.find_module(real_name) # NOQA module = imp.load_package(real_name, path) self.modules[name] = module return module
import numpy as np import matplotlib.pyplot as plt import pandas as pd #import catboost import imp import sys # Manually load the model incase it does now work with import imp.load_package('catboost', 'C:\python\Lib\site-packages\catboost') import catboost as cb print(sys.path) sys.path.append('C:\python\Lib\site-packages\mypackages') from catboost import CatBoostRegressor import pandas as pd import sys import numpy as np import scipy as sp import matplotlib import matplotlib.pyplot as plt import seaborn as sns import sklearn as sk from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingClassifier ds = pd.read_csv(r'file_name.csv') cols = ds.columns.tolist() n = int(cols.index('Y_Col')) cols = cols[:n] + cols[n + 1:] + [cols[n]]
import imp import numpy as np import pandas as pd # import rampwf as rw local_workflow = imp.load_package('workflow', './workflow') problem_title = 'Mars craters detection and classification' # A type (class) which will be used to create wrapper objects for y_pred Predictions = local_workflow.predictions.Predictions # An object implementing the workflow workflow = local_workflow.workflow.ObjectDetector() score_types = [ local_workflow.scores.SCP(precision=4), local_workflow.scores.OSPA(precision=4), local_workflow.scores.AveragePrecision(precision=4), local_workflow.scores.Precision(precision=4), local_workflow.scores.Recall(precision=4), ] def get_cv(folder_X, y): # _, X = folder_X # cv = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=57) # return cv.split(X, y)
""" from ._version import __version__ # mokeypatch cclib/chemlab classes improved for this implementation # TODO don't think this is working, use mock? import os, sys, platform ##TODO a weird thing sometimes happens if docx installed, #whereby 'import enum' installs docx/enum instead of enum (req for numba)!? if platform.system() == 'Windows': import imp init = os.path.abspath(__file__) pkg = os.path.dirname(init) sites = os.path.dirname(pkg) imp.load_package('enum', os.path.join(sites, 'enum')) from .cclib_patch.parser import data sys.modules['cclib.parser.data'] = data import cclib # version 1.3 import chemlab # version 0.4 from .molecule import Molecule from .analysis import Analysis from .file_io import Folder from .docs import MSDocument from .utils import df_to_img, set_imagik_exe import cPickle as pkl
def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing pathes. # the return encoding could be uppercase or None fs_encoding = sys.getfilesystemencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { "utf-8": b"\xc3\xa4", "cp1250": b"\x8C", "cp1251": b"\xc0", "cp1252": b"\xc0", "cp1253": b"\xc1", "cp1254": b"\xc0", "cp1255": b"\xe0", "cp1256": b"\xe0", "cp1257": b"\xc0", "cp1258": b"\xc0", } if sys.platform == "darwin": self.assertEqual(fs_encoding, "utf-8") # Mac OS X uses the Normal Form D decomposition # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html special_char = b"a\xcc\x88" else: special_char = known_locales.get(fs_encoding) if not special_char: self.skipTest("can't run this test with %s as filesystem encoding" % fs_encoding) decoded_char = special_char.decode(fs_encoding) temp_mod_name = "test_imp_helper_" + decoded_char test_package_name = "test_imp_helper_package_" + decoded_char init_file_name = os.path.join(test_package_name, "__init__.py") try: # if the curdir is not in sys.path the test fails when run with # ./python ./Lib/test/regrtest.py test_imp sys.path.insert(0, os.curdir) with open(temp_mod_name + ".py", "w") as file: file.write("a = 1\n") file, filename, info = imp.find_module(temp_mod_name) with file: self.assertIsNotNone(file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEqual(info[0], ".py") self.assertEqual(info[1], "U") self.assertEqual(info[2], imp.PY_SOURCE) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) mod = imp.load_source(temp_mod_name, temp_mod_name + ".py") self.assertEqual(mod.a, 1) mod = imp.load_compiled(temp_mod_name, imp.cache_from_source(temp_mod_name + ".py")) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, "w") as file: file.write("b = 2\n") package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] for ext in (".py", ".pyc", ".pyo"): support.unlink(temp_mod_name + ext) support.unlink(init_file_name + ext) support.rmtree(test_package_name)
# # $Id$ # --------------------------------------------------------------------------- from distutils.core import setup import re import os import sys import imp # Load the data. here = os.path.dirname(os.path.abspath(sys.argv[0])) sys.path = [here] + sys.path mf = os.path.join(here, 'adolpy') adolpy = imp.load_package('adolpy', mf) long_description = adolpy.__doc__ version = str(adolpy.__version__) author = adolpy.__author__ email = adolpy.__email__ url = adolpy.__url__ license = adolpy.__license__ setup(name="adolpy", version=version, description="Automatic differentiation using the forward model", long_description=long_description, url=url, license=license, author=author,
def load_pkg(name, location, parent): pname = modname(parent) + '.' + name module = imp.load_package(pname, location) setattr(sysmod(parent), pname, module) return (pname, module)
def get_module(path): module = imp.load_package('module',path) return module
#!/usr/bin/env python # Standard Python Imports import os import numpy as np import time import imp import sys from argparse import ArgumentParser # OpenRAVE from openravepy import * #RaveInitialize(True, DebugLevel.Debug) # Import Dfab Python, Watch out for hard coded directory dfab_pack = imp.load_package('dfab', './dfab-master/python/dfab/') from dfab.mocap import extract_trajectory, datafiles from dfab.geometry.quaternion import to_threexform from dfab.rapid.joint_sequence import single_trajectory_program curr_path = os.getcwd() relative_ordata = '/models' ordata_path_thispack = curr_path + relative_ordata #this sets up the OPENRAVE_DATA environment variable to include the files we're using openrave_data_path = os.getenv('OPENRAVE_DATA', '') openrave_data_paths = openrave_data_path.split(':') if ordata_path_thispack not in openrave_data_paths: if openrave_data_path == '': os.putenv('OPENRAVE_DATA', ordata_path_thispack) else:
#!/usr/bin/env python # Standard Python Imports import os import numpy as np import time import imp import sys from argparse import ArgumentParser # OpenRAVE from openravepy import * #RaveInitialize(True, DebugLevel.Debug) # Import Dfab Python, Watch out for hard coded directory dfab_pack = imp.load_package('dfab', '../dfab/python/dfab/') from dfab.mocap import extract_trajectory, datafiles from dfab.geometry.quaternion import to_threexform from dfab.rapid.joint_sequence import single_trajectory_program from scipy.optimize import fmin from math import sqrt, cos, sin, radians curr_path = os.getcwd() relative_ordata = '/models' ordata_path_thispack = curr_path + relative_ordata #this sets up the OPENRAVE_DATA environment variable to include the files we're using openrave_data_path = os.getenv('OPENRAVE_DATA', '') openrave_data_paths = openrave_data_path.split(':') if ordata_path_thispack not in openrave_data_paths:
import imp import os import sys from pathlib import Path project_dir = Path(Path.cwd(), __file__).parent # Remove the distribution directory from sys.path if os.path.abspath(sys.path[0]) == str(project_dir): del sys.path[0] try: import sgfmill except ImportError: sys.exit("test_installed_sgfmill: can't find the sgfmill package") PACKAGE_NAME = "sgfmill_tests" # Make sgfmill_tests importable without the sibling sgfmill packagepath = Path(project_dir, PACKAGE_NAME) mdl = imp.load_package(PACKAGE_NAME, str(packagepath)) sys.modules[PACKAGE_NAME] = mdl found = Path(Path.cwd(), sgfmill.__file__).parent print("testing sgfmill package in %s" % found, file=sys.stderr) from sgfmill_tests import run_sgfmill_testsuite run_sgfmill_testsuite.run(sys.argv[1:])
def get_module(path): module = imp.load_package('module', path) return module
def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing pathes. locale_encoding = locale.getpreferredencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { 'utf-8': b'\xe4', 'cp1250': b'\x8C', 'cp1251': b'\xc0', 'cp1252': b'\xc0', 'cp1253': b'\xc1', 'cp1254': b'\xc0', 'cp1255': b'\xe0', 'cp1256': b'\xe0', 'cp1257': b'\xc0', 'cp1258': b'\xc0', } special_char = known_locales.get(locale_encoding) if special_char: encoded_char = special_char.decode(locale_encoding) temp_mod_name = 'test_imp_helper_' + encoded_char test_package_name = 'test_imp_helper_package_' + encoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: with open(temp_mod_name + '.py', 'w') as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) self.assertNotEquals(None, file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEquals('.py', info[0]) self.assertEquals('U', info[1]) self.assertEquals(imp.PY_SOURCE, info[2]) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEquals(1, mod.a) file.close() mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEquals(1, mod.a) mod = imp.load_compiled(temp_mod_name, temp_mod_name + '.pyc') self.assertEquals(1, mod.a) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w') as file: file.write('b = 2\n') package = imp.load_package(test_package_name, test_package_name) self.assertEquals(2, package.b) finally: support.unlink(temp_mod_name + '.py') support.unlink(temp_mod_name + '.pyc') support.unlink(temp_mod_name + '.pyo') support.unlink(init_file_name + '.py') support.unlink(init_file_name + '.pyc') support.unlink(init_file_name + '.pyo') support.rmtree(test_package_name)
def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing pathes. # the return encoding could be uppercase or None fs_encoding = sys.getfilesystemencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { 'utf-8': b'\xc3\xa4', 'cp1250': b'\x8C', 'cp1251': b'\xc0', 'cp1252': b'\xc0', 'cp1253': b'\xc1', 'cp1254': b'\xc0', 'cp1255': b'\xe0', 'cp1256': b'\xe0', 'cp1257': b'\xc0', 'cp1258': b'\xc0', } if sys.platform == 'darwin': self.assertEqual(fs_encoding, 'utf-8') # Mac OS X uses the Normal Form D decomposition # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html special_char = b'a\xcc\x88' else: special_char = known_locales.get(fs_encoding) if not special_char: self.skipTest( "can't run this test with %s as filesystem encoding" % fs_encoding) decoded_char = special_char.decode(fs_encoding) temp_mod_name = 'test_imp_helper_' + decoded_char test_package_name = 'test_imp_helper_package_' + decoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: # if the curdir is not in sys.path the test fails when run with # ./python ./Lib/test/regrtest.py test_imp sys.path.insert(0, os.curdir) with open(temp_mod_name + '.py', 'w') as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) with file: self.assertIsNotNone(file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEqual(info[0], '.py') self.assertEqual(info[1], 'r') self.assertEqual(info[2], imp.PY_SOURCE) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') if not sys.dont_write_bytecode: mod = imp.load_compiled( temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w') as file: file.write('b = 2\n') with warnings.catch_warnings(): warnings.simplefilter('ignore') package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] for ext in ('.py', '.pyc'): support.unlink(temp_mod_name + ext) support.unlink(init_file_name + ext) support.rmtree(test_package_name) support.rmtree('__pycache__')
def update_event(self, inp=-1): self.set_output_val(0, imp.load_package(self.input(0), self.input(1)))
def test_issue5604(self): fs_encoding = sys.getfilesystemencoding() known_locales = { 'utf-8': b'\xc3\xa4', 'cp1250': b'\x8c', 'cp1251': b'\xc0', 'cp1252': b'\xc0', 'cp1253': b'\xc1', 'cp1254': b'\xc0', 'cp1255': b'\xe0', 'cp1256': b'\xe0', 'cp1257': b'\xc0', 'cp1258': b'\xc0' } if sys.platform == 'darwin': self.assertEqual(fs_encoding, 'utf-8') special_char = b'a\xcc\x88' else: special_char = known_locales.get(fs_encoding) if not special_char: self.skipTest( "can't run this test with %s as filesystem encoding" % fs_encoding) decoded_char = special_char.decode(fs_encoding) temp_mod_name = 'test_imp_helper_' + decoded_char test_package_name = 'test_imp_helper_package_' + decoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: sys.path.insert(0, os.curdir) with open(temp_mod_name + '.py', 'w') as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) with file: self.assertIsNotNone(file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEqual(info[0], '.py') self.assertEqual(info[1], 'r') self.assertEqual(info[2], imp.PY_SOURCE) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') if not sys.dont_write_bytecode: mod = imp.load_compiled( temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w') as file: file.write('b = 2\n') with warnings.catch_warnings(): warnings.simplefilter('ignore') package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] for ext in ('.py', '.pyc'): support.unlink(temp_mod_name + ext) support.unlink(init_file_name + ext) support.rmtree(test_package_name) support.rmtree('__pycache__')
# # $Id$ # --------------------------------------------------------------------------- from distutils.core import setup import re import os import sys import imp # Load the data. here = os.path.dirname(os.path.abspath(sys.argv[0])) sys.path = [here] + sys.path mf = os.path.join(here, 'adolpy') adolpy = imp.load_package('adolpy', mf) long_description = adolpy.__doc__ version = str(adolpy.__version__) author = adolpy.__author__ email = adolpy.__email__ url = adolpy.__url__ license = adolpy.__license__ setup( name="adolpy", version=version, description="Automatic differentiation using the forward model", long_description=long_description, url=url, license=license,