Relative imports in Python 3

unfortunately, this module needs to be inside the package, and it also needs to be runnable as a script, sometimes. Any idea how I could achieve that? It’s quite common to have a layout like this… main.py mypackage/ __init__.py mymodule.py myothermodule.py …with a mymodule.py like this… #!/usr/bin/env python3 # Exported function def as_int(a): return int(a) … Read more

How do I import a module given the full path?

For Python 3.5+ use (docs): import importlib.util import sys spec = importlib.util.spec_from_file_location(“module.name”, “/path/to/file.py”) foo = importlib.util.module_from_spec(spec) sys.modules[“module.name”] = foo spec.loader.exec_module(foo) foo.MyClass() For Python 3.3 and 3.4 use: from importlib.machinery import SourceFileLoader foo = SourceFileLoader(“module.name”, “/path/to/file.py”).load_module() foo.MyClass() (Although this has been deprecated in Python 3.4.) For Python 2 use: import imp foo = imp.load_source(‘module.name’, ‘/path/to/file.py’) foo.MyClass() … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)