python using variables from another file

You can import the variables from the file: vardata.py verb_list = [x, y, z] other_list = [1, 2, 3] something_else = False mainfile.py from vardata import verb_list, other_list import random print random.choice(verb_list) you can also do: from vardata import * to import everything from that file. Be careful with this though. You don’t want to … Read more

How can I import a module dynamically 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